【发布时间】:2013-03-31 01:56:48
【问题描述】:
我有许多范围要独立连接,并将连接范围的值放入不同的单元格中。
我想:
连接范围 A1:A10 中的值并将结果放入 F1
然后连接 Range B1:B10 并将结果放入 F2
然后连接 Range C1:C10 并将结果放在 F3 等中。
以下宏连接范围 A1:A10,然后将结果放入 F1(这是我想要的)。然而,它还将第一个串联的信息存储到内存中,这样当它进行下一个串联时,在单元格 F2 中,我得到 F1 和 F2 的串联结果。
Sub concatenate()
Dim x As String
Dim Y As String
For m = 2 To 5
Y = Worksheets("Variables").Cells(m, 5).Value
'Above essentially has the range information e.g. a1:a10 in sheet variables
For Each Cell In Range("" & Y & "") 'i.e. range A1:A10
If Cell.Value = "" Then GoTo Line1 'this tells the macro to continue until a blank cell is reached
x = x & Cell.Value & "," 'this provides the concatenated cell value
Next
Line1:
ActiveCell.Value = x
ActiveCell.Offset(1, 0).Select
Next m
End Sub
【问题讨论】:
-
在
Next m之前插入简单语句:x="" -
哦,天才!我在这上面浪费了一整天!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!
标签: excel vba concatenation