运行时错误1004应用程序定义或对象定义错误 [英] Runtime error 1004 Application defined or object defined error

查看:172
本文介绍了运行时错误1004应用程序定义或对象定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加vlookup的第一行代码上收到运行时错误1004应用程序定义或对象定义错误。从底部约5行。我很欣赏任何有关驱动它的信息。我的截止时间很短。





  Sub  Karen()

工作表( 在此处粘贴数据)。选择

Dim Colnum 作为 整数
Dim 前缀作为 字符串
Dim 后缀作为 字符串
Dim x 作为



对于 x = 1 100
我f UCase(单元格( 1 ,x))= CASE CODE 然后
Colnum = x - 1 < span class =code-comment>' 此示例中左侧Case Code的列。检查并确保它不是第一列或发生错误是有意义的
退出 对于
结束 如果
下一步

对于 x = 1 60000

如果中(范围( T1:T60000)。Cells(x),< span class =code-digit> 6 , 1 )= - 然后

prefix = Mid(范围( T1:T60000)。单元格(x), 1 5
结束 如果

如果 Cells(x,Colnum + 1 )<> Len(修剪(范围) ( D1:D60000)。Cells(x)))= 5 然后
Cells(x,Colnum)= CStr (前缀和单元格(x,Colnum + 1 ))

