简体   繁体   English

根据单元格值将特定行复制到另一个工作簿

[英]Copy specific row to another workbook based on a cell value

I want to copy row(A:E), row(F:AH), and row(AL)from the active workbook to row(A:E), row(G:AI), row(AJ) of another workbook.我想将行(A:E)、行(F:AH)和行(AL)从活动工作簿复制到另一个工作簿的行(A:E)、行(G:AI)、行(AJ)。 Here's the code that I'm working on.这是我正在处理的代码。 I saw it here and just edited it.我在这里看到它并刚刚编辑它。

Dim wb1 As Workbook, wb2 As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim copyFrom As Range
Dim lRow As Long
Dim strSearch As String
Dim ret

ret = Application.GetOpenFilename(FileFilter:="Excel Workbooks (*.xls, .xlsx*),*.xls, .xlsx*", _
                                  Title:="Select data file for Monitoring Log")
Set wb1 = ThisWorkbook
Set ws1 = wb1.Worksheets("Macro Template")

strSearch = "Newly Distributed"

With ws1

    .AutoFilterMode = False

    lRow = .Range("AL" & .Rows.Count).End(xlUp).Row

    With .Range("AL7:AL" & lRow)
        .AutoFilter Field:=1, Criteria1:="=*" & strSearch & "*"
        Set copyFrom = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow
    End With

    '~~> Remove any filters
    .AutoFilterMode = False
End With

'~~> Destination File
Set wb2 = Application.Workbooks.Open(ret)
Set ws2 = wb2.Worksheets("Source")

With ws2
    If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
        lRow = .Cells.Find(What:="*", _
                      After:=.Range("A1"), _
                      Lookat:=xlPart, _
                      LookIn:=xlFormulas, _
                      SearchOrder:=xlByRows, _
                      SearchDirection:=xlPrevious, _
                      MatchCase:=False).Row + 1
    Else
        lRow = 1
    End If

    copyFrom.Copy .Rows(lRow)
End With

wb2.Save
wb2.Close

This code copies the entire row.此代码复制整行。 How can I revise it to copy specific rows.如何修改它以复制特定行。

From the source code, i just could see you copy the data from the column AL to another worksheet.从源代码中,我只能看到您将 AL 列中的数据复制到另一个工作表。
I modified your code and it successfully copy to the another worksheet.我修改了您的代码并成功复制到另一个工作表。 The copy function could be written in 1 line instead of multiple line.复制函数可以写在 1 行而不是多行。

Option Explicit
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lRow As Long
Dim lRow2 As Long
Dim strSearch As String

Sub Test()

Dim ret

ret = Application.GetOpenFilename(FileFilter:="Excel Workbooks (*.xls, .xlsx*),*.xls, .xlsx*", _
                                  Title:="Select data file for Monitoring Log")
Set wb1 = ThisWorkbook
Set ws1 = wb1.Worksheets("Macro Template")

strSearch = "Newly Distributed"

With ws1

    .AutoFilterMode = False

    lRow = .Cells(Rows.Count, "AL").End(xlUp).Row
    'lRow = .Range("AL" & .Rows.Count).End(xlUp).Row

    With .Range("AL7:AL" & lRow)
        .AutoFilter Field:=1, Criteria1:="=*" & strSearch & "*"
    End With

End With

'~~> Destination File
Set wb2 = Application.Workbooks.Open(ret)
Set ws2 = wb2.Worksheets("Source")

With ws2
    If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
        lRow2 = .Cells(Rows.Count, "A").End(xlUp).Row
    Else
        lRow2 = 1
    End If

    'copyFrom.Copy .Rows(lRow)
    ws1.Range("AL8:AL" & lRow).SpecialCells(xlCellTypeVisible).Copy Destination:=ws2.Range("A" & lRow2)
End With

    '~~> Remove any filters
    ws1.AutoFilterMode = False

wb2.Save
wb2.Close
End Sub

Replace代替

copyFrom.Copy .Rows(lRow)

with

copyFrom.Columns("A:E").Copy .Cells(lRow, "A")
copyFrom.Columns("F:AH").Copy .Cells(lRow, "G")
copyFrom.Columns("AL").Copy .Cells(lRow, "AJ")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据单元格值将特定数据(不是整行!)复制到另一张表 - Copy Specific data (Not the entire row!) to another sheet based on cell value 根据单元格值自动过滤(或循环)并复制到另一个工作簿 - Autofilter (or loop) and copy to another workbook based on Cell Value 根据单元格中的值将行复制到另一张工作表 - Copy row to another sheet based on value in a cell VBA:根据当前工作簿的单元格值从另一个工作簿复制数据 - VBA: Copy data from another workbook, based on cell value of current workbook 更新行中的特定单元格时自动将行复制到另一个工作簿/工作表 - Automatically copy row to another workbook/worksheet when specific cell in the row is updated 将多个工作簿复制到另一个工作簿中的特定行 - Copy multiple workbooks to a specific row in another workbook 根据名称有条件地将行复制到另一个工作簿 - Conditionally Copy a Row to Another Workbook based on the name 错误424-将单元格值复制到另一个工作簿 - Error 424 - copy cell value to another workbook 根据单元格值,确定当前行并将3个工作表中的同一行复制到新工作簿中(循环)-VBA - Based on Cell Value, identify current row and copy the same row in 3 worksheets to a new workbook (in a loop) - VBA Excel宏-如何根据特定的单元格值复制/拆分行 - Excel Macro - How to copy/split row based on specific cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM