我有一个重复的任务我想自动化而不是一直使用= Concatenate函数。到目前为止,这是我的代码:
Cells(2, 5).Value = Cells(2, 1).Value&" - "&Cells(2, 2).Value
不幸的是,这会导致"编译错误:预期:声明结束"错误,突出了" - "。我怎样才能将这些文字夹在中间," - ",在这两个值之间?
答案 0 :(得分:6)
Cells(2, 5).Value = Cells(2, 1).Value & " - " & Cells(2, 2).Value
答案 1 :(得分:3)
Option Explicit
Function ConcatenateRow(rowRange As Range, joinString As String) As String
Dim x As Variant, temp As String
temp = ""
For Each x In rowRange
temp = temp & x & joinString
Next
ConcatenateRow = Left(temp, Len(temp) - Len(joinString))
End Function
然后在excel文件中,只需使用此公式,选择要连接的单元格范围,并为其添加一个字符串(在本例中为“ - ”)以放入它们之间。
答案 2 :(得分:2)
一个需要的建议:
Private Sub CommandButton1_Click()
Dim i As Long
Dim j As Long
Dim x As String
i = 1
j = Range("max").Value
x = Cells(i, 2)
For i = 2 To j
x = x & " - " & Cells(i, 2)
Next i
'MsgBox (x)
Range("d1").Value = x
i = 0
End Sub