2014-09-20 76 views
0

真的希望有人能幫助我。所以我有以下代碼。獨立代碼本身可以很好地工作,但是在執行腳本時,它只能通過第一個條件循環。我希望它做的是每次循環遍歷所有的代碼。我認爲這是我錯過的一件小事,但我似乎無法找到解決方案。sub不會循環通過第二條語句vba

Sub Copypre() 
Dim i As Integer 
Dim n As Integer 

For i = 2 To 10 

'Checks the number of entries in the "Pre" table, to make sure that there are no spaces between the lines 

On Error Resume Next 
    n = Worksheets("Pre").Range("A2:A6000").Cells.SpecialCells(xlCellTypeConstants).Count 
     If n = Null Then 
      n = i 

'Goes through the different sheets to find all "pre" values and paste them in the "Pre" sheet 

    If ThisWorkbook.Sheets("273").Range("A" & i).Value = "Pre" Then 

     Range(Cells(i, 1), Cells(i, 3)).Select 
     Selection.Copy 
     Sheets("Pre").Select 
     Range("A" & n).Select 
     ActiveSheet.Paste 
     Sheets("2736").Select 

       End If 
      End If 
     Next i 
    End Sub 
+1

目前尚不清楚源工作表是否被命名爲* 273 *或* 2736 *。 – Jeeped 2014-09-20 21:15:32

+0

很難弄清楚你的問題是什麼,但是你肯定有一些錯誤的代碼,如果'n = null'。 'n'被定義爲一個整數,不能爲空。你應該改變你的測試爲'If n = 0'。也許這會解決你的問題。 – DeanOC 2014-09-20 21:16:56

+0

你如何從一張紙到另一張紙(如果這是你想要的)? – pnuts 2014-09-20 21:18:54

回答

0

循環可以快速消耗時間長的數據列,我懷疑你的代碼被嚴重編輯。嘗試將這種替代方法複製到目標工作表中。

Sub Copypre() 
    With Sheets("273").Cells(1, 1).CurrentRegion 
     .AutoFilter 
     .Columns(1).AutoFilter field:=1, Criteria1:="=Pre" 
     If CBool(Application.Subtotal(103, .Offset(1, 0))) Then 
      .Offset(1, 0).Resize(, 3).Copy _ 
       Destination:=Sheets("Pre").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) 
     End If 
     .AutoFilter 
    End With 
End Sub 

所有這些都可以在沒有單個變量聲明的情況下完成。

附錄:

至於你原來的問題,整個if "pre"/copy/paste部分嵌套在if n = Null所以如果n = Null是事實,就只能達到內。如果沒有.SpecialCells(xlCellTypeConstants)進行計數,則n將被分配其默認值(例如,)。 不等於因此條件從未滿足。要檢查您的代碼,請添加以下行。

On Error Resume Next 
n = Worksheets("Pre").Range("A2:A6000").Cells.SpecialCells(xlCellTypeConstants).Count 
Debug.Print "n is " & n 

它運行後,打開立即窗口Ctrl+G。如果在Pre!A2:A6000中沒有非公式值,您應該看到n is 0

+0

但是OP似乎想要處理多個表單? – pnuts 2014-09-20 21:34:13

+0

@pnuts =是的,工作表名稱有些不明確之處。我推斷這只不過是一個錯字,但我可能在這一點上是錯誤的。 – Jeeped 2014-09-20 21:47:35

+0

同意可能會或可能不會是一個錯字,但我正在考慮評論「通過不同的工作表...」 – pnuts 2014-09-20 21:49:22

1

您的代碼有幾個問題,但主要問題可能是If n = Null永遠不會成立,因爲整數不能是Null。您可以將其更改爲If n = 0

一對夫婦的事情要考慮:

錯誤處理:隨時回到正常的錯誤,儘快與On Error GoTo 0處理。這樣你就會知道(假設你的工作簿中沒有工作表"2736"),你的代碼試圖選擇一個不存在的工作表。

Range參數:要當心當使用Range(和Cells)參數時,不指定片材。當您在選擇的不同工作表之間切換回來和第四次時,會發生一些變化,您可能會疏忽跟蹤Range從中返回數據的工作表。考慮申報每個工作表,然後複製你的範圍,如:

Dim w273 As Worksheet 
Set w273 = ThisWorkbook.Sheets("273") 
w273.Range(w273.Cells(i, 1), w273.Cells(i, 3)).Copy 
0

感謝分配所有的建議。無效的技巧奏效!我對VBA完全陌生,所以很高興能從專家那裏獲得一些技巧和竅門。我會盡量讓代碼更簡單,因爲Jeeped提到,因爲這不是很優雅。關於牀單,我完全可以理解這種困惑,我也修正了這一點。它的工作原理如下所示:

Sub Copypre() 

    Dim i As Integer 
    Dim n As Integer 

    For i = 2 To 5000 
     ' Checks the number of entries in the "Pre" table, to make sure that there are no spaces between the lines 

     On Error Resume Next 
     n = Worksheets("Pre").Range("A2:A6000").Cells.SpecialCells(xlCellTypeConstants).Count 

     ' Goes through the different sheets to find all pre values and paste them in the "Pre" sheet 
     If ThisWorkbook.Sheets("2736").Range("A" & i).Value = "Pre" Then 

      Sheets("2736").Select 
      Sheets("2736").Range(Cells(i, 1), Cells(i, 3)).Select 
      Selection.Copy 
      Sheets("Pre").Select 
      Range("A" & (n + 2)).Select 
      ActiveSheet.Paste 
      Sheets("2736").Select 

      Sheets("2736").Select 
      Range(Cells(i, 5), Cells(i, 6)).Select 
      Selection.Copy 
      Sheets("Pre").Select 
      Range("E" & (n + 2)).Select 
      ActiveSheet.Paste 
      Sheets("2736").Select 

     End If 
    Next i 
End Sub