根据特定列上的单元格值将行数添加到另一个工作表中

问题描述 投票:0回答:1

我是VBA的新手,但是在这种情况下,手动执行此操作非常繁琐,因此我必须学习。

我需要一个脚本,该脚本可以在列上找到某些文本值,然后将一定数量的行以及所有行值复制到另一个工作表中。第一行为完整行值,接下来的行为前5行。搜索的文本值例如是“ DOL-1”或“ VFD”。

[经过大量的研究和反复试验后,我设法将可完成此工作的脚本拼接在一起,但是它显然写得不好并且没有优化。我曾尝试搜索类似的问题并尝试了答案,但我无能为力,无法执行此脚本。

我想知道是否有更好和/或更快速的方法来实现与此脚本相同的功能?

Sub Add_Rows()

Dim wbC As Workbook
Dim wbP As Workbook
Dim wsC As Worksheet
Dim wsP As Worksheet
Dim cell As Range
Dim r As Integer
Dim dataTable As Range

r = 8
'rownumber

Set wbP = Application.Workbooks.Open("C:\Projects\Feed_list.xlsx")
Set wsP = wbP.Worksheets("Feed_list")
' set paste destination (these variables aren't really even used because I couldn't get them to work)

Set wbC = Application.Workbooks.Open("C:\Projects\Generated_list.xlsm")
Set wsC = wbC.Worksheets("GEN")
' set copy location (these variables aren't really even used because I couldn't get them to work)

Windows("Generated_list.xlsm").Activate

Application.ScreenUpdating = False

For Each cell In Range("AB2:AB5000")

    If cell.Value = "DOL-1" Then

        Debug.Print cell.Address
        Windows("Generated_list.xlsm").Activate
        Range(cell, cell.Offset(, -25)).Copy 
        Windows("Feed_list.xlsx").Activate
        Sheets("Feed_list").Select

        'Debug.Print r

        Rows(r).Select
        Selection.EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        r = r + 1
        Rows(r).Select
        Selection.EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        r = r + 1
        Rows(r).Select
        Selection.EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        r = r + 1
        Rows(r).Select
        Selection.EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        r = r + 1

        Windows("Generated_list.xlsm").Activate
        Range(cell.Offset(, -21), cell.Offset(, -25)).Copy
        Windows("Feed_list.xlsx").Activate
        Sheets("Feed_list").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

        Windows("Generated_list.xlsm").Activate
        Range(cell.Offset(, -21), cell.Offset(, -25)).Copy
        Windows("Feed_list.xlsx").Activate
        Sheets("Feed_list").Select
        Selection.Offset(-1).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

        Windows("Generated_list.xlsm").Activate
        Range(cell.Offset(, -21), cell.Offset(, -25)).Copy
        Windows("Feed_list.xlsx").Activate
        Sheets("Feed_list").Select
        'Rows(r).Select
        Selection.Offset(-1).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

    End If

    If cell.Value = "VFD" Then

        Debug.Print cell.Address
        Windows("Generated_list.xlsm").Activate
        Range(cell, cell.Offset(, -25)).Copy
        Windows("Feed_list.xlsx").Activate
        Sheets("Feed_list").Select

        'Debug.Print r

        Rows(r).Select
        Selection.EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        r = r + 1
        Rows(r).Select
        Selection.EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        r = r + 1

        Windows("Generated_list.xlsm").Activate
        Range(cell.Offset(, -21), cell.Offset(, -25)).Copy
        Windows("Feed_list.xlsx").Activate
        Sheets("Feed_list").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

        Windows("Generated_list.xlsm").Activate
        Range(cell.Offset(, -21), cell.Offset(, -25)).Copy
        Windows("Feed_list.xlsx").Activate
        Sheets("Feed_list").Select
        Selection.Offset(-1).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

    End If

'these if functions are repeated about 20 times with different text values and number of rows copied

Next

Application.ScreenUpdating = True
Windows("Feed_list.xlsx").Activate
Sheets("Feed_list").Select

End Sub

我制作了小幅示例图片。 Generated_list looks like this.(公告栏AB)

提要列表looks like this at first.

并且在运行脚本之后,应该look like this.

excel vba copy rows
1个回答
-1
投票
Sub Main()
    Call Add_Rows(8)
End Sub

Sub Add_Rows(whereToAdd As Long)
    Dim wb_Feed As Workbook, wb_Gen As Workbook
    Dim ws_Feed As Worksheet, ws_Gen As Worksheet
    Dim lastRow As Long, lastCol As Long, i As Long, idxType As Long

    Set wb_Feed = Workbooks.Open("C:\Projects\Feed_list.xlsx")
    Set wb_Gen = Workbooks.Open("C:\Projects\Generated_list.xlsm")

    Set ws_Feed = wb_Feed.Worksheets("Feed_List")
    Set ws_Gen = wb_Gen.Worksheets("Generated_List")


    ' Find the last row and last column of the data in Generated List
    ' Assume that the first column does not contain any blank data in middle
    lastRow = ws_Gen.Cells(ws_Gen.Rows.Count, "A").End(xlUp).Row
    lastCol = ws_Gen.Cells(1, ws_Gen.Columns.Count).End(xlToLeft).Column    ' First row is header

    ' Column AB is the last column
    idxType = lastCol

    With ws_Gen
        For i = 2 To lastRow
            If .Cells(i, idxType).Value = "VFD" Then
                ' Insert a row to Feed List
                ws_Feed.Range("A" & whereToAdd).EntireRow.Insert

                ' Copy entire row
                .Range(.Cells(i, 1), .Cells(i, lastCol)).Copy
                ' Paste
                ws_Feed.Range("A" & whereToAdd).PasteSpecial xlPasteAll
                Application.CutCopyMode = False

                ' Since VFD, insert extra 1 line according to your screenshot
                whereToAdd = whereToAdd + 1
                ws_Feed.Range("A" & whereToAdd).EntireRow.Insert

                ' Copy first 5 columns
                .Range(.Cells(i, 1), .Cells(i, 5)).Copy
                ' Paste
                ws_Feed.Range("A" & whereToAdd).PasteSpecial xlPasteAll
                Application.CutCopyMode = False

                ' Update where to add next
                whereToAdd = whereToAdd + 1

            ElseIf .Cells(i, idxType).Value = "DOL-1" Then
                ' Insert a row to Feed List
                ws_Feed.Range("A" & whereToAdd).EntireRow.Insert

                ' Copy entire row
                .Range(.Cells(i, 1), .Cells(i, lastCol)).Copy
                ' Paste
                ws_Feed.Range("A" & whereToAdd).PasteSpecial xlPasteAll
                Application.CutCopyMode = False

                ' Since DOL-1 insert extra 3 lines according to your screenshot
                whereToAdd = whereToAdd + 1
                ws_Feed.Range("A" & whereToAdd).EntireRow.Insert
                ws_Feed.Range("A" & whereToAdd).EntireRow.Insert
                ws_Feed.Range("A" & whereToAdd).EntireRow.Insert

                ' Copy first 5 columns
                .Range(.Cells(i, 1), .Cells(i, 5)).Copy
                ws_Feed.Range("A" & whereToAdd).PasteSpecial xlPasteAll
                ws_Feed.Range("A" & whereToAdd + 1).PasteSpecial xlPasteAll
                ws_Feed.Range("A" & whereToAdd + 2).PasteSpecial xlPasteAll
                Application.CutCopyMode = False

                ' Update where to add next
                whereToAdd = whereToAdd + 3
            End If
        Next i
    End With

    ' You should close the workbook after you finish your job
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.