如何使用 vba excel 将两个不同的数据列合并为一列,例如将 ColumnA 和 ColumnC 中的数据合并到 ColumnF 中,并使用逗号之类的分隔符。如果我有一个输入框来选择要组合的列以及组合结果将出现在哪一列,那就太好了。
例如:
ColA ColC ColF
Apple Orang Apple,Orang
Pear Grape Pear,Grape
如果你真的想要一个宏来提示你输入不同的列,试试这个:
Sub Macro1()
Dim firstCol As String
firstCol = InputBox(Prompt:="Enter first column: ", Title:="Enter data", Default:="A")
Dim secondCol As String
secondCol = InputBox(Prompt:="Enter second column: ", Title:="Enter data", Default:="B")
Dim resultCol As String
resultCol = InputBox(Prompt:="Enter results column: ", Title:="Enter data", Default:="C")
LastRow = Cells(Rows.Count, firstCol).End(xlUp).Row
Dim comma As String
comma = ","","","
For Each rng In Range(firstCol & "1:" & firstCol & LastRow)
Range(resultCol & rng.Row).Formula = "=CONCATENATE(" & firstCol & rng.Row & comma & secondCol & rng.Row & ")"
Next
End Sub