2012-02-15 43 views
2

我在這裏有一個宏的代碼,它執行一些數據選擇,並從一個工作表放置到另一個工作表。現在這個代碼完成了我想要的90%的功能。問題是,如果它遇到中有沒有(因此Len(cellVal) = 0cellVal = ""它覆蓋目標單元格源小區。我如何讓excel vba移動到for循環中的下一個單元?

你會看到,在第二elseif的塊有一個註釋。現在它nothign但如果語句評估爲真,即是沒有在源小區,我希望Excel移動到下一個源小區不修改目標單元格中​​的內容。

如何這可能完成的任何想法?

富蘭克林

For i = 7 To endPointFlash 
    Dim cellVal As String 
    cellVal = Cells(i, "G") 

    If (Len(cellVal)) > 0 Then 
     RawData.Activate 
    ElseIf (Len(cellVal)) = 0 Or cellVal = "" Then 
     ' need to tell excel to do nothing and move to the next cell 
    End If 

    For j = 1 To endPointRaw 
     If cellVal = Mid(Cells(j, "A"), 1, Len(cellVal)) Then 
      val2 = Mid(Cells(j, "A"), 1, Len(cellVal)) 
      val3 = Cells(j, "D") 
      Flash.Cells(i, "H").Value = val3 
      Exit For 
     Else: Flash.Cells(i, "H").Value = 0 
     End If 
    Next j 
Flash.Activate 
Next i 

回答

2

非常清楚和明確我會寫這樣的:

Dim cellVal As String 

For i = 7 To endPointFlash 
    cellVal = Flash.Cells(i, "G") 

    If Len(cellVal) = 0 Then 
     ' Do nothing. 
    Else 
     For j = 1 To endPointRaw 
      If cellVal = Mid(RawData.Cells(j, "A"), 1, Len(cellVal)) Then 
       val2 = Mid(RawData.Cells(j, "A"), 1, Len(cellVal)) 
       val3 = RawData.Cells(j, "D") 
       Flash.Cells(i, "H").Value = val3 
       Exit For 
      Else 
       Flash.Cells(i, "H").Value = 0 
      End If 
     Next j 
    End If 

Next i 

這是很清楚的讀者,包含空字符串的單元格不應導致任何操作;在所有其他情況下執行以下操作。

另外我不會來回激活每張紙。這樣做會使你的代碼變慢(並使屏幕閃爍)。相反,我使用正確的工作表名稱識別每個致電Cells的電話。再次,這使得它非常明確,你得到的東西。

注意:cellVal = ""必然意味着Len(cellVal)) = 0,所以沒有必要在你的條件寫兩個。

+0

你似乎更快了幾分鐘:-) – assylias 2012-02-16 09:08:10

3

像這樣:

For i = 7 To endPointFlash 
    Dim cellVal As String 
    cellVal = Cells(i, "G") 

    If (Len(cellVal)) > 0 Then 
     RawData.Activate 
    ElseIf (Len(cellVal)) = 0 Or cellVal = "" Then 
     ' need to tell excel to do nothing and move to the next cell 
    Else 
     For j = 1 To endPointRaw 
      If cellVal = Mid(Cells(j, "A"), 1, Len(cellVal)) Then 
       val2 = Mid(Cells(j, "A"), 1, Len(cellVal)) 
       val3 = Cells(j, "D") 
       Flash.Cells(i, "H").Value = val3 
       Exit For 
      Else: Flash.Cells(i, "H").Value = 0 
      End If 
     Next j 
    End If 

Flash.Activate 
Next i 

或者這樣:

For i = 7 To endPointFlash 
    Dim cellVal As String 
    cellVal = Cells(i, "G") 

    If (Len(cellVal)) > 0 Then 
     RawData.Activate 
    ElseIf (Len(cellVal)) = 0 Or cellVal = "" Then 
     ' need to tell excel to do nothing and move to the next cell 
     Goto NextLoop 
    End If 

    For j = 1 To endPointRaw 
     If cellVal = Mid(Cells(j, "A"), 1, Len(cellVal)) Then 
      val2 = Mid(Cells(j, "A"), 1, Len(cellVal)) 
      val3 = Cells(j, "D") 
      Flash.Cells(i, "H").Value = val3 
      Exit For 
     Else: Flash.Cells(i, "H").Value = 0 
     End If 
    Next j 
NextLoop: 
Flash.Activate 
Next i 
+0

+1推薦使用'中秋節$'字符串函數,而比'Mid' – brettdj 2012-02-16 01:56:06

+1

在你的第一個解決方案中,'Else'之後的代碼永遠不會被執行...... – assylias 2012-02-16 09:02:53

+0

@assylias - 謝謝,你很正確,但是我試圖避免重寫他的代碼,因爲我無法理解他正在嘗試做什麼。主要是他打算與哪些牀單合作。相反,我給了他正確的邏輯,忽略了條件和其他錯誤。我有點懶惰我同意。 – Reafidy 2012-02-16 09:27:01

0

你可以試試這個(不知道你的Flash.Activate應在小區空執行 - 如果這樣移動它的End If下):

Dim cellVal As String 

For i = 7 To endPointFlash 
    cellVal = Cells(i, "G") 

    If cellVal <> "" Then 
     RawData.Activate 

     For j = 1 To endPointRaw 
      If cellVal = Mid(Cells(j, "A"), 1, Len(cellVal)) Then 
       val2 = Mid(Cells(j, "A"), 1, Len(cellVal)) 
       val3 = Cells(j, "D") 
       Flash.Cells(i, "H").Value = val3 
       Exit For 
      Else 
       Flash.Cells(i, "H").Value = 0 
      End If 
     Next j 

     Flash.Activate 

    End If 'Else cell is empty: do nothing 
Next i 
相關問題