我的代码运行并在一些excel工作簿上运行,但不是全部,为什么? VBA

时间:2017-05-31 12:51:22

标签: excel vba excel-vba

我的代码VBA有一个奇怪的问题。

我正在使用excel 2010,我写了一个代码,除了一些代码之外,它可以完美地用于多个源代码。

我的代码对象是从多个工作簿中复制同一工作表中的相同单元格,并将其作为列粘贴到目标工作簿中。

我的代码在50个工作簿上运行没有任何问题,除了2。 请注意,这两个样本与其他样本相同,但当然是不同的值。 如果我将这2个工作簿与其他50个工作簿一起使用,我会收到错误消息“执行错误'1004',然后我应该停止该过程。 黄线代表公式: 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`

你知道为什么会出现这个问题吗? 我该怎么办?是否有任何改变设置等?

注意我已经尝试将这些2保存为没有宏的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

1 个答案:

答案 0 :(得分:1)

您没有迭代50个工作簿。您正在迭代50个文件名

.Formula = "='" & "[" & myFile & "]Para RF'!L2"

此行上的错误1004表示myFile以某种方式包含无效字符,或者(更有可能)该工作簿中没有名为Para RF的工作表。

尝试直接在单元格中输入='[that file name.xlsx]Para RF'!L2(在任何工作簿中)。你会看到这个:

A formula in this worksheet contains one or more invalid references. Verify that your formula contains a valid path, workbook, range name, and cell reference.

因此,验证您的公式是否包含有效路径,工作簿,范围名称和单元格引用。

如果在失败的工作簿中 这样的Para RF工作表,请确保没有前导/尾随空格。