【发布时间】: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什么都不是?!