在我的程序的一部分中,我想通過VBA打開現有的Excel文件,以修改它並操作數據。Excel VBA - 無法找到運行時錯誤1004文件
'Declaration des variables d'objects Excel
Dim wb As Workbook
Dim ws As Worksheet
Dim Fname As String
'Declaration des variables de calcul
Dim a As Double
Dim numimpact, nummatrix, debut, fin, e, n As Long
Dim i As Boolean
'Initialisation des variables
i = True
a = 0
e = 1
numimpact = 1
nummatrix = 1
debut = 2
n = 1000
fin = debut + n
'Boucle de lecture de tous les fichiers Excel
Do While i = True
'Test et incrementation des fichiers Excel
If numimpact < 7 Then
'Ouverture fichiers
Fname = "D:\mmLaurencon\Desktop\NL\Test\CFRP1\"
Set wb = Workbooks.Open(Fname & "CFRP1-" & nummatrix & "-" & numimpact & ".xlsm")
'Set wb = Workbooks.Open("D:\mmLaurencon\Desktop\NL\Test\CFRP 1\CFRP1-1-" & numimpact)
Set ws = wb.Worksheets(1)
'Parcourir colonne B
Do While Cells(e, 2).Value <> ""
For Each e In Columns(2)
Cells(fin, 3).Value = Application.Sum(Cells(debut, 2).Value, Cells(fin, 2).Value)/n
debut = debut + 1
fin = fin + 1
e = e + 1
'save the file
ActiveWorkbook.SaveAs Filename:= _
"D:\mmLaurencon\Desktop\NL\Test\CFRP " & nummatrix & "\CFRP1-1-" & numimpact & ".xlsm", _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
'close the file
wb.Close
Next
numimpact = numimpact + 1
Loop
ElseIf numimpact = 7 Then
nummatrix = nummatrix + 1
numimpact = 1
ElseIf nummatrix = 10 Then
i = False
End If
Loop
我做了這個代碼,但無法找到一個運行時錯誤1004的文件出現在導線Set wb = Workbooks.Open (Fname & "CFRP1-" & nummatrix & "-" & numimpact & ".xlsm")
。我不明白爲什麼,因爲我指出了正確的路徑和文件。我嘗試了另一種方法來執行此操作Set wb = Workbooks.Open("D:\mmLaurencon\Desktop\NL\Test\CFRP 1\CFRP1-1-" & numimpact)
,但出現新的運行時錯誤1004,文檔可能是隻讀或加密的。
你有什麼想錯了嗎?先謝謝你!
您是否嘗試過將Fname設置爲:Fname = CreateObject(「WScript.Shell」).fontfolders(「Desktop」)&「\ NL \ Test \ CFRP1 \」' –
另外:'Dim numimpact,nummatrix,debut, fin,e,n As Long' - 只有'n'被聲明爲long,其他則是變體。你需要使用'Dim numimpact As Long,nummatrix As Long,.....' –
謝謝大家糾正我的重大錯誤......但是我仍然有一個錯誤1004文件找不到'Set wb = Workbooks.Open(「D:\ mmLaurencon \ Desktop \ NL \ Test \ CFRP 1 \ CFRP1-」&nummatrix&「 - 」&numimpact&「.XLS」)'。對於另一種替代方案,它正在工作,但我在'Set ws = wb.Worksheets(1)'行中有另一個錯誤。我不明白爲什麼路徑是正確的,並且ws是一個excel對象,因此使用'Set'應該可以工作,您不覺得嗎? –