Sub ConcatColumns()
Do While ActiveCell <> "" 'Loops until the active cell is blank.
'The "&" must have a space on both sides or it will be
'treated as a variable type of long integer.
ActiveCell.Offset(0, 1).FormulaR1C1 = _
ActiveCell.Offset(0, -1) & " , " & ActiveCell.Offset(0, 0)
ActiveCell.Offset(1, 0).Select
Loop
End Sub
以上是我的代码。
我想连接A&amp;列。 B进入C列,中间用逗号。 如果列A / B为空,则列C不应该有逗号,而只是值本身。
答案 0 :(得分:0)
根据您的评论,您尝试执行Excel公式=IF(A1="",IF(B1="","",B1),IF(B1="",A1,A1&" , "&B1))
之类的操作,建议您使用以下代码:
Sub ConcatColumns()
Do While ActiveCell.Value <> "" Or ActiveCell.Offset(0, -1).Value <> ""
With ActiveCell
' IF(A1=""
If .Offset(0, -1).Value = "" Then
' IF(B1=""
If .Value = "" Then
' ""
.Offset(0, 1).Value = "" ' Shouldn't occur, but let's be safe
Else
' B1
.Offset(0, 1).Value = .Value
End If
' IF(B1=""
ElseIf .Value = "" Then
' A1
.Offset(0, 1).Value = .Offset(0, -1).Value
Else
' A1&" , "&B1
.Offset(0, 1).Value = .Offset(0, -1).Value & " , " & .Value
End If
End With
ActiveCell.Offset(1, 0).Select
Loop
End Sub