工作表粘贴和排序数据中的列
我有一个要从一张纸复制到另一张的数据列表。这是我的数据列表。我的目标是将A列从表2中的第1列移至B列,然后将数据从“ A”“ A”到“ B”到“ C” ...基本上按字母顺序排列。 例如:A列包含(A,A,A,B,CA,B,D,A,B,A),我希望将其排列在第2页的B列中(A,A,A,A,A,A,A ,a,a,b,b,b,c,d)。以下是我的代码。如果可以的话,请帮助。
Sub Button1_Click()
lastrow = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lastrow
'Conditional if statement that copies "a's" in Sheet 1, Column A and pastes in Sheet 2, Column B
If Worksheets("Sheet1").Range("A" & i).Value = "a" Then
Worksheets("Sheet1").Range("A" & i).Copy
Worksheets("Sheet2").Activate
lastRow2 = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
Worksheets("Sheet2").Range("B" & i + 1).Select
ActiveSheet.Paste
'Worksheets("Sheet2").Range("B" & i + 1).Interior.Color = vbCyan
'Worksheets("Sheet2").Range("B" & i).Borders.Color = rbgBlack
End If
'Deletes empty cells and shifts "a's" upward
If Worksheets("Sheet2").Range("B" & i).Value = "" Then
Columns("B:B").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Delete Shift:=xlUp
End If
'Conditional if statement that copies data in Sheet 1, Column B and pastes in Sheet 2, Column C
If Worksheets("Sheet1").Range("A" & i).Value = "a" Then
Worksheets("Sheet1").Range("B" & i).Copy
Worksheets("Sheet2").Activate
lastRow2 = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
Worksheets("Sheet2").Range("C" & i + 1).Select
ActiveSheet.Paste
End If
'Deletes empty cells and shifts data upward in Column C
If Worksheets("Sheet2").Range("A" & i).Value = "" Then
Columns("C:C").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Delete Shift:=xlUp
End If
Next i
End Sub
I have a list of data that I'm copying from one sheet to another. Here is my list of data. My goal is to move Column A from Sheet 1 to Column B in Sheet 2 and sort the data from "a" to "b" to "c"...basically in alphabetical order.
For example: Column A contains (a, a, a, b ,c a, b, d, a, b, a) and I want it to be arranged in Column B of Sheet 2 like this (a, a, a, a, a, a, b, b, b, c, d). Below is my code. Please help if you can.
Sub Button1_Click()
lastrow = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lastrow
'Conditional if statement that copies "a's" in Sheet 1, Column A and pastes in Sheet 2, Column B
If Worksheets("Sheet1").Range("A" & i).Value = "a" Then
Worksheets("Sheet1").Range("A" & i).Copy
Worksheets("Sheet2").Activate
lastRow2 = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
Worksheets("Sheet2").Range("B" & i + 1).Select
ActiveSheet.Paste
'Worksheets("Sheet2").Range("B" & i + 1).Interior.Color = vbCyan
'Worksheets("Sheet2").Range("B" & i).Borders.Color = rbgBlack
End If
'Deletes empty cells and shifts "a's" upward
If Worksheets("Sheet2").Range("B" & i).Value = "" Then
Columns("B:B").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Delete Shift:=xlUp
End If
'Conditional if statement that copies data in Sheet 1, Column B and pastes in Sheet 2, Column C
If Worksheets("Sheet1").Range("A" & i).Value = "a" Then
Worksheets("Sheet1").Range("B" & i).Copy
Worksheets("Sheet2").Activate
lastRow2 = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
Worksheets("Sheet2").Range("C" & i + 1).Select
ActiveSheet.Paste
End If
'Deletes empty cells and shifts data upward in Column C
If Worksheets("Sheet2").Range("A" & i).Value = "" Then
Columns("C:C").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Delete Shift:=xlUp
End If
Next i
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您使自己变得太复杂了。这应该有效:
它需要Sheet1的Columna,将其内容物切割并粘贴到Sheet2的B列中,然后按字母顺序排序它们。
最好的,
C
I believe you are complicating yourself too much. This should work:
It takes ColumnA of Sheet1, cuts its contents and pastes them in ColumnB of Sheet2, before sorting them in alphabetical order.
Best,
C.