我的代碼VBA有一個奇怪的問題。我的代碼運行在一些excel工作簿上,但不是全部,爲什麼? VBA
我使用Excel 2010,我寫了一個代碼,除了一些除了一些之外,在多個源代碼上完美工作。
我的代碼對象是從多個工作簿中的同一工作表中複製相同的單元格並將其作爲列粘貼到目標工作簿中。
我的代碼在50個工作簿上運行,沒有任何問題,除了2. 請注意,這2個樣本與其他樣本相同,但當然有不同的值。 如果我將這兩個工作簿加入其他50個工作簿,我有一個錯誤信息'執行錯誤',然後我應該停止這個過程。 黃線站上一個公式: myFile = Dir(myPath & myExtension) Set y = Workbooks.Open(destFullpath)
`With y.Sheets("Feuil1").Range("A" & i + 1)
**.Formula = "='" & "[" & myFile & "]Para RF'!L2" 'date**
.Value = .Value
y.Sheets("Feuil1").Range("A" & i + 1).NumberFormat = "dd/mm/yy;@" ' <-- to specify that it is a date format
End With`
你有一個想法,爲什麼這個問題發生? 我該怎麼辦?有什麼改變與設置等?
注意:我試圖將這兩個文件保存爲沒有宏的excel,所以xlsx並沒有運行。 我試圖取消保護牀單:沒有運行 我打破了他們和其他人之間的聯繫:這也沒有幫助!
它是什麼?
謝謝!
這是整個代碼:
Sub LoopAllExcelFilesInFolderr()
'PURPOSE: To loop through all Excel files in a user specified folder and
perform a set task on them
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim destFullpath As String
Dim myExtension As String
DimFldrPicker As FileDialog
Dim y As Workbook
Dim i As Integer
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Application.DisplayAlerts = False
'Retrieve Target Folder Path From User
myPath = "Z:\VBA\para_macro\"
destFullpath = "Z:\VBA\base-macro.xlsx"
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xls*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Set y = Workbooks.Open(destFullpath)
For i = 1 To y.Sheets("Feuil1").Range("M1")
'Ensure Workbook has opened before moving on to next line of code
DoEvents
With y.Sheets("Feuil1").Range("A" & i + 1)
.Formula = "='" & "[" & myFile & "]Para RF'!L2" 'date devis
.Value = .Value
y.Sheets("Feuil1").Range("A" & i + 1).NumberFormat = "dd/mm/yy;@" ' <-- to specify that it is a date format
End With
With y.Sheets("Feuil1").Range("B" & i + 1)
.Formula = "='" & "[" & myFile & "]Para RF'!E11" 'date d'installation
.Value = .Value
End With
With y.Sheets("Feuil1").Range("c" & i + 1)
.Formula = "='" & "[" & myFile & "]Para RF'!H5" 'type
.Value = .Value
End With
With y.Sheets("Feuil1").Range("D" & i + 1)
.Formula = "='" & "[" & myFile & "]Para RF'!H8" 'montant final
.Value = .Value
.NumberFormat = "0.000"
End With
With y.Sheets("Feuil1").Range("E" & i + 1)
.Formula = "='" & "[" & myFile & "]Para RF'!K8" 'montant tarif
.Value = .Value
.NumberFormat = "0.000"
End With
With y.Sheets("Feuil1").Range("F" & i + 1)
.Formula = "='" & "[" & myFile & "]Para RF'!K10" 'remise
.Value = .Value
.NumberFormat = "0.000"
End With
With y.Sheets("Feuil1")
.Range("G2:G" & .Cells(.Rows.count, "F").End(xlUp).Row).Formula = "=$F2/$E2"
y.Sheets("Feuil1").Range("G2:G" & .Cells(.Rows.count, "F").End(xlUp).Row).NumberFormat = "0.00%"
End With
With y.Sheets("Feuil1").Range("H" & i + 1)
.Formula = "='" & "[" & myFile & "]Para RF'!D6" 'société
.Value = .Value
End With
With y.Sheets("Feuil1").Range("I" & i + 1)
.Formula = "='" & "[" & myFile & "]Para RF'!F8" 'ville
.Value = .Value
End With
With y.Sheets("Feuil1").Range("J" & i + 1)
.Formula = "='" & "[" & myFile & "]Para RF'!G5" 'nom vendeur
.Value = .Value
End With
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
myFile = Dir()
Next
'Save and Close Workbook
y.Close saveChanges:=True
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
不可能在沒有看到病人的情況下準確診斷問題。粘貼代碼。 – KoderM16
這兩個工作簿是在「受保護的視圖」或「只讀」中打開的嗎? – UGP
也沒有「代碼不起作用」一個有效/良好的錯誤描述(所以請[編輯]你的問題,並添加錯誤信息),也沒有人可以說如果你不顯示它代碼最新錯誤(所以請[編輯]你的問題,並添加你的宏代碼)。 –