所以我在Excel中有以下表格
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
有关如何执行此操作的任何建议?
答案 0 :(得分:2)
你没有说过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
答案 1 :(得分:2)
尝试:
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
答案 2 :(得分:0)
你可以自己录制一个宏来做这样的事情:
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行。
答案 3 :(得分:0)
以下是手动完成并录制宏的结果...... 比手工编码要多得多,但更容易做到imo。
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
答案 4 :(得分:0)
您可以添加列并将列连接在一起,然后删除其他列。
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)