在我的程序的一部分中,我想通过VBA打开现有的Excel文件,以便修改它并操纵数据。
'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
我制作了此代码,但在Set wb = Workbooks.Open (Fname & "CFRP1-" & nummatrix & "-" & numimpact & ".xlsm")
行上找不到运行时错误1004文件。当我指出正确的路径和文件时,我不明白为什么。我尝试了另一种方法Set wb = Workbooks.Open("D:\mmLaurencon\Desktop\NL\Test\CFRP 1\CFRP1-1-" & numimpact)
但是新的运行时错误1004文档可能是只读的或加密的。
你知道出了什么问题吗?提前谢谢!