+ Reply to Thread
Results 1 to 7 of 7

Need to compare 1 row in two worksheets and delete entire column if a match not found

  1. #1
    Registered User
    Join Date
    05-18-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003
    Posts
    59

    Need to compare 1 row in two worksheets and delete entire column if a match not found

    Hi all,

    I would like to create a vba script to check and delete all the columns (in sheet1) that do not exist in sheet2

    For example sheet1 may have columns:

    _______A___B___C_____D______E____F___G___H
    row 4: This | is | an | example | of | what | I | want

    and sheet2 may have columns:
    _______A____B___C__D
    row 2: This | an | of | I

    so as you an see, in sheet2.rows(2), the words "is, example, what, want" do not exist, so I would like to delete the columns "B,D,F,H" from sheet1.rows(4) so that the resulting sheet1 will only have columns that sheet2 has.

    *also note that i am comparing two different rows in sheet1 and sheet2

    Does anyone know how to do this?

    Thanks in advanced!
    Jason
    Last edited by jasondu; 05-22-2012 at 11:52 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    02-09-2012
    Location
    Mauritius
    MS-Off Ver
    Excel 2007
    Posts
    1,055

    Re: Need to compare 1 row in two worksheets and delete entire column if a match not found

    Hi

    Upload sample file
    Click *, if my suggestion helps you. Have a good day!!

  3. #3
    Registered User
    Join Date
    05-18-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003
    Posts
    59

    Re: Need to compare 1 row in two worksheets and delete entire column if a match not found

    Hi, I have created a sample file if you have any questions please do not hesitate to ask

    Jason
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    05-18-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003
    Posts
    59

    Re: Need to compare 1 row in two worksheets and delete entire column if a match not found

    does anyone have a solution for this?

  5. #5
    Registered User
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,164

    Re: Need to compare 1 row in two worksheets and delete entire column if a match not found

    Will row 4 in sheet1 always be compared with row2 in sheet2? What about the other rows?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  6. #6
    Registered User
    Join Date
    05-18-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003
    Posts
    59

    Re: Need to compare 1 row in two worksheets and delete entire column if a match not found

    row 4 will always be compared with row 2 yes, other rows dont matter i just need to find 1 instance if text exists on both sheets then leave the column in sheet1, if not the delete the entire column

  7. #7
    Registered User
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,164

    Re: Need to compare 1 row in two worksheets and delete entire column if a match not found

    Jasondu,

    Use this code
    Please Login or Register  to view this content.
    Ensure that whatever currency formatting you have is removed and change it to general. If not, the code will not work.

    Copy the Excel VBA code
    Select the workbook in which you want to store the Excel VBA code
    Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
    Choose Insert | Module
    Where the cursor is flashing, choose Edit | Paste

    To run the Excel VBA code:
    Choose Tools | Macro | Macros
    Select a macro in the list, and click the Run button.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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