Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 错误#1004:应用程序定义或对象定义的错误。宏将停止_Excel_Vba - Fatal编程技术网

Excel 错误#1004:应用程序定义或对象定义的错误。宏将停止

Excel 错误#1004:应用程序定义或对象定义的错误。宏将停止,excel,vba,Excel,Vba,我试图找到解决问题的方法,但我做不到。我找到了一个将信息从一个excel文件导入到另一个excel文件的代码。我重新使用了工作表命名和列编号,但当我尝试运行它时,它给了我一个错误:“error#1004:应用程序定义或对象定义的错误。宏将停止”。你能帮我吗 Private Sub CommandButton1_Click() On Error GoTo errorhandler Dim ThisWorkbook As Workbook Dim ws As Worksheet Dim RngFle

我试图找到解决问题的方法,但我做不到。我找到了一个将信息从一个excel文件导入到另一个excel文件的代码。我重新使用了工作表命名和列编号,但当我尝试运行它时,它给了我一个错误:“error#1004:应用程序定义或对象定义的错误。宏将停止”。你能帮我吗

Private Sub CommandButton1_Click()
On Error GoTo errorhandler
Dim ThisWorkbook As Workbook
Dim ws As Worksheet
Dim RngFleetData, rng As Range
Dim x As Variant
Dim countryN, counnty As String

Dim lReadFirstRow As Long
Dim lReadLastRow As Long
Dim lWriteFirstRow As Long
Dim lWriteLastRow As Long
Dim iRow As Integer
Dim NumOfMonth As Double
filenev = ActiveWorkbook.Name
Application.Calculation = xlCalculationManual
NRRowsRange = 1

 x = Application.GetOpenFilename("Excel Spreadsheets ,*.xls*", , "Open File")
If x = False Then
    Exit Sub
End If

Set ThisWorkbook = Workbooks.Open(x, False, True)

ThisWorkbook.Worksheets("Sheet1").Unprotect

 copied = 0

 j = 1
 Do While Workbooks(filenev).Sheets("auto").Cells(j, 1) <> "fields extract"
 j = j + 1
 Loop
 j = j + 3

 i = 0
 Do While ThisWorkbook.Worksheets("Sheet1").Cells(i, 3) <> ""
    If ThisWorkbook.Worksheets("Sheet1").Cells(i, 1) <> 0 Then

        Workbooks(filenev).Sheets("auto").Cells(j, 1) = ThisWorkbook.Worksheets("Sheet1").Cells(i, 3)
        Workbooks(filenev).Sheets("auto").Cells(j, 2) = ThisWorkbook.Worksheets("Sheet1").Cells(i, 12)
        Workbooks(filenev).Sheets("auto").Cells(j, 3) = ThisWorkbook.Worksheets("Sheet1").Cells(i, 13)
        Workbooks(filenev).Sheets("auto").Cells(j, 4) = ThisWorkbook.Worksheets("Sheet1").Cells(i, 16)
        Workbooks(filenev).Sheets("auto").Cells(j, 5) = ThisWorkbook.Worksheets("Sheet1").Cells(i, 19)
        Workbooks(filenev).Sheets("auto").Cells(j, 6) = ThisWorkbook.Worksheets("Sheet1").Cells(i, 20)
        Workbooks(filenev).Sheets("auto").Cells(j, 7) = ThisWorkbook.Worksheets("Sheet1").Cells(i, 22)
        Workbooks(filenev).Sheets("auto").Cells(j, 8) = ThisWorkbook.Worksheets("Sheet1").Cells(i, 23)
        Workbooks(filenev).Sheets("auto").Cells(j, 9) = ThisWorkbook.Worksheets("Sheet1").Cells(i, 24)
        Workbooks(filenev).Sheets("auto").Cells(j, 10) = ThisWorkbook.Worksheets("Sheet1").Cells(i, 25)
        Workbooks(filenev).Sheets("auto").Cells(j, 11) = ThisWorkbook.Worksheets("Sheet1").Cells(i, 26)
        Workbooks(filenev).Sheets("auto").Cells(j, 12) = ThisWorkbook.Worksheets("Sheet1").Cells(i, 27)
        Workbooks(filenev).Sheets("auto").Cells(j, 13) = ThisWorkbook.Worksheets("Sheet1").Cells(i, 28)
        Workbooks(filenev).Sheets("auto").Cells(j, 14) = ThisWorkbook.Worksheets("Sheet1").Cells(i, 32)
        Workbooks(filenev).Sheets("auto").Cells(j, 15) = ThisWorkbook.Worksheets("Sheet1").Cells(i, 33)
        Workbooks(filenev).Sheets("auto").Cells(j, 16) = ThisWorkbook.Worksheets("Sheet1").Cells(i, 34)
        Workbooks(filenev).Sheets("auto").Cells(j, 17) = ThisWorkbook.Worksheets("Sheet1").Cells(i, 35)
        Workbooks(filenev).Sheets("auto").Cells(j, 18) = ThisWorkbook.Worksheets("Sheet1").Cells(i, 11)


    If ThisWorkbook.Worksheets("Sheet1").Cells(i, 1) = "" Then Workbooks(filenev).Sheets("auto").EntireRow.Delete
       If ThisWorkbook.Worksheets("Sheet1").Cells(i, 2) = 0 Then Workbooks(filenev).Sheets("auto").EntireRow.Delete
       Application.Goto Workbooks(filenev).Sheets("auto").Cells(j, 1)
       ActiveCell.Rows(NRRowsRange).EntireRow.Select
       Selection.Copy
       Selection.Insert Shift:=xlDown
       copied = 1



