2015-08-14 51 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