【问题标题】:Copy from different workbook based on 3 criteria根据 3 个标准从不同的工作簿复制
【发布时间】:2017-12-11 08:03:38
【问题描述】:

我想将数据从工作簿15B2[...]" (sheet DATA) 复制到工作簿。我从(sheet getDATA) 开始宏。如果 N 、 CI 列中的单元格为空白且 DA 列的值为 3-Incompletion em> 在里面。

不知何故,宏在第二个 if 语句之后停止并直接转到 End if 而不复制任何内容:

If InStr(.Range("DA" & LastRow7).Value2, "3-Incompletion") > 0 
And Trim(.Range("N" & LastRow7).Value2) = "" 
And Trim(.Range("CI" & LastRow7).Value2) = "" Then

我不知道这个函数到底是做什么的。它是否查看每一行并计算符合条件的行?

完整代码如下:

Sub insertINCOMPLETION()

Dim dataWB As Workbook
Dim reportWB As Workbook
Dim workB As Workbook
Dim incomplRNG As Range
Dim LastRow6 As Long
Dim LastRow7 As Long

For Each workB In Application.Workbooks
    If Left(workB.Name, 4) = "15B2" Then
        Set dataWB = workB
        Exit For
    End If
Next

If Not dataWB Is Nothing Then
    Set reportWB = ThisWorkbook

    With reportWB.Sheets("getDATA")
        LastRow6 = .Cells(.Rows.Count, "B").End(xlUp).Offset(1).Row
    End With

    With dataWB.Sheets("Data")
        LastRow7 = .Cells(.Rows.Count, "F").End(xlUp).Row

        If InStr(.Range("DA" & LastRow7).Value2, "3-Incompletion") > 0 
        And Trim(.Range("N" & LastRow7).Value2) = "" 
        And Trim(.Range("CI" & LastRow7).Value2) = "" Then
            Set incomplRNG = Application.Union(.Range("F8:F" & _ 
            LastRow7),.Range("H8:H" & LastRow7), .Range("DA8:DA" & LastRow7))
            incomplRNG.Copy
            reportWB.Sheets("getDATA").Range("B" & LastRow6).PasteSpecial xlPasteValues
        End If
    End With
End If

End Sub

我需要帮助来解决这个问题,因为我不太擅长编程 VBA。

【问题讨论】:

  • macro stop 是什么意思,是错误(哪个)还是 Excel 崩溃了,还是什么?
  • 宏直接进入“End if”并跳过复制部分。宏正在查找的 Excel 文件已打开,并且工作表“getDATA”存在。
  • If statement 中的条件不匹配。此外,您只检查最后一行数据,如果最后一行符合条件,则尝试复制所有数据。是你需要的吗?
  • 此外,您编写的I don't know exactly what this function does....If...Then、Trim 和Instr 函数和语句是VBA 的基础。如果您不理解它们,请在 Google 上进行定义和使用:)
  • 并学会正确缩进你的代码,因为可以很容易地看到 End If 所涉及的 If。大概dataWB 什么都不是?!

标签: vba excel


【解决方案1】:

尽我所能从您的问题中看出您的意图,您的代码和上面的 cmets 下面的过程应该可以满足您的需求。它未经测试,但它可能包含的任何错误都应该是您可以轻松修复的小错误(或在此处向我指出)。

第一个过程在检查数据块的最后一行后复制数据块。 Version_2 检查每一行并仅复制符合条件的行。

Option Explicit

Sub insertINCOMPLETION()

    Dim DataWb As Workbook
    Dim ReportWB As Workbook
    Dim LastReportRow As Long
    Dim LastDataRow As Long

    For Each DataWb In Application.Workbooks
        If InStr(1, DataWb.Name, "15B2", vbTextCompare) = 1 Then Exit For
    Next

    If Not DataWb Is Nothing Then
        Set ReportWB = ThisWorkbook
        With ReportWB.Sheets("getDATA")
            LastReportRow = .Cells(.Rows.Count, "B").End(xlUp).Row + 1
        End With

        With DataWb.Sheets("Data")
            LastDataRow = .Cells(.Rows.Count, "F").End(xlUp).Row

            If (InStr(1, .Range("DA" & LastDataRow).Value2, "3-Incompletion", vbTextCompare) > 0) And _
                    (Trim(.Range("N" & LastDataRow).Value2) = "") And _
                    (Trim(.Range("CI" & LastDataRow).Value2) = "") Then
                .Range("F8:F" & LastDataRow).Copy ReportWB.Sheets("getDATA").Range("B" & LastReportRow)
                .Range("H8:H" & LastDataRow).Copy ReportWB.Sheets("getDATA").Range("C" & LastReportRow)
                .Range("DA8:DA" & LastDataRow).Copy ReportWB.Sheets("getDATA").Range("D" & LastReportRow)
            End If
        End With
    End If
End Sub

Sub insertINCOMPLETION_Version_2()

    Dim DataWb As Workbook
    Dim ReportWB As Workbook
    Dim LastReportRow As Long
    Dim LastDataRow As Long
    Dim R As Long

    For Each DataWb In Application.Workbooks
        If InStr(1, DataWb.Name, "15B2", vbTextCompare) = 1 Then Exit For
    Next

    If Not DataWb Is Nothing Then
        Set ReportWB = ThisWorkbook
        With ReportWB.Sheets("getDATA")
            LastReportRow = .Cells(.Rows.Count, "B").End(xlUp).Row + 1
        End With

        With DataWb.Sheets("Data")
            LastDataRow = .Cells(.Rows.Count, "F").End(xlUp).Row

            Application.ScreenUpdating = False
            For R = 8 To LastDataRow
                If (InStr(1, .Cells(R, "DA").Value2, "3-Incompletion", vbTextCompare) > 0) And _
                        (Trim(.Cells(R, "N").Value2) = "") And _
                        (Trim(.Cells(R, "CI").Value2) = "") Then
                    ReportWB.Sheets("getDATA").Cells(LastReportRow, "B").Value = .Cells(R, "F").Value
                    ReportWB.Sheets("getDATA").Cells(LastReportRow, "C").Value = .Cells(R, "H").Value
                    ReportWB.Sheets("getDATA").Cells(LastReportRow, "D").Value = .Cells(R, "DA").Value
                    LastReportRow = LastReportRow + 1
                End If
            Next R
            Application.ScreenUpdating = True
        End With
    End If
End Sub

【讨论】:

  • 嘿,谢谢!现在我可以看到一些数据被复制了。不知何故,它不会过滤“3-incompletion”,而只是复制所有内容。我认为它不会检查单元格是否也为空白。
  • 另外,.PasteSpecial xlPasteValues 似乎在这里不起作用。
  • 这与你委托错误代码来传达你想要完成的任务有关。尝试简单的英语。
  • 非常感谢您的帮助。对不起,我的英语不好。不幸的是,所有 3 个标准都匹配非常重要。否则我可以简单地删除空白单元格。但是代码怎么可能会查找“3-incompletion”但复制所有内容。
  • 这可能是因为您告诉我们只在最后一行查找“3-incompletion”和空白单元格。现在我在上面添加了一个变体,它检查从第 8 行开始的每一行 - 只是猜测大声笑:
猜你喜欢
  • 1970-01-01
  • 2011-11-24
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-25
  • 1970-01-01
  • 2020-11-11
  • 1970-01-01
相关资源
最近更新 更多