2014-03-24 37 views
0

=我有一個代碼執行一系列計算到文件夾目標中的所有Excel(.xls)文件,並將特定數字放在指定單元格(G2,H2,I2,M2和O1)中。唯一的問題是,當我點擊運行時代碼沒有響應。它不會給出錯誤。我在這裏出錯了嗎?執行公式的代碼不運行的文件夾中的所有excel文件?

Sub Code() 

    Dim file As String 
    Dim wbResults As Workbook 
    Dim i As Long 
    Dim myPath As String 

    Application.ScreenUpdating = False 
    Application.DisplayAlerts = False 

    myPath = "C:\Location\" 

    file = Dir$(myPath & "*.xls*") 

    While (Len(file) > 0) 
     Set wbResults = Workbooks.Open(Filename:=myPath & file, UpdateLinks:=0) 

     With wbResults.Worksheets(Split(file, ".")(0)) 
      i = .Cells(.Rows.Count, 2).End(xlUp).Row 

      With .Range("G2") 
       .Formula = "=0" 
      End With 

      With .Range("G3:G" & i) 
       .Formula = "=SQRT(((E3-E2)^2)+((F3-F2)^2))" 
      End With 

      With .Range("H2") 
       .Formula = "=1" 
      End With 

      With .Range("H3:H" & i) 
       .Formula = "=G3+H2" 
      End With 

      With .Range("I2") 
       .Formula = "=1" 
      End With 

      With .Range("I3") 
       .Formula = "=IF(D3>=SUM($I$2:I2*2.5+$O$1),1,0)" 
      End With 

      With .Range("I4:I" & i) 
       .Formula = "=IF(D3>=SUM($I$3:I3)*2.5+$O$1,1,0)" 
      End With 

      With .Range("J2:K" & i) 
       .Formula = "=IF($I2=1,D2,J1)" 
      End With 

      With .Range("K2:K" & i) 
       .Formula = "=IF($I2=1,H2,K1)" 
      End With 

      With .Range("L2:L" & i) 
       .Formula = "=IF(I2=1,(J2-J1)/(K2-K1),"")" 
      End With 

      With .Range("M2") 
       .Formula = "=0" 
      End With 

      With .Range("M3:M" & i) 
       .Formula = "=IF(L3="",M2,L3)" 
      End With 

      With .Range("O1") 
       .Formula = "=177.5" 
      End With 

     End With 

     wbResults.Close SaveChanges:=True 
     file = Dir 
    Wend 

    Application.DisplayAlerts = True 
    Application.ScreenUpdating = True 
End Sub 

回答

1

爲了調試目的,註釋掉這些行。一旦修復,請取消註釋。

Application.ScreenUpdating = False 
Application.DisplayAlerts = False 

我看到一對夫婦的問題。

  • 如果找不到與.xls擴展名的文件,則不會發生任何事情。
  • 無處不在,你在公式字符串""你必須這樣""""

    With .Range("L2:L" & i) 
        .Formula = "=IF(I2=1,(J2-J1)/(K2-K1),"""")" 
    End With 
    
  • 雙引號你缺少這些公式的IF

    With .Range("J2:K" & i) 
        .Formula = "=IF($I2=1,D2,J1)" 
    End With 
    
    With .Range("K2:K" & i) 
        .Formula = "=IF($I2=1,H2,K1)" 
    End With 
    
+0

謝謝之前等號您!看起來雙引號是問題。發佈後不久,我注意到了等號。 – JC11

相關問題