2015-04-03 64 views
0

我想知道在计算行时是否可以使用if else语句。所以我将数据从一个工作簿导入到另一个工作簿。如果U列中的单元格为空,它将不会导入它,而是转到下一个单元格。
但是,导入行的总数仍在计算跳过的行数,因为它从选定区域开始计数。
那么是否有可能排除列U中可能有空单元格的那些?使用if语句来计算行

复活节快乐


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

“valg.Cells”和“wkbCurrent.ActiveSheet.Range(”U“:U”)“中的行之间的关系有点难以理解,但Application.CountA(wkbCurrent.ActiveSheet.Range (“U”:U“))'或'Application.CountIf(wkbCurrent.ActiveSheet.Range(”U“:U”),“<>”)'返回U:U中非空白单元格的计数那实际上是被复制过来的? – Jeeped 2015-04-03 12:51:36

回答

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." 

正如我在读你的代码,然后你可以摆脱这个循环之后的一切。