j = j + 1
End If
i = i + 1
Loop

If copied = 1 Then
ActiveCell.Rows(NRRowsRange).EntireRow.Select
Selection.Delete
Selection.Insert Shift:=xlUp
End If

Application.DisplayAlerts = False
ThisWorkbook.Close False
Application.DisplayAlerts = True


MsgBox "fields has been imported sucessfully!"

  Application.Calculation = xlCalculationAutomatic
Workbooks(filenev).Sheets("auto").Activate

errorhandler:
Select Case Err.Number
Case 9
MsgBox "Hey Buddy, this is NOT the right extract! Macro will STOP", vbExclamation, "STOP"
ThisWorkbook.Close False
Case 0
Case Else
MsgBox "Error # " & Err & " : " & Error(Err) & "Macro will STOP"
End Select
End Sub
Private子命令按钮1\u单击()
关于错误转到错误处理程序
将此工作簿设置为工作簿
将ws设置为工作表
变暗RngFleetData,rng As范围
Dim x作为变体
昏暗的乡村,如弦般的宁静
第一排的长度与第一排相同
变暗lReadLastRow为长
第一排一样长
暗淡的灰岩和长的灰岩一样
Dim iRow作为整数
这个月是双倍的
filenev=ActiveWorkbook.Name
Application.Calculation=xlCalculationManual
nRowsRange=1
x=Application.GetOpenFilename(“Excel电子表格,*.xls*”,“打开文件”)
如果x=False,则
出口接头
如果结束
设置此工作簿=工作簿。打开(x,False,True)
此工作簿。工作表(“表1”)。取消保护
复制=0
j=1
Do While工作簿(filenev).Sheets(“自动”).Cells(j,1)“字段提取”
j=j+1
环
j=j+3
i=0
执行此工作簿。工作表(“表1”)。单元格(i,3)”
如果此工作簿.Worksheets(“Sheet1”).Cells(i,1)为0,则
工作簿(filenev).Sheets(“auto”).Cells(j,1)=此工作簿.工作表(“Sheet1”).Cells(i,3)
工作簿(filenev).Sheets(“auto”).Cells(j,2)=此工作簿.工作表(“Sheet1”).Cells(i,12)
工作簿(filenev).Sheets(“auto”).Cells(j,3)=此工作簿.工作表(“Sheet1”).Cells(i,13)
工作簿(filenev).Sheets(“auto”).Cells(j,4)=此工作簿.工作表(“Sheet1”).Cells(i,16)
工作簿(filenev).Sheets(“auto”).Cells(j,5)=此工作簿.工作表(“Sheet1”).Cells(i,19)
工作簿(filenev).Sheets(“auto”).Cells(j,6)=此工作簿.工作表(“Sheet1”).Cells(i,20)
工作簿(filenev).Sheets(“auto”).Cells(j,7)=此工作簿.工作表(“Sheet1”).Cells(i,22)
工作簿(filenev).Sheets(“auto”).Cells(j,8)=此工作簿.工作表(“Sheet1”).Cells(i,23)
工作簿(filenev).Sheets(“auto”).Cells(j,9)=此工作簿.工作表(“Sheet1”).Cells(i,24)
工作簿(filenev).Sheets(“auto”).Cells(j,10)=此工作簿.工作表(“Sheet1”).Cells(i,25)
工作簿(filenev).Sheets(“auto”).Cells(j,11)=此工作簿.工作表(“Sheet1”).Cells(i,26)
工作簿(filenev).Sheets(“auto”).Cells(j,12)=此工作簿.工作表(“Sheet1”).Cells(i,27)
工作簿(filenev).Sheets(“auto”).Cells(j,13)=此工作簿.工作表(“Sheet1”).Cells(i,28)
工作簿(filenev).Sheets(“auto”).Cells(j,14)=此工作簿.工作表(“Sheet1”).Cells(i,32)
工作簿(filenev).Sheets(“auto”).Cells(j,15)=此工作簿.工作表(“Sheet1”).Cells(i,33)
工作簿(filenev).Sheets(“auto”).Cells(j,16)=此工作簿.工作表(“Sheet1”).Cells(i,34)
工作簿(filenev).Sheets(“auto”).Cells(j,17)=此工作簿.工作表(“Sheet1”).Cells(i,35)
工作簿(filenev).Sheets(“auto”).Cells(j,18)=此工作簿.工作表(“Sheet1”).Cells(i,11)
如果此工作簿.Worksheets(“Sheet1”).Cells(i,1)=“”,则工作簿(filenev).Sheets(“auto”).EntireRow.Delete
如果ThisWorkbook.Worksheets(“Sheet1”).Cells(i,2)=0,则工作簿(filenev).Sheets(“auto”).EntireRow.Delete
应用程序。转到工作簿(filenev)。工作表(“自动”)。单元格(j,1)
ActiveCell.Rows(nRowsRange).EntireRow.Select
选择,复制
选择。插入移位:=xlDown
复制=1
j=j+1
如果结束
i=i+1
环
如果复制=1,则
ActiveCell.Rows(nRowsRange).EntireRow.Select
选择。删除
选择。插入移位:=xlUp
如果结束
Application.DisplayAlerts=False
此工作簿。关闭错误
Application.DisplayAlerts=True
MsgBox“字段已成功导入!”
Application.Calculation=xlCalculationAutomatic
工作簿(filenev).工作表(“自动”).激活
错误处理程序:
选择案例错误编号
案例9
MsgBox“嘿,伙计,这不是正确的提取!宏将停止”,VBEquipment,“停止”
此工作簿。关闭错误
案例0
其他情况
MsgBox“Error#”&Err&:“&Error(Err)&”宏将停止
结束选择
端接头

