Vba 将某些数据行移动到列中
如果我将所有数据放在一个很长的列中,如下所示:Vba 将某些数据行移动到列中,vba,excel,Vba,Excel,如果我将所有数据放在一个很长的列中,如下所示: A B C 1 2 3 D E F 4 5 6 G H I 7 8 9 可以这样移动数据吗 Column1 Column2 Column3 Column4 Column5 Column6 A B C 1 2 3 D E F 4 5 6 G H
A
B
C
1
2
3
D
E
F
4
5
6
G
H
I
7
8
9
可以这样移动数据吗
Column1 Column2 Column3 Column4 Column5 Column6
A B C 1 2 3
D E F 4 5 6
G H I 7 8 9
我尝试了粘贴特殊+转置,但我有超过10000条记录,所以使用这种方法会花费我太多的时间
我是excel和宏的新手,非常感谢
编辑:
我甚至尝试将所有数据转换为多个列,然后使用此宏选择要将它们全部转换为一列的列:
Sub OneColumn()
' Jason Morin as amended by Doug Glancy
' http://makeashorterlink.com/?M19F26516
''''''''''''''''''''''''''''''''''''''''''
'Macro to copy columns of variable length
'into 1 continuous column in a new sheet
''''''''''''''''''''''''''''''''''''''''''
Dim from_lastcol As Long
Dim from_lastrow As Long
Dim to_lastrow As Long
Dim from_colndx As Long
Dim ws_from As Worksheet, ws_to As Worksheet
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set ws_from = ActiveWorkbook.ActiveSheet
from_lastcol = ws_from.Cells(1, Columns.Count).End(xlToLeft).Column
'Turn error checking off so if no "AllData" trying to delete doesn't generate Error
On Error Resume Next
'so not prompted to confirm delete
Application.DisplayAlerts = False
'Delete if already exists so don't get error
ActiveWorkbook.Worksheets("AllData").Delete
Application.DisplayAlerts = True
'turn error checking back on
On Error GoTo 0
'since you refer to "AllData" throughout
Set ws_to = Worksheets.Add
ws_to.Name = "AllData"
For from_colndx = 1 To from_lastcol
from_lastrow = ws_from.Cells(Rows.Count, from_colndx).End(xlUp).Row
'If you're going to exceed 65536 rows
If from_lastrow + ws_to.Cells(Rows.Count, 1).End(xlUp).Row <= 65536 Then
to_lastrow = ws_to.Cells(Rows.Count, 1).End(xlUp).Row
Else
MsgBox "This time you've gone to far"
Exit Sub
End If
ws_from.Range(ws_from.Cells(1, from_colndx), ws_from.Cells(from_lastrow, _
from_colndx)).Copy ws_to.Cells(to_lastrow + 1, 1)
Next
' this deletes any blank rows
ws_to.Columns(1).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
您可以从以下几行中看到:
Sub TransposeColumn()
Dim rng As Range
Dim ws As Worksheet
Set rng = Worksheets("Input").UsedRange
Set ws = Worksheets("Output")
j = 1
k = 1
For i = 1 To rng.Rows.Count
If rng.Cells(i, 1) = vbNullString Then
j = j + 1
k = 1
Else
''ws.Cells(k, j) = rng.Cells(i, 1)
''EDIT
ws.Cells(j, k) = rng.Cells(i, 1)
k = k + 1
End If
Next
End Sub
您可以从以下几行中看到:
Sub TransposeColumn()
Dim rng As Range
Dim ws As Worksheet
Set rng = Worksheets("Input").UsedRange
Set ws = Worksheets("Output")
j = 1
k = 1
For i = 1 To rng.Rows.Count
If rng.Cells(i, 1) = vbNullString Then
j = j + 1
k = 1
Else
''ws.Cells(k, j) = rng.Cells(i, 1)
''EDIT
ws.Cells(j, k) = rng.Cells(i, 1)
k = k + 1
End If
Next
End Sub
这就是我做同样事情的方式。。。它将在列C中创建新表,该列位于…之上,基于您的示例,即每组数据之间有一个空白单元格:
Sub TransposeGroups()
Dim RNG As Range, Grp As Long, NR As Long
Set RNG = Range("A:A").SpecialCells(xlConstants)
NR = 1
For Grp = 1 To RNG.Areas.Count
RNG.Areas(Grp).Copy
Range("C" & NR).PasteSpecial xlPasteAll, Transpose:=True
NR = NR + 1
Next Grp
End Sub
这适用于任何长度的数据和数据中最多8500个“组”
这也使用了AREAS方法,但是通过使用子组,它克服了组的限制,因此它应该适用于任何大小的数据集
Sub TransposeGroups2()
'Uses the AREAS method and will work on any size data set
'overcomes the limitation of areas by working in subgroups
Dim RNG As Range, rngSTART As Range, rngEND As Range
Dim LR As Long, NR As Long, SubGrp As Long, Itm As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
NR = 1
SubGrp = 1
Set rngEND = Range("A" & SubGrp * 10000).End(xlUp)
Set RNG = Range("A1", rngEND).SpecialCells(xlConstants)
Do
For Itm = 1 To RNG.Areas.Count
RNG.Areas(Itm).Copy
Range("C" & NR).PasteSpecial xlPasteAll, Transpose:=True
NR = NR + 1
Next Itm
If rngEND.Row = LR Then Exit Do
Set rngSTART = rngEND.Offset(1)
SubGrp = SubGrp + 1
Set rngEND = Range("A" & (SubGrp * 10000)).End(xlUp)
Set RNG = Range(rngSTART, rngEND).SpecialCells(xlConstants)
Loop
End Sub
这就是我做同样事情的方式。。。它将在列C中创建新表,该列位于…之上,基于您的示例,即每组数据之间有一个空白单元格:
Sub TransposeGroups()
Dim RNG As Range, Grp As Long, NR As Long
Set RNG = Range("A:A").SpecialCells(xlConstants)
NR = 1
For Grp = 1 To RNG.Areas.Count
RNG.Areas(Grp).Copy
Range("C" & NR).PasteSpecial xlPasteAll, Transpose:=True
NR = NR + 1
Next Grp
End Sub
这适用于任何长度的数据和数据中最多8500个“组”
这也使用了AREAS方法,但是通过使用子组,它克服了组的限制,因此它应该适用于任何大小的数据集
Sub TransposeGroups2()
'Uses the AREAS method and will work on any size data set
'overcomes the limitation of areas by working in subgroups
Dim RNG As Range, rngSTART As Range, rngEND As Range
Dim LR As Long, NR As Long, SubGrp As Long, Itm As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
NR = 1
SubGrp = 1
Set rngEND = Range("A" & SubGrp * 10000).End(xlUp)
Set RNG = Range("A1", rngEND).SpecialCells(xlConstants)
Do
For Itm = 1 To RNG.Areas.Count
RNG.Areas(Itm).Copy
Range("C" & NR).PasteSpecial xlPasteAll, Transpose:=True
NR = NR + 1
Next Itm
If rngEND.Row = LR Then Exit Do
Set rngSTART = rngEND.Offset(1)
SubGrp = SubGrp + 1
Set rngEND = Range("A" & (SubGrp * 10000)).End(xlUp)
Set RNG = Range(rngSTART, rngEND).SpecialCells(xlConstants)
Loop
End Sub
重要的是要表明你已经尝试过了。录制一个宏,该宏执行一些您想要执行的操作,并查看是否可以对其进行修改以执行所有操作。把你被卡住的部分贴出来。为了更好地理解,在A列的每一个中断处,你想让它下面的数据在下一个断点上方水平地转换到下一行的B-*列吗?@Raystafarian谢谢你的回答。是的,就是这样。@Remou谢谢你的回答。我编辑了我的问题。我没有发布它,因为我甚至不知道我是否使用了正确的方法来获得结果,这是我第一次使用宏。重要的是显示您已经尝试过。录制一个宏,该宏执行一些您想要执行的操作,并查看是否可以对其进行修改以执行所有操作。把你被卡住的部分贴出来。为了更好地理解,在A列的每一个中断处,你想让它下面的数据在下一个断点上方水平地转换到下一行的B-*列吗?@Raystafarian谢谢你的回答。是的,就是这样。@Remou谢谢你的回答。我编辑了我的问题。我没有发布它,因为我甚至不知道我是否使用了正确的方法来获得结果,这是我第一次使用宏。非常感谢您的代码。我尝试过,但收到一条错误消息:运行时错误“9”:下标超出范围。这不是经过修饰的代码,这是一个经过测试的示例。下标超出范围表示输入不正确。您是否使用自己的工作表名称来代替输入和输出?您是否检查过rng的地址是否适合您?(
debug.print rng.address
)我明白了,对不起,我对宏一无所知。我将编辑它,然后再试一次。我现在尝试了,没有错误,非常感谢你的代码。现在输出略有不同,我在我的问题中发布了它。有可能使它水平重新排列吗?好的,我误解了你的输出,请稍候。非常感谢您的代码。我尝试了它,但收到一条错误消息:运行时错误“9”:下标超出范围。这不是经过修饰的代码,它是一个经过测试的示例。下标超出范围表示输入不正确。您是否使用自己的工作表名称来代替输入和输出?您是否检查过rng的地址是否适合您?(debug.print rng.address
)我明白了,对不起,我对宏一无所知。我将编辑它,然后再试一次。我现在尝试了,没有错误,非常感谢你的代码。现在输出略有不同,我在我的问题中发布了它。有可能使它水平重新排列吗?好的,我误解了你的输出,请稍等。真的感谢你的回答,我刚刚尝试过,它工作正常。非常感谢。非常感谢你的回答,我刚试过,效果很好。非常感谢你。