工作表粘贴和排序数据中的列

发布于 2025-02-10 14:03:33 字数 2231 浏览 3 评论 0原文

我有一个要从一张纸复制到另一张的数据列表。这是我的数据列表。我的目标是将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛夏尉蓝 2025-02-17 14:03:33

我相信您使自己变得太复杂了。这应该有效:

    lastrow = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    Range("A2:A" & lastrow).Select
    Selection.Cut
    Sheets("Sheet2").Select
    Range("B2").Select
    ActiveSheet.Paste
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add2 Key:=Range("B2:B" & lastrow) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet2").Sort
        .SetRange Range("B2:B" & lastrow)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

它需要Sheet1的Columna,将其内容物切割并粘贴到Sheet2的B列中,然后按字母顺序排序它们。

最好的,
C

I believe you are complicating yourself too much. This should work:

    lastrow = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    Range("A2:A" & lastrow).Select
    Selection.Cut
    Sheets("Sheet2").Select
    Range("B2").Select
    ActiveSheet.Paste
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add2 Key:=Range("B2:B" & lastrow) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet2").Sort
        .SetRange Range("B2:B" & lastrow)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

It takes ColumnA of Sheet1, cuts its contents and pastes them in ColumnB of Sheet2, before sorting them in alphabetical order.

Best,
C.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文