VBA: I get the following error: run-time error '1004' Application-defined or object-defined error

南笙酒味 提交于 2020-01-06 20:01:14

问题


Current issue:

My code used to work partially until I add extra data to sheet2, now when it fails and debug sends me to:

Set partsheet = Worksheets("Sheet1").Range("A1", Range("A65536").End(xlUp)) 

Utility of this code:

I basically have two columns in different sheets: that I want to compare & when similar import data:

Sheet1 - column1 is a job number

Sheet1 - column2 is a part number

for the same job number you can have multiple parts - meaning the job number repeats itself in column1

Sheet2 - column1 is the same job numbers as sheet1 but the data is never repeat

Sheet2 - column2 is an id number specific to each job number (hence why in this case job numbers do not repeat themselves)

Therefore I want to import the data from Sheet2 - column2 using the commun job numbers as reference point. When I import the data to Sheet1 - column3 the unique id number will have to repeat itself for job numbers in column1(sheet1) that are repeated - hence the double for loop.

What I mean by used to partially work:

To start with the code only partially worked, I think this was due to the fact that certain job numbers in sheet1 - column1 have no corresponding value in sheet2 - column1 (the data isnt perfect). Maybe I need to add code for the else part of the if statement but im not sure what.

vba Code:

Option Explicit

Sub testFil()
    Dim Jobref_in_partsheet As Range
    Dim Jobref_in_jobsheet As Range
    Dim partsheet As Range
    Dim jobsheet As Range
    Dim MyRow As Long
    Dim MyOtherRow As Long

    Set partsheet = Worksheets("Sheet1").Range("A1", Range("A65536").End(xlUp))
    Set jobsheet  = Worksheets("Sheet2").Range("A1", Range("A65536").End(xlUp))

    For Each Jobref_in_partsheet In partsheet
        For Each Jobref_in_jobsheet In jobsheet
            If Jobref_in_partsheet.Value = Jobref_in_jobsheet.Value Then
                MyRow = Jobref_in_partsheet.Row
                MyOtherRow = Jobref_in_jobsheet.Row
                Worksheets("Sheet1").Cells(MyRow, 3) = Worksheets("Sheet2").Cells(MyOtherRow, 2)
            Else
                 'Do nothing
            End If
        Next
    Next
End Sub

I'd really appreciate any help I could get.


回答1:


Worksheets("Sheet1").Range("A1", Range("A65536").End(xlUp))

There is a problem here: you are trying to create a Range object, first cell of which is A1 of the Sheet1, and last cell is the last cell used in column A in the active sheet. As you can guess, Sheet1 and active sheet can be different sheets, and you can't create a range that belongs multiple sheets.

You should provide the parent sheet explicitly for your last cell:

Worksheets("Sheet1").Range("A1", Worksheets("Sheet1").Range("A65536").End(xlUp))

Using .Address is not really a workaround, as it might result in address fetched from one sheet to be used against another sheet.




回答2:


Answer Update:

I think this might be more easily accomplished using =VLOOKUP, which is just a normal excel function. Basically you'll want to apply this formula to every cell in Sheet1 row C.

'Psudeo Code
=VLOOKUP([look up value] A1, [range to look in] Sheet2!A:B, [row to return] 2, [use exact match] false)

You'll need to read up on VLOOKUP before using it, but hopefully my little bit of psuedo code can show you if you can actually use it for your purpose. If you implement it like I said then basically it will use the first value (A1) to look up something in another range (the first column of A:B). If it finds a match it will return what is in the second column (hence the 2, so column B of A:B). False is just to tell the program that you do not want matches that are similar, you only want matches that are exactly the same.

Yeah, VLOOKUP is confusing as all get out, but it's quite useful.

This will be a lot easier than using VBA to do the same task. I suppose that may or may not actually answer your question, but I thought I'd bring it up just incase you hadn't considered it.

Old, wrong, answer:

I tend to make sure I turn everything to strings when creating a range. EG:

Set partsheet = Worksheets("Sheet1").Range("B1:" & Range("B65536").End(xlUp).Address)

Range("B65536").End(xlUp).Address will return the string representing the address of the top of that column. IF, for example, the top was "B31" then "B1:" & Range("B65536").End(xlUp).Address should return "B1:B31" as the range.



来源:https://stackoverflow.com/questions/13975496/vba-i-get-the-following-error-run-time-error-1004-application-defined-or-obj

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!