我必须填充“国家范围”列并使用此公式。但是,如果列中有一个X(列L到Q),我需要填充所有国家/地区名称。国家/地区名称之间应填充分号。见图。
=IF(L3="X","Corporate;",IF(M3="x","Mexico;",IF(N3="x","Argentina;",IF(O3="X","Dubai;",IF(P3="X","Broken Arrow;",IF(Q="X","Brazil;"))))))
我尝试关注https://www.mrexcel.com/forum/excel-questions/553169-concatenate-row-variable-number-columns.html中的代码,但收到类型不匹配错误。我将每列的x替换为受尊重的国家/地区名称。
Sub ConCatFromColumnC()
Dim X As Long, LastRow As Long, LastCol As Long, Delimiter As String
Const StartRow As Long = 1
Delimiter = vbLf
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
For X = StartRow To LastRow
LastCol = Cells(X, Columns.Count).End(xlToLeft).Column
If LastCol = 3 Then
Cells(X, "B").Value = Cells(X, "C").Value
Else
Cells(X, "B").Value = Join(Application.Index(Range(Cells(X, "C"), Cells(X, LastCol)).Value, 1, 0), Delimiter)
End If
Next
End Sub
答案 0 :(得分:0)
现在看起来如下工作。
Sub ConCatFromColumnC()
Dim X As Long, LastRow As Long, LastCol As Long, Delimiter As String
Const StartRow As Long = 2
Delimiter = vbLf
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
For X = StartRow To LastRow
'LastCol = Cells(X, Columns.Count).End(xlToLeft).Column
LastCol = 17
If LastCol = 12 Then
Cells(X, "K").Value = Cells(X, "L").Value
Else
Cells(X, "K").Value = Join(Application.Index(Range(Cells(X, "L"), Cells(X, LastCol)).Value, 1, 0), Delimiter)
End If
Next
End Sub