gpt4 book ai didi

excel - 将范围与单个值连接

转载 作者:行者123 更新时间:2023-12-03 02:45:38 27 4
gpt4 key购买 nike

我正在尝试将范围与单个值连接起来。

Sub Macro1()
Dim rngOne As Range, strngOne as String, combos As Range

'finds the last row and sets it as the ending range
Dim LastRowColC As Integer
LastRowColC = Range("C65536").End(xlUp).Row

Set rngOne = Worksheets(1).Range("C3" & LastRowColC)
strngOne = "00000"
combos = rngOne & strngOne

Range("D3").Select
Insert combos
End Sub

为什么这不将变量“combos”插入到单元格中?

更多说明(从评论中复制)

基本上,我想获取 C 列的每个单元格中的值,并将 00000 添加到所有这些值的末尾。因此,如果 C1 为 50,我希望最终结果复制 50 并将 C1 替换为 5000000,如果 C2 为 575,则将其替换为 57500000,所有这些都在 C 中的数据范围内。

如果不可能的话,我更愿意将其粘贴到同一列中的值上。然后对于您给出的示例,我想要 D1= AAA00000、D2=BBB00000、D3 =CCC00000 等

最佳答案

这就是你正在尝试的吗?我给了你两种方法。

方式1

Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
Dim rng As Range

'~~> Change this to the relevant worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")

With ws
'~~> Get last row in Col C
lRow = .Range("C" & .Rows.Count).End(xlUp).Row

'~~> Construct your range
Set rng = .Range("C3:C" & lRow)

'~~> Multiply all the cells in the range with 100000
'~~> so that 55 become 5500000, 123 becomes 12300000 and so on
rng.Value = Evaluate(rng.Address & "*100000")
End With
End Sub

方式2

在单元格 D1 中键入 100000,然后运行此宏

Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
Dim rng As Range

'~~> Change this to the relevant worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")

With ws
'~~> Get last row in Col C
lRow = .Range("C" & .Rows.Count).End(xlUp).Row

'~~> Construct your range
Set rng = .Range("C3:C" & lRow)

'~~> This cell has 100000
.Range("D1").Copy

'~~> Paste Special Value/Multiply
rng.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlMultiply, _
SkipBlanks:=False, _
Transpose:=False

Application.CutCopyMode = False
End With
End Sub

关于excel - 将范围与单个值连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27070173/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com