【问题标题】:Using VBA to Move Certain Data to the Right使用 VBA 将某些数据向右移动
【发布时间】:2012-06-07 22:29:37
【问题描述】:

在列表中,我想将非“P”项目移到同一张纸上的右侧。然后我需要将“P”项向下复制以匹配右侧的项数。请参阅示例进行说明。

Right Click on Link and Save File for Sample

感谢您的帮助。

【问题讨论】:

    标签: excel list vba move


    【解决方案1】:
    Sub MoveP()
    ' Move non P rows to right,
    ' starting with the row of the P above it,
    ' and add P info on each row
    
    ' If you want to backup before starting uncomment next two rows of code
    '    Sheets("Raw Data").Select
    '    Sheets("Raw Data").Copy Before:=Sheets(1)
    
    Dim maxRows as Integer
    Dim emptyRowsToStopAt
    Dim emptyRows
    Dim cell1Text As String
    Dim currentRightRow As Integer
    Dim currentPRow As Integer
    
    maxRows = 150 ' change this if you want to process more (or less)
    emptyRowsToStopAt = 5
    currentRightRow = 0
    currentPRow = 0
    
    For i = 2 To maxRows
    
        If emptyRows > emptyRowsToStopAt Then 
           Exit For
        End If
    
        cell1Text = Cells(i, 1)
        Dim startsWithP As Boolean
        startsWithP = InStr(1, cell1Text, "P")
    
        If startsWithP Then
            currentPRow = i
            currentRightRow = currentPRow ' we start with the same line
    
            emptyRows = 0
    
        ElseIf IsEmpty(Cells(i, 1)) Or Cells(i, 1) = "" Then
        '    ' its an empty cell
            emptyRows = emptyRows + 1
    
        Else ' its a non P entry
            emptyRows = 0
            'copy info from left to correct line on right
            Range(Cells(i, 1), Cells(i, 11)).Select
            Selection.Cut
            Range(Cells(currentRightRow, 13), Cells(currentRightRow, 13)).Select
            ActiveSheet.Paste
    
            ' duplicate PRow to left (when non-p was not copied to PRow)
            ' -- see note below: only 3 cells duplicated
            If currentPRow <> currentRightRow Then ' not on the original P Row
                ' copy p heading
                Range(Cells(currentPRow, 1), Cells(currentPRow, 3)).Select 
                ' only first 3 cells copied
                ' change '3' to '11' if you want all
                Selection.Copy
    
                ' past p heading on current row
                Range(Cells(i, 1), Cells(i, 1)).Select
                ActiveSheet.Paste
            End If ' non p row copied to originally non p row
    
            ' and mark current row as written
            currentRightRow = currentRightRow + 1
        End If
    Next
    
    Call CleanupPtable
    
    End Sub
    
    Sub CleanupPtable()
    '
    ' Clean up the P table Macro
    ' Adapted from macro recorded 08/06/2012 by pashute
    '
        Range(Cells(1, 1), Cells(1, 11)).Select
        Selection.Copy
        Range("M1").Select
        ActiveSheet.Paste
    
        ' yellow column
        Columns("L:L").Select
        Selection.Interior.ColorIndex = 36
    
        ' yellow column lines
        Columns("L:L").Select
        ' Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        ' Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        '   With Selection.Borders(xlInsideVertical)
        '    .LineStyle = xlContinuous
        '    .Weight = xlThin
        '    .ColorIndex = xlAutomatic
        ' End With
        With Selection.Borders(xlInsideHorizontal)
           .LineStyle = xlContinuous
           .Weight = xlThin
           .ColorIndex = xlAutomatic
        End With
    
        ' yellow column width
        Selection.ColumnWidth = 2.43
    
        ' Automatic filters to all fields
        Rows("1:1").Select
        Selection.AutoFilter
    
        ' autofit
        Cells.Select
        Cells.EntireColumn.AutoFit
    
    End Sub
    

    【讨论】:

    • 两个 P 项目没有正确排列,但其他一切正常。我会玩它,看看我是否可以修复或学习 VBA。谢谢。
    • 我为此工作了一个小时。所以......什么不起作用?代码注释很好,所以你应该能够完全理解它。我可以调试它。哪些 P 行没有对齐?
    • 如何在 reafidy 的回答中添加评论?在 Chrome 和 IE 中,只有我自己的答案没有添加评论按钮。无论如何,他的代码(除了不添加标题和黄色)看起来更优雅,但它是如何工作的?我只看到他添加到数组中。左右数组是怎么被清空的,数组是怎么写到Excel中正确位置的?
    • 您已经非常接近了,有些空格会影响它。请参阅随附的屏幕截图。 goobee.info/stuff/screenshot2.jpg
    • @pashute,在其他会员解决方案上发布 cmets 需要 50 声望。
    【解决方案2】:

    试试这个:

    Sub HTH()
        Dim vArray As Variant
        Dim rCell As Range
    
        Application.ScreenUpdating = False
    
        For Each rCell In Worksheets("Raw Data").UsedRange.Resize(, 1)
            With rCell
                If UCase(Left(.Value, 1)) = "P" Then
                    vArray = .Resize(, 11).Value
                ElseIf IsNumeric(.Value) And Not IsEmpty(.Value) Then
                    .Offset(-1, 12).Resize(, 11).Value = .Resize(, 11).Value
                    If IsNumeric(.Offset(1).Value) And Not IsEmpty(.Offset(1).Value) Then
                        .Resize(, 11).Value = vArray
                    Else
                        .Resize(, 11).Value = ""
                    End If
                End If
            End With
        Next      
    
        Application.ScreenUpdating = True
    
    End Sub
    

    我认为您可以手动复制标题,但如果您需要自动复制,请添加:

    With Worksheets("Raw Data")
        .Cells(1, "M").Resize(, 11).Value = .Cells(1, 1).Resize(, 11).Value
    End With
    

    如果您需要中间的黄色突出显示,请添加:

     With Columns("L:L").Interior
        .Pattern = xlSolid
        .Color = 65535
     End With
    

    【讨论】:

    • 它不会将某些生日复制到右侧。我将通过 VBA 进行挑选,看看是否可以修复或从中学习。谢谢。
    • 我明白你的意思,我没有注意到这一点。这将是一个简单的改变,但我无法遵循这种模式。在您的示例中,对于名字“玛丽”,她的生日似乎消失了??
    • 糟糕,我说得太早了。一些向右移动的 DOB 在错误的行上。见单元格 S1、S4、S8、S28 和 S31。谢谢
    • 对不起,我在样本上弄错了,“P”DOB 不应该被复制过来。见单元格 S1、S4、S8、S28 和 S31。谢谢
    • 你能上传一个更好的样本吗,它有点不清楚,或者我对你现在想要什么感到困惑。我相信我们可以为您整理好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多