+ Reply to Thread
Results 1 to 9 of 9

Find value in Workbook2 from value in Workbook 1

  1. #1
    Registered User
    Join Date
    05-12-2020
    Location
    Wales
    MS-Off Ver
    365
    Posts
    82

    Find value in Workbook2 from value in Workbook 1

    I wonder if someone could help with the following.

    I've defined a value in a workbook where the code is being executed. The destination workbook is then opened on the relevant worksheet. I'm getting the following error when it comes to finding the value.

    Code so far.

    Please Login or Register  to view this content.
    I'm getting this error,

    Run-time error '1004':
    Activate method of Range class failed
    At this point,


    Please Login or Register  to view this content.
    I don't know why?

    I have posted to another forum, as below but have not received any responses.

    https://www.mrexcel.com/board/thread.../#post-6157023

  2. #2
    Registered User
    Join Date
    05-12-2020
    Location
    Wales
    MS-Off Ver
    365
    Posts
    82

    Re: Find value in Workbook2 from value in Workbook 1

    I have recorded a Macro from one Workbook to the other, which provides the following code,

    Please Login or Register  to view this content.
    When I incorporate it into my code it throws up the following error,

    Run-time error:
    Application-defined or object-defined error
    The only additional line I have is the following, to allow for opening the "Test tracker" Workbook if closed.

    Please Login or Register  to view this content.
    Last edited by AndyEd; 02-22-2024 at 12:05 PM.

  3. #3
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,612

    Re: Find value in Workbook2 from value in Workbook 1

    Fast answers need visual help. Please read the yellow banner at the top of this page on how to attach a file and a mocked up solution.
    Ben Van Johnson

  4. #4
    Registered User
    Join Date
    05-12-2020
    Location
    Wales
    MS-Off Ver
    365
    Posts
    82

    Re: Find value in Workbook2 from value in Workbook 1

    Sorry...sample workbooks attached.
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,223

    Re: Find value in Workbook2 from value in Workbook 1

    I've read this thread before and began to suspect that the code was run from the sheet module. And it looks like I wasn't wrong.
    A lot of errors are in this code.
    Only in this line:
    Please Login or Register  to view this content.
    I counted as many as 5 of them.
    As I mentioned earlier, the code is run from the sheet module. The lack of full qualifiers causes the machine to interpret Cells as an object in this sheet. That is, an attempt is made to search for the Reference value (that is, 1) in the Memo sheet, starting from the active cell (but in the Tracker sheet!) and if the value was found it is supposed to activate the found cell in the Memo sheet when the Tracker sheet is active. You may have to read the previous sentence multiple times to understand what is happening.
    In addition, you should not look for the fragment in the cell values, but the value of the whole cell. So you can't use xlPart. If the searched value was not found, the Activate method would cause an error.

    A more correct code could look like this:
    Please Login or Register  to view this content.
    Note also that the code does not use cell C5.

    Artik
    Last edited by Artik; 02-22-2024 at 09:13 PM.

  6. #6
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,237

    Re: Find value in Workbook2 from value in Workbook 1

    Untitled.png

    Please Login or Register  to view this content.
    Last edited by sintek; 02-23-2024 at 08:46 AM.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  7. #7
    Registered User
    Join Date
    05-12-2020
    Location
    Wales
    MS-Off Ver
    365
    Posts
    82

    Re: Find value in Workbook2 from value in Workbook 1

    Thanks both.

    I'll have good a proportion of it to work out how it works.

    I've gone with sintek' solution as it contains the least amount of code.

    I don't know what people like me would do without people like you.

  8. #8
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,237

    Re: Find value in Workbook2 from value in Workbook 1

    ................................
    THANKS.gif

  9. #9
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,223

    Re: Find value in Workbook2 from value in Workbook 1

    Quote Originally Posted by AndyEd View Post
    I've gone with sintek' solution as it contains the least amount of code.
    Aha, so it's supposed to be as short as possible.
    Here you go
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    However, programming is not about coming up with the shortest code possible, because not every shorter code executes for less time. It all depends on the methods used.
    In addition, often shorter code is unreadable and more difficult to modify. This remark just doesn't apply to your case. Sintek wrote basically the same code as I did, only shorter. As you can see, you can still be shorter.
    Only that these short codes will someday not work properly. There may not be values or they will be wrong in cells B3 or B5, the "Test Tracker" workbook may not be in the defined location, the "Trucker" sheet may have a different name or not be the first sheet in the workbook, the target sheet may be protected, etc, etc.
    My "long" code at least somewhat protects the execution from unexpected error. No, it's not perfect, but it's safer.
    So in the future, don't look at whether the code is short, you just need to analyze it in terms of the method used (whether it is the best you know) and whether the code is protected from unexpected errors. Many times it happens that security features take up more code than the main task to be performed.

    Artik
    Last edited by Artik; 02-25-2024 at 08:33 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Copy Range from Workbook2 to Workbook Problem
    By chasoe in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 07-21-2023, 05:38 AM
  2. Replies: 0
    Last Post: 10-29-2022, 04:40 PM
  3. [SOLVED] Copy rows from Workbook 1 to Workbook2 if condition is met
    By Xsample in forum Excel Programming / VBA / Macros
    Replies: 24
    Last Post: 03-01-2018, 10:24 AM
  4. Find value from Workbook1 in Workbook2, HOffset and copy range data
    By Dill Pickle in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-17-2016, 09:46 PM
  5. How to bring data from workbook1 to workbook2 by comparing values of workbook2
    By amethystfeb in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-04-2013, 09:38 AM
  6. Workbook1 should start Workbook2
    By KausBorealis in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-03-2012, 10:52 PM
  7. Data from workbook1 to workbook2 every week but in different place in workbook2
    By cheelie in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-29-2007, 06:26 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1