如何连接列
How to concatenate columns
所以我在 Excel
中关注 table
A B C D E F G
4 5 5 4 8 8 9
4 8 7 7 8 7 8
2 1 7 4 7 8 8
upto 2000th row
我需要它的格式如下,但只使用 VBA
A B C
4 55488 9
4 87787 8
2 17478 8
upto 2000th row
关于如何执行此操作的任何建议?
你可以自己录制一个宏来做这样的事情:
Sub Test()
' add new column
Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
' add formula in the newly created column B
Range("B1").Select
ActiveCell.FormulaR1C1 = "=RC[1]&RC[2]&RC[3]&RC[4]&RC[5]"
' repeat the fomula for 2000 rows
Range("B1").Select
Selection.AutoFill Destination:=Range("B1:B2000")
' copy and paste over itself as values so that the formula is converted to resulting data
Range("B1:B2000").Select
Columns("B:B").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
' remove C through G columns
Range("C6").Select
Application.CutCopyMode = False
Columns("C:G").Select
Selection.Delete Shift:=xlToLeft
Range("A1").Select
End Sub
此代码直接由录制宏生成并修改以适应 2000 行。
您没有提及 C 之后的列,所以我将它们保持原样...
sub SO()
Dim c As String
For i = 2 To 2000
c = Cells(i, 2) & Cells(i, 3) & Cells(i, 4) & Cells(i, 5) & Cells(i, 6)
Cells(i, 2) = c
Cells(i, 3) = Cells(i, 7)
Next
end sub
这是手动执行并录制宏的结果...
比手工编码复杂得多,但在我看来更容易。
Sub Macro1()
'
' Macro1 Macro
'
'
Application.Left = 406.75
Application.Top = 289
Columns("G:G").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("G1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[-5],RC[-4],RC[-3],RC[-2],RC[-1])"
Range("G1").Select
Range(Selection, Selection.End(xlDown)).Select
Range("G1:G2000").Select
Selection.FillDown
Selection.End(xlUp).Select
Columns("G:G").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("B:F").Select
Range("F1").Activate
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("D2").Select
Application.Left = 17.5
Application.Top = 277.75
Application.Goto Reference:="Macro1"
End Sub
尝试:
Sub Santosh()
For i = 1 To 2400
Cells(i, "B") = Cells(i, "B") & Cells(i, "C") & Cells(i, "D") & Cells(i, "E") & Cells(i, "F")
Next i
Columns("C:F").Delete
End Sub
您可以添加一列并将这些列连接在一起,然后删除其他列。
A B C D E F G
4 5 5 4 8 8 9
4 8 7 7 8 7 8
2 1 7 4 7 8 8
A B C D E F G H
4 5 5 4 8 8 * 9
4 8 7 7 8 7 * 8
2 1 7 4 7 8 * 8
where * = cstr(B2) & cstr(C2) & cstr(D2) & cstr(E2) & cstr(F2)
所以我在 Excel
中关注 table A B C D E F G
4 5 5 4 8 8 9
4 8 7 7 8 7 8
2 1 7 4 7 8 8
upto 2000th row
我需要它的格式如下,但只使用 VBA
A B C
4 55488 9
4 87787 8
2 17478 8
upto 2000th row
关于如何执行此操作的任何建议?
你可以自己录制一个宏来做这样的事情:
Sub Test()
' add new column
Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
' add formula in the newly created column B
Range("B1").Select
ActiveCell.FormulaR1C1 = "=RC[1]&RC[2]&RC[3]&RC[4]&RC[5]"
' repeat the fomula for 2000 rows
Range("B1").Select
Selection.AutoFill Destination:=Range("B1:B2000")
' copy and paste over itself as values so that the formula is converted to resulting data
Range("B1:B2000").Select
Columns("B:B").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
' remove C through G columns
Range("C6").Select
Application.CutCopyMode = False
Columns("C:G").Select
Selection.Delete Shift:=xlToLeft
Range("A1").Select
End Sub
此代码直接由录制宏生成并修改以适应 2000 行。
您没有提及 C 之后的列,所以我将它们保持原样...
sub SO()
Dim c As String
For i = 2 To 2000
c = Cells(i, 2) & Cells(i, 3) & Cells(i, 4) & Cells(i, 5) & Cells(i, 6)
Cells(i, 2) = c
Cells(i, 3) = Cells(i, 7)
Next
end sub
这是手动执行并录制宏的结果... 比手工编码复杂得多,但在我看来更容易。
Sub Macro1()
'
' Macro1 Macro
'
'
Application.Left = 406.75
Application.Top = 289
Columns("G:G").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("G1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[-5],RC[-4],RC[-3],RC[-2],RC[-1])"
Range("G1").Select
Range(Selection, Selection.End(xlDown)).Select
Range("G1:G2000").Select
Selection.FillDown
Selection.End(xlUp).Select
Columns("G:G").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("B:F").Select
Range("F1").Activate
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("D2").Select
Application.Left = 17.5
Application.Top = 277.75
Application.Goto Reference:="Macro1"
End Sub
尝试:
Sub Santosh()
For i = 1 To 2400
Cells(i, "B") = Cells(i, "B") & Cells(i, "C") & Cells(i, "D") & Cells(i, "E") & Cells(i, "F")
Next i
Columns("C:F").Delete
End Sub
您可以添加一列并将这些列连接在一起,然后删除其他列。
A B C D E F G
4 5 5 4 8 8 9
4 8 7 7 8 7 8
2 1 7 4 7 8 8
A B C D E F G H
4 5 5 4 8 8 * 9
4 8 7 7 8 7 * 8
2 1 7 4 7 8 * 8
where * = cstr(B2) & cstr(C2) & cstr(D2) & cstr(E2) & cstr(F2)