我想知道在计算行时是否可以使用if else语句。所以我将数据从一个工作簿导入到另一个工作簿。如果U列中的单元格为空,则不会导入它,而是转到下一个单元格 但是,导入行的总计数仍在计算跳过的行数,因为它是从所选区域开始计数的 那么是否可以排除U列中可能有空单元格的那些?
复活节快乐
For Each c In valg.Cells
If wkbCurrent.ActiveSheet.Range("U" & c.Row) <> "" Then
' Macro runs here
Else
MsgBox wkbCurrent.ActiveSheet.Range("H" & c.Row) & " must have an MDS ID" & vbNewLine & "Skipping this supplier and continue the import", vbCritical, "Error"
'Exit Sub
End If
Next
' Find the number of rows that is copied over
wkbCurrent.ActiveSheet.Activate
areaCount = Selection.Areas.Count
If areaCount <= 1 Then
MsgBox "The selection contains " & Selection.Rows.Count & " suppliers."
' Write it in A10 in CIF LISTEN
wkbNew.Worksheets(1).Range("A10").Value = "COMMENTS: " & Selection.Rows.Count & " Suppliers Added"
Else
i = 1
For Each A In Selection.Areas
'MsgBox "Area " & I & " of the selection contains " & _
a.Rows.count & " rows."
i = i + 1
rwCount = rwCount + A.Rows.Count
Next A
MsgBox "The selection contains " & rwCount & " suppliers."
' Write it in A10 in CIF LISTEN
wkbNew.Worksheets(1).Range("A10").Value = "COMMENTS: " & rwCount & " Suppliers Added"
End If
答案 0 :(得分:0)
不是在源数据中循环一次以复制行,而是再次计算您复制的内容,为什么不在第一次计算它们时计算它们?
我认为'Macro runs here
是您的复制代码的位置,要么传入被调用例程更新的CopiedCount
参数(预设为0),要么将其更改为返回的函数CopiedCount
,或者只是在IF
语句的那一部分增加一个计数器。
这样的事情:
Dim CopiedCount as Integer
For Each c In valg.Cells
If wkbCurrent.ActiveSheet.Range("U" & c.Row) <> "" Then
' Macro runs here
CopiedCount = CopiedCount + 1
Else
MsgBox wkbCurrent.ActiveSheet.Range("H" & c.Row) & " must have an MDS ID" & _
vbNewLine & "Skipping this supplier and continue the import", _
vbCritical, "Error"
'Exit Sub
End If
Next
MsgBox "There were " & CopiedCount & " suppliers transferred."
当我正在阅读你的代码时,你可以在循环之后摆脱一切。