提前谢谢你

我看到这行有错误

 i = 0
 Do While ThisWorkbook.Worksheets("Sheet1").Cells(i, 3) <> ""
要删除哪一行?你必须提到那场争吵。比如说

Workbooks(filenev).Sheets("auto").Rows(1).Delete

编辑
对不起,我忍不住给你这个建议。我注意到了一些我想指出的事情

A。使用
选项Explicit
这将确保声明所有变量。现在,为什么这很重要?使用
选项Explicit

a) 。它强制您将变量声明为特定的数据类型

b) 。它监视您的代码,检查在键入变量时可能发生的拼写错误

你可能也想读书

B使用正确的处理方法。这是必需的,这样您就可以捕获错误,更不用说“恢复默认值”

例如,您正在设置
Application.Calculation=xlCalculationManual
?我推荐这样的东西

Option Explicit

Private Sub Sample()
    Dim clc As Long

    On Error GoTo errorhandler

    clc = Application.Calculation

    Application.Calculation = xlCalculationManual

    '
    '~~> REST OF YOUR CODE
    '

LetsContinue:
    Application.Calculation = clc '<~~ Reset Calc
    Exit Sub
errorhandler:
    Select Case Err.Number
    Case 9
        MsgBox "Hey Buddy, this is NOT the right extract! Macro will STOP", vbExclamation, "STOP"
        ThisWorkbook.Close False
    Case Else
        MsgBox "Error # " & Err & " : " & Error(Err) & "Macro will STOP"
    End Select
    Resume LetsContinue
End Sub
选项显式
私有子样本()
Dim clc尽可能长
关于错误转到错误处理程序
clc=应用程序计算
Application.Calculation=xlCalculationManual
'
“~~>其余的代码
'
让我们继续:

Application.Calculation=clc'您能在调试模式下运行它吗?按F8键可以设置几个断点?然后让我们确切地知道您得到的是哪一行错误?顺便说一句,我不会在这里使用像
ThisWorkbook
这样的名称。您可以将其更改为其他内容,尽管这可能不是问题。+1@bonCodigo:这是一个有效点<代码>此工作簿
是保留名称。另外:@Aquila:在错误转到e时注释行

Option Explicit

Private Sub Sample()
    Dim clc As Long

    On Error GoTo errorhandler

    clc = Application.Calculation

    Application.Calculation = xlCalculationManual

    '
    '~~> REST OF YOUR CODE
    '

LetsContinue:
    Application.Calculation = clc '<~~ Reset Calc
    Exit Sub
errorhandler:
    Select Case Err.Number
    Case 9
        MsgBox "Hey Buddy, this is NOT the right extract! Macro will STOP", vbExclamation, "STOP"
        ThisWorkbook.Close False
    Case Else
        MsgBox "Error # " & Err & " : " & Error(Err) & "Macro will STOP"
    End Select
    Resume LetsContinue
End Sub