+ Reply to Thread
Results 1 to 17 of 17

VBA to Find and Replace Using a Lookup Table - But only Certain Columns/Multiple Sheets

  1. #1
    Forum Contributor
    Join Date
    05-04-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    125

    VBA to Find and Replace Using a Lookup Table - But only Certain Columns/Multiple Sheets

    Hi there, I'm looking for help with modification of the following code.

    The code below replaces certain values on a datasheet (SydneyOut) after looking at the values in a lookup table(Values), which is on a seperate tab..

    The issue I have is that I need the code to only replace the values in columns that I choose, and there will be multiple, C:C, F:F etc. Also there will be multiple sheets so it would be ideal if It could rename them all (but chances are the columns won't be the same on each sheet, but if not I could create a seperate command button for each area. (this is Sydney).

    For example on sheet SydneyOut out it could be columns C;C, F:F, but on Brisbane it could be D:D, G:G.Mailtracker2.xlsm

    I've tried to define the range but my I couldn't get them defined.

    Any help would be appreciated, I've attached a sample file.

    Thanks

    Dave




    Formula: copy to clipboard
    Please Login or Register  to view this content.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA to Find and Replace Using a Lookup Table - But only Certain Columns/Multiple Sheet

    Sometimes VBA is overkill. A VLOOKUP() formula will accomplish what you want, in just a few seconds.

    1) In BM2: =VLOOKUP(C2, Values!$A:$B, 2, 0)
    2) Doubleclick the lower right corner of BM2 to copy down the whole data set
    3) Copy BM2:BM161
    4) Click on C2 and select Paste Special > Values
    5) Clear column BM, or repeat for column F.

    If you might have blank cells in the column and don't want the N/A error, use this:


    Now, if you really want a macro, click on a cell in any column on any sheet and run this macro to do the same thing:

    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA to Find and Replace Using a Lookup Table - But only Certain Columns/Multiple Sheet

    If you don't want the tediumn of clicking on a cell before running the macro, it could also be tweaked to go across every cell in row1 and run itself on any column that has "Destination" in the header.

  4. #4
    Forum Contributor
    Join Date
    05-04-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    125

    Re: VBA to Find and Replace Using a Lookup Table - But only Certain Columns/Multiple Sheet

    Hi JBeaucaire, thanks very much for replying.

    In terms of context I won't be the end user of the workbook, and the people that will won't be skilled enough to use vlookups unfortunately. The data that looking to manipulate is being exported from an 4 access 2003 databases (8 tables in total) using the following code for each table

    Please Login or Register  to view this content.
    The user will have a command button for each table (there are 8 in total) and exporting to an excel workbook.

    Unfortunately the data in the access tables is referenced to a lookup table, so the data I'll get in the excel sheet won't have the true values (locations), and hence why I'll need to replace these in each of the 8 sheets.

    The macro I was aiming to get would enable the end user to press one command button to replace the values in certain columns C/F/G etc across all 8 sheets without having to highlight cells or really need to manipulate any of the data if that makes sense.

    That's why I thought it would be easier to manipulate the code to only search, replace in certain columns across multiple sheets.

    Thanks

    David
    Last edited by JBeaucaire; 11-10-2013 at 06:47 PM. Reason: Added Code tags AROUND the code, check my signature for an example.

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA to Find and Replace Using a Lookup Table - But only Certain Columns/Multiple Sheet

    What about my idea? Search every sheet and for each column with the word "Destination" in the header, run my macro on that column?
    Last edited by JBeaucaire; 11-10-2013 at 11:14 PM.

  6. #6
    Forum Contributor
    Join Date
    05-04-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    125

    Re: VBA to Find and Replace Using a Lookup Table - But only Certain Columns/Multiple Sheet

    Hi JBeaucaire, while the macro works it need to be run once for each column. So that's 20 columns on each sheet with a total of 8 sheets, 160 individual runs unless I'm the macros can somehow run across each sheet once? Is that possible?

    Cheers

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA to Find and Replace Using a Lookup Table - But only Certain Columns/Multiple Sheet

    Dave, I've asked the question twice with no answer yet. I'm trying to find a way to teach the macro to find the columns where it needs to run ON ITS OWN, without you needing to teach it specifically. It looks like the columns where you want to insert these city names are all called DestinationX in the row1 title row.

    Am I correct? Could we do it that way? Run the macro on every column with "Destination" in the header?

    Before you offer any other information, please answer that question.

  8. #8
    Forum Contributor
    Join Date
    05-04-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    125

    Re: VBA to Find and Replace Using a Lookup Table - But only Certain Columns/Multiple Sheet

    Hi JBeaucaire, sorry I didn't pick up on that, the header in row1 will always have one of two words, "Destination" is the first and "From" is the second, these will never be together but as the macros will be used on an output from two access tables, with one being a mail out table (Destination) and the other being mail received (From).

    So yes it could run on it's own if it looked for either Destination or From.

    Thanks

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA to Find and Replace Using a Lookup Table - But only Certain Columns/Multiple Sheet

    Are the 8 sheets always named the same thing? Or do we need to run it on all sheets in the workbook EXCEPT Values and Values2?

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA to Find and Replace Using a Lookup Table - But only Certain Columns/Multiple Sheet

    This version will run on every sheet in the workbook that doesn't have "Value" in the sheet name:

    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 11-12-2013 at 10:59 AM. Reason: Correction

  11. #11
    Forum Contributor
    Join Date
    05-04-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    125

    Re: VBA to Find and Replace Using a Lookup Table - But only Certain Columns/Multiple Sheet

    Hi JBeaucaire,

    I've run the macro across the first sheet and it works perfectly, however when I start with two sheets (second being a copy of the original sheet with the name Brisbaneout) it works on the first but clears the contents of all cells in the columns where it should be pasting the place names in the second sheet.

    Infact if I run it on the Brisbane sheet first it clears the values, then when I run it on the Sydney sheet it also clears the values.

    Thanks

    David

    Very strange

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA to Find and Replace Using a Lookup Table - But only Certain Columns/Multiple Sheet

    I'm certainly willing to look at the troublesome workbook. Zip it up and attach it here.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

  13. #13
    Forum Contributor
    Join Date
    05-04-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    125

    Re: VBA to Find and Replace Using a Lookup Table - But only Certain Columns/Multiple Sheet

    Hi JBeaucaire, I'll upload the file tonight, my network doesn't seem to want me to upload the file now.

    Thanks for your continued efforts.

    Thanks

    David

  14. #14
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA to Find and Replace Using a Lookup Table - But only Certain Columns/Multiple Sheet

    oops! I left an "Activesheet" reference in the new macro in post #10. I've highlighted and corrected that line of code. That error would cause exactly the kind of error you experienced. Try it with that correction.

  15. #15
    Forum Contributor
    Join Date
    05-04-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    125

    Re: VBA to Find and Replace Using a Lookup Table - But only Certain Columns/Multiple Sheet

    HI JBeaucaire, I've retested the code and it seems to work perfectly.

    Thanks for your efforts in helping me to get this resolved.

    Cheers

    David

  16. #16
    Forum Contributor
    Join Date
    05-04-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    125

    Re: VBA to Find and Replace Using a Lookup Table - But only Certain Columns/Multiple Sheet

    JBeaucaire, just wondering if you could assist re the code.

    In a new worksheet it works perfectly, but I've tried to integrate it into an existing sheet (saved as .xlsx and now .xlsm) but possibly created in an older version of excel, and I get an error at the following line of code:
    Please Login or Register  to view this content.
    If InStr(.Cells(1, Col), "Destination") > 0 Or InStr(.Cells(1, Col), "From") > 0 Then
    Please Login or Register  to view this content.
    The error says Error 13 - Type Mismatch

    When I hover over the line it says InStr(.Cells(1, Col), "Destination") = Type Mismatch

    .Cells(1, Col) = Error 2023 and then Col 3 further along

    The sample data is the same across both workbooks, one works perfectly and the other has the error. All tabs are named identical.

    Any suggestions?

    Thanks

    David

  17. #17
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA to Find and Replace Using a Lookup Table - But only Certain Columns/Multiple Sheet

    I would have to see the macro and the troublesome workbook together.

+ 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. Multiple find and replace for multiple sheets
    By geedaigo in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-25-2013, 03:05 PM
  2. [SOLVED] Lookup Table to Find/Replace Every Instance of a String (2/2)
    By Baghel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-04-2013, 05:49 AM
  3. Replies: 12
    Last Post: 12-31-2012, 04:13 AM
  4. Find and Replace in multiple sheets
    By Josh_123456 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-11-2011, 05:29 PM
  5. Find and Replace in multiple sheets
    By Excel Newbie05 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-06-2008, 04:34 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