2015-08-14 52 views
-2

我在循環FOR中有錯誤。我不明白爲什麼。我的目的是激活「自動計算」選項,然後刪除所有舊行並最終添加新行。循環中的錯誤

Sub refresh() 
    ' 
    ' refresh Macro 
    ' 
    ' Touche de raccourci du clavier: Ctrl+y 
    ' 
    Dim LastRow As Integer, i As Integer 

     Application.Calculation = xlAutomatic 
     Range("A6:AP1000").Select 
     Application.DisplayAlerts = False 
     Selection.Delete 
     Application.DisplayAlerts = True 
     Range("A6:AP1000").Select 
     Selection.ClearContents 

     Sheets("PTR").Range(「A」 & Rows.Count).Select 

    For i = 2 To Sheets("PTR").Range(「A」 & Rows.Count).End(xlUp).Row 

    If Cells(i, 1) = "X" Then 
    Range(Cells(i, 1), Cells(i, 20)).Select 
    Selection.Copy 
     Sheets("Analyse de risque").Range("B" & Rows.Count).PasteSpecial xlPasteValuesAndNumberFormats 
     Application.CutCopyMode = False 
    End If 
    Next i 
    End Sub 
+0

這個錯誤是......?哦,並且不要使用微軟的詞來編輯你的代碼。 '「A」'不是有效的引號... –

回答

0
Dim LastRow1 As Long 
Dim LastRow2 As Long 
Dim i As Integer 
Dim WS1 As Worksheet 
Dim WS2 As Worksheet 

Set WS1 = Worksheets("PTR") 
LastRow1 = WS1.Cells(1048576, 1).End(xlUp).Row ' COLUMN 1 ????????? 
Set WS2 = Worksheets("Analyse de risque") 
LastRow2 = WS2.Cells(1048576, 1).End(xlUp).Row ' COLUMN 1 ????? 

For i = 2 To LastRow1 
    If Cells(i, 1) = "X" Then 
     Range(Cells(i, 1), Cells(i, 20)).Copy 
     WS2.Cells(LastRow2, 1).PasteSpecial xlPasteValuesAndNumberFormats 
     Application.CutCopyMode = False 
     LastRow2 = LastRow2 + 1 
    End If 
Next i 
+0

感謝您發佈這個問題的答案!在堆棧溢出中僅使用代碼解決方案(http://meta.stackexchange.com/a/148274),因爲沒有上下文的代碼轉儲不能解釋解決方案如何或爲什麼會起作用,這使得無法原始海報(或任何未來的讀者)瞭解其背後的邏輯。請編輯你的問題,幷包括你的代碼的解釋,以便其他人可以從你的答案中受益。 – AHiggins