如果 Mid (范围( D1:D60000)。Cells(x), 6 1 )= - 然后
后缀=中(范围( D1:D60000)。Cells(x), 7 5
单元格(x,Colnum)= CStr (前缀&后缀)
Else:Cells(x,Colnum)= CStr (前缀& Cells(x,Colnum + 1 ))

如果范围( D1:D60000)。Cells(x)= *** ******* 然后
Cells(x,Colnum)= CStr (Cells(x,Colnum + 1 ))
结束 如果
结束 如果

结束 如果

下一步

For x = 1 60000

如果中(范围( T1:T60000)。单元格(x), 6 1 ) = - 然后

prefix = Mid(范围( T1:T60000)。Cells(x) , 1 5
结束 如果

如果 Cells(x,Colnum + 1 )<> Len(修剪(范围) ( D1:D60000)。Cells(x)))= 4 然后
Cells(x,Colnum)= CStr ( prefix& 0& Cells(x,Colnum + 1 ))


结束 如果

下一步

对于 x = < span class =code-digit> 1 60000

如果中(范围( T1:T60000 )。单元格(x), 6 1 )= - 然后

prefix = Mid(范围( T1:T60000)。Cells(x), 1 5
结束 如果

If Cells(x,Colnum + 1 )<> Len(修剪(范围) ( D1:D60000)。Cells(x)))= 10 然后
Cells(x,Colnum)= Cells(x,Colnum + 1


结束 如果


下一步


用于 x = 1 60000

如果中(范围( T1:T60000)。单元格(x), 6 1 )= < span class =code-string> - < span class =code-keyword>然后

prefix = Mid(范围( T1:T60000)。单元格(x), 1 5
结束 如果

如果单元格(x,Colnum + 1 )<> Len(修剪(范围) ( D1:D60000)。Cells(x)))= 11 然后
Cells(x,Colnum)= Mid(Trim(Range( D1:D60000)。Cells(x)), 1 5 )&中(修剪(范围( D1:D60000)。Cells(x)), 7 5

结束 如果

下一步


范围( V1)= On Restricted List
Range( W1)= 导出变量
范围( X1)= 在促销列表中

对于 x = 1 60000


如果单元格(x,Colnum + 5 )> 0 然后


范围( V1:V60000)。Cells(x)= < span class =code-string> = IF(ISERROR(VLOOKUP(RC [-19],'Export Restricted List'!C [-21],1,FALSE,)

Range( W1:W60000)。Cells(x)= = IF(ISERROR(VLOOKUP(RC [-20],'Export Restricted List'!C [-22]:C [-17],5,FALSE) ,
范围( X1:X60000)。Cells(x)= = IF(ISERROR(VLOOKUP(RC [-21],'Export Restricted List'!C [-22]:C [-17],6,FALSE),

结束 如果

下一步

结束 Sub

解决方案

建议:使用调试器运行程序并检查每一行的变量和单元格。



对于您的错误消息:您希望将公式存储在单元格中但是

   = IF(ISERROR(VLOOKUP(RC [-19],'导出限制列表'!C [-21],1,FALSE,) 

不是一个公式,实际上你的3个查找行都不是公式。更重要的是,单元格是一个对象,而不是变量。



你的程序很奇怪。你有没有在VBA for Excel中创建这样的程序?



我看到很多地方你的程序无法做你想做的事,即使没有错误信息。



您应该花时间学习在单元格中存储值和公式的正确方法。得到一个教程,得到一个在单元格中存储东西的示例程序。



纠正我看到的所有问题都是对程序的完全重写,所以你要么找到一个tuto有了excel的VBA细节,要么你雇用一个程序员。


你可以检查excel表名.Excel名称应该是粘贴数据这里只有你的代码才能运行。我已经尝试过你的代码唯一的问题是你可能没有工作表名称作为粘贴数据。


  Sub  CheckOrderMacro()

工作表( 在此处粘贴订单数据 )。选择

Dim Colnum 正如 整数
Dim 前缀作为 字符串
Dim 后缀正如 字符串
Dim x 正如
Dim RestrictLst 作为 变体
Dim LastRow 作为
Dim Sht As 工作表


' 查看导出限制列表以确定最后一行数据
' 并根据A1到A的最后一行创建数组...

设置 Sht = ThisWorkbook.Worksheets( Export Restricted List
LastRow = Sht.Cells (Sht.Rows.Count, A)。结束(xlUp)。

' 设置限制列表数组
RestrictLst = Range( 'Export Restricted List'!A1:A& LastRow)。值

' 移至粘贴订单数据此处工作表以比较案例代码
' 针对数组RestrictLst

< span class =code-keyword>设置 Sht = ThisWorkbook.Worksheets( 在此处粘贴订单数据

' 以下代码确定哪个列包含案例代码。
' 虽然宏依赖于从D列开始粘贴的订单数据。

范围( C1)。选择
对于 x = 1 100

' 必不可少的标题案例代码在之前,之间或之后没有额外的空格。
如果 UCase(单元格( 1 ,x))= CASE CODE 然后

' 左侧列的案例代码将保存值以与数组进行比较。
Colnum = x - 1
退出 对于
结束 如果
下一步

' 循环捕获case co前缀的行案例代码的后缀和后缀
' 并将它们组合成一个完整的案例代码进行比较。
对于 x = 1 1000

如果中(范围( T1:T1000)。Cells(x), 6 1 )= - 然后

prefix = Mid(范围( T1:T1000)。单元格(x), 1 5
结束 如果

如果单元格(x,Colnum + 1 )<> Len(修剪(范围) ( D1:D1000)。Cells(x)))= 5 然后
Cells(x,Colnum)= CStr (前缀和单元格(x,Colnum + 1 ))

如果 Mid (范围( D1:D1000)。Cells(x), 6 1 )= - 然后
后缀=中(范围( D1:D1000)。单元格(x), 7 5
Cells(x,Colnum) = CStr (前缀&后缀)
Else:Cells(x,Colnum)= CStr (前缀& Cells(x,Colnum + 1 ))

如果范围( D1:D1000)。Cells(x)= *** ******* 然后
Cells(x,Colnum)= CStr (Cells(x,Colnum + 1 ))
结束 如果
结束 如果

结束 如果

下一步

对于 x = 1 1000

如果 Mid(范围( T1:T1000)。Cells(x), 6 1 )= - 然后

prefix = Mid(范围( T1:T1000)。单元格(x), 1 5
结束 如果

< span class =code-keyword> If
Cells(x,Colnum + 1 )<> Len(修剪(范围) ( D1:D1000)。Cells(x)))= 4 然后
Cells(x,Colnum)= CStr ( prefix& 0& Cells(x,Colnum + 1 ))
结束 如果

下一步

对于 x = 1 1000

If Mid(范围( T1:T1000)。细胞( x), 6 1 )= - 然后

prefix = Mid(范围( T1:T1000).Cells(x), 1 5
结束 如果

If Cells (x,Colnum + 1 )<> Len(修剪(范围) ( D1:D1000)。Cells(x)))= 10 然后
Cells(x,Colnum)= Cells(x,Colnum + 1
结束 如果

下一步

对于 x = 1 1000

如果中(范围( T1:T1000)。单元格(x), 6 1 )= - 然后

prefix = Mid(范围( T1:T1000)。细胞( x), 1 5
结束 如果

如果 Cells(x,Colnum + 1 )<> Len(修剪(范围) ( D1:D1000)。Cells(x)))= 11 然后
Cells(x,Colnum)= Mid(Trim(Range( D1:D1000)。Cells(x)), 1 5 )& Mid(Trim(范围( D1:D1000)。Cells(x)), 7 5

结束 如果

下一步



对于 x = 1 To 1000

范围( V1:V1000)。Cells(x)= = VLOOKUP(RC [-19 ],'导出限制列表'!C [-21]:C [-21],1,FALSE)
范围( W1:W1000)。Cells(x)= = VLOOKUP(RC [-20],'导出限制列表'!C [-22]:C [-15],5,FALSE)
范围( X1:X1000)。Cells(x)= = VLOOKUP(RC [-21],'导出限制列表'!C [-23]:C [-15],6,FALSE)
范围( Y1:Y1000)。Cells(x)= = VLOOKUP(RC [-22],'导出限制列表'!C [-24]:C [-15],7,FALSE )


下一步

范围( V1)= On Restricted List
范围( W1)= 导出变体
范围( X1)= 促销列表
范围( Y1)= 限制开始

范围( V2:Y500)。选择
Selection.Copy
Selection.PasteSpecial Paste:= xlPasteValues,Operation:= xlNone,SkipBlanks _
:= False,Transpose:= False
Selection.Replace What:= #N / A,替换:= ,LookAt:= xlPart,_
SearchOrder:= xlByRows,MatchCase:= False,SearchFormat:= False,_
ReplaceFormat:= False
Selection.Replace内容:= 1/0/1900,替换:= ,LookAt:= xlPart,_
SearchOrder:= xlByRows,MatchCase:= False,SearchFormat:= False,_
ReplaceFormat:= False
Application.CutCopyMode = False

范围( W2:Y500)。选择
Selection.Replace什么:= 0 ,替换:= ,LookAt:= xlPart,_
SearchOrder:= xlByRows, MatchCase:= False,SearchFormat:= False,_
ReplaceFormat:= False

范围( D1)。选择

MsgBox( 订单检查完成。请查看结果。

结束 Sub


I am receiving the Run-time error 1004 Application defined or object defined error on the first line of code where I am trying to add a vlookup. It is about 5 lines from the bottom. I appreciate any insight on what is driving it. I am under a tight deadline to finish.


Sub Karen()

Worksheets("Paste Data Here").Select

Dim Colnum As Integer
Dim prefix As String
Dim suffix As String
Dim x As Long



For x = 1 To 100
If UCase(Cells(1, x)) = "CASE CODE" Then
Colnum = x - 1 'The column to the left Case Code in this example.  It would make sense to check and make sure it isn't the first column or an error will occur
Exit For
End If
Next

For x = 1 To 60000

    If Mid(Range("T1:T60000").Cells(x), 6, 1) = "-" Then

        prefix = Mid(Range("T1:T60000").Cells(x), 1, 5)
    End If

            If Cells(x, Colnum + 1) <> "" And Len(Trim(Range("D1:D60000").Cells(x))) = 5 Then
             Cells(x, Colnum) = CStr(prefix & Cells(x, Colnum + 1))

                If Mid(Range("D1:D60000").Cells(x), 6, 1) = "-" Then
                    suffix = Mid(Range("D1:D60000").Cells(x), 7, 5)
                    Cells(x, Colnum) = CStr(prefix & suffix)
                        Else: Cells(x, Colnum) = CStr(prefix & Cells(x, Colnum + 1))

                            If Range("D1:D60000").Cells(x) = "**********" Then
                                Cells(x, Colnum) = CStr(Cells(x, Colnum + 1))
                            End If
                End If

            End If

Next

For x = 1 To 60000

    If Mid(Range("T1:T60000").Cells(x), 6, 1) = "-" Then

        prefix = Mid(Range("T1:T60000").Cells(x), 1, 5)
    End If

            If Cells(x, Colnum + 1) <> "" And Len(Trim(Range("D1:D60000").Cells(x))) = 4 Then
             Cells(x, Colnum) = CStr(prefix & "0" & Cells(x, Colnum + 1))


            End If

Next

For x = 1 To 60000

    If Mid(Range("T1:T60000").Cells(x), 6, 1) = "-" Then

        prefix = Mid(Range("T1:T60000").Cells(x), 1, 5)
    End If

            If Cells(x, Colnum + 1) <> "" And Len(Trim(Range("D1:D60000").Cells(x))) = 10 Then
             Cells(x, Colnum) = Cells(x, Colnum + 1)


            End If


Next


For x = 1 To 60000

    If Mid(Range("T1:T60000").Cells(x), 6, 1) = "-" Then

        prefix = Mid(Range("T1:T60000").Cells(x), 1, 5)
    End If

            If Cells(x, Colnum + 1) <> "" And Len(Trim(Range("D1:D60000").Cells(x))) = 11 Then
             Cells(x, Colnum) = Mid(Trim(Range("D1:D60000").Cells(x)), 1, 5) & Mid(Trim(Range("D1:D60000").Cells(x)), 7, 5)

            End If

Next


Range("V1") = "On Restricted List"
Range("W1") = "Export Variant"
Range("X1") = "On Promo List"

For x = 1 To 60000


If Cells(x, Colnum + 5) > 0 Then


    Range("V1:V60000").Cells(x) = "=IF(ISERROR(VLOOKUP(RC[-19],'Export Restricted List'!C[-21],1,FALSE,)"
    Range("W1:W60000").Cells(x) = "=IF(ISERROR(VLOOKUP(RC[-20],'Export Restricted List'!C[-22]:C[-17],5,FALSE),"""")"
    Range("X1:X60000").Cells(x) = "=IF(ISERROR(VLOOKUP(RC[-21],'Export Restricted List'!C[-22]:C[-17],6,FALSE),"""")"

End If

Next

End Sub

解决方案

advice: Run your program with the debugger and check the variables and cells on every single line.

For your error message: you want to store a formula in a cell but

"=IF(ISERROR(VLOOKUP(RC[-19],'Export Restricted List'!C[-21],1,FALSE,)"

is not a formula, in fact none of your 3 lookup lines is a formula. More importantly, a cell is an object, not a variable.

Your program is weird. Have you ever made such a program in VBA for Excel ?

I see many places where your program will fail to do what you want, even if there is no error message.

You should take time to learn the proper way to store values and formulas in a cell. get a tutorial, get a sample program that store things in cells.

Correcting all the problems I see is a complete rewrite of the program, so either you find a tuto with specifics to VBA for excel, either you hire a programmer.


can you please check the excel sheet name .Excel name should be Paste Data Here hen only your code will work. I have tried your code Only thing is you might not have a sheetname as Paste Data Here.


Sub CheckOrderMacro()

Worksheets("Paste Order Data Here").Select

Dim Colnum As Integer
Dim prefix As String
Dim suffix As String
Dim x As Long
Dim RestrictLst As Variant
Dim LastRow As Long
Dim Sht As Worksheet


' Look at Export Restricted List to determine last row of data
' and create array based on A1 to A last row...

Set Sht = ThisWorkbook.Worksheets("Export Restricted List")
LastRow = Sht.Cells(Sht.Rows.Count, "A").End(xlUp).Row

'Set Array for Restricted List
RestrictLst = Range("'Export Restricted List'!A1:A" & LastRow).Value

'Move to the Paste Order Data Here worksheet to compare the case codes
'against the array RestrictLst

Set Sht = ThisWorkbook.Worksheets("Paste Order Data Here")

'The following code determines what column contains the case codes.
'Although the macro is reliant on the order data being pasted starting in column D.

Range("C1").Select
For x = 1 To 100

    ' It is essential that the header Case Code does not have additional spaces before, in between or after.
    If UCase(Cells(1, x)) = "CASE CODE" Then

        'The column to the left Case Code will hold the values to compare to the array.
        Colnum = x - 1
Exit For
    End If
Next

' Loops through the rows capturing the prefix of the case codes and suffix of the case codes
' and combining them to a full case code for comparison.
For x = 1 To 1000

    If Mid(Range("T1:T1000").Cells(x), 6, 1) = "-" Then
        
        prefix = Mid(Range("T1:T1000").Cells(x), 1, 5)
    End If
    
    If Cells(x, Colnum + 1) <> "" And Len(Trim(Range("D1:D1000").Cells(x))) = 5 Then
        Cells(x, Colnum) = CStr(prefix & Cells(x, Colnum + 1))
             
        If Mid(Range("D1:D1000").Cells(x), 6, 1) = "-" Then
            suffix = Mid(Range("D1:D1000").Cells(x), 7, 5)
            Cells(x, Colnum) = CStr(prefix & suffix)
        Else: Cells(x, Colnum) = CStr(prefix & Cells(x, Colnum + 1))
                
            If Range("D1:D1000").Cells(x) = "**********" Then
                Cells(x, Colnum) = CStr(Cells(x, Colnum + 1))
            End If
        End If
    
    End If

Next

For x = 1 To 1000

    If Mid(Range("T1:T1000").Cells(x), 6, 1) = "-" Then
        
        prefix = Mid(Range("T1:T1000").Cells(x), 1, 5)
    End If
    
    If Cells(x, Colnum + 1) <> "" And Len(Trim(Range("D1:D1000").Cells(x))) = 4 Then
        Cells(x, Colnum) = CStr(prefix & "0" & Cells(x, Colnum + 1))
    End If

Next

For x = 1 To 1000

    If Mid(Range("T1:T1000").Cells(x), 6, 1) = "-" Then
        
        prefix = Mid(Range("T1:T1000").Cells(x), 1, 5)
    End If
    
    If Cells(x, Colnum + 1) <> "" And Len(Trim(Range("D1:D1000").Cells(x))) = 10 Then
        Cells(x, Colnum) = Cells(x, Colnum + 1)
     End If

Next

For x = 1 To 1000

    If Mid(Range("T1:T1000").Cells(x), 6, 1) = "-" Then
        
        prefix = Mid(Range("T1:T1000").Cells(x), 1, 5)
    End If
    
    If Cells(x, Colnum + 1) <> "" And Len(Trim(Range("D1:D1000").Cells(x))) = 11 Then
        Cells(x, Colnum) = Mid(Trim(Range("D1:D1000").Cells(x)), 1, 5) & Mid(Trim(Range("D1:D1000").Cells(x)), 7, 5)
             
    End If

Next



For x = 1 To 1000

    Range("V1:V1000").Cells(x) = "=VLOOKUP(RC[-19],'Export Restricted List'!C[-21]:C[-21],1,FALSE)"
    Range("W1:W1000").Cells(x) = "=VLOOKUP(RC[-20],'Export Restricted List'!C[-22]:C[-15],5,FALSE)"
    Range("X1:X1000").Cells(x) = "=VLOOKUP(RC[-21],'Export Restricted List'!C[-23]:C[-15],6,FALSE)"
    Range("Y1:Y1000").Cells(x) = "=VLOOKUP(RC[-22],'Export Restricted List'!C[-24]:C[-15],7,FALSE)"

    
Next

Range("V1") = "On Restricted List"
Range("W1") = "Export Variant"
Range("X1") = "On Promo List"
Range("Y1") = "Restriction Begins"

Range("V2:Y500").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
Selection.Replace What:="#N/A", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
Selection.Replace What:="1/0/1900", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
Application.CutCopyMode = False
    
Range("W2:Y500").Select
Selection.Replace What:="0", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
Range("D1").Select
    
MsgBox ("Order Check is complete. Please review results.")

End Sub


这篇关于运行时错误1004应用程序定义或对象定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