2015-04-03 102 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." 

正如我在讀你的代碼,然後你可以擺脫這個循環之後的一切。