+ Reply to Thread
Results 1 to 4 of 4

Match column between two sheets and copy row format once column number matches

Hybrid View

  1. #1
    Registered User
    Join Date
    07-09-2010
    Location
    Delhi NCR, India
    MS-Off Ver
    Excel 2003
    Posts
    46

    Match column between two sheets and copy row format once column number matches

    Hello,

    Can someone look at the below code and tell me where I am making mistake. I have written this code to match a column between two tabs "Main" and "Backup". If the column number matches, the code should copy the row formatting of that column number from "Backup" sheet to "Main" sheet.

    Currently, it is copying the formatting from "Main" sheet to "Main" sheet itself. Dont know where I am making mistake. Thanks.

    Sub Rowformat()
    
    Dim Sht1Rng As Range
    Dim Sht2Rng As Range
    Dim D As Range
    
    'Compares the ID cell in both worksheets to each other
    
    Set Sht1Rng = Worksheets("Main").Range("B6", Worksheets("Main").Range("B65536").End(xlUp))
    
    Set Sht2Rng = Worksheets("Backup").Range("B6", Worksheets("Backup").Range("B65536").End(xlUp))
    
    For Each B In Sht1Rng
    Set D = Sht2Rng.Find(B.Value, LookIn:=xlValues)
    
    'If same value found in col B of "Backup" sheet then copy
    If Not D Is Nothing Then
                
                Sheets("Backup").Select
                Range(Cells(D.Row, 1), Cells(D.Row, 58)).Copy
                
                Sheets("Main").Select
                Range(Cells(D.Row, 1), Cells(D.Row, 58)).PasteSpecial xlPasteFormats
                            
                
    End If
    Set D = Nothing
    
    Next B
    
    End Sub
    Last edited by kapil; 07-30-2010 at 07:54 AM. Reason: made a small change in code

  2. #2
    Forum Contributor vikas.bhandari's Avatar
    Join Date
    04-07-2006
    Location
    Delhi, India
    MS-Off Ver
    Office 2007 and 2010
    Posts
    303

    Re: Match column between two sheets and copy row format once column number matches

    Hi there!!

    I tweaked your code a little, and it is working fine now. Please use the following code :


    
    Sub Rowformat()
    
    Dim Sht1Rng As Range
    Dim Sht2Rng As Range
    Dim D As Range
    Dim mainSht As Worksheet
    Dim backUpSht As Worksheet
    Dim rngToCopyTo As Range
    
    'Compares the ID cell in both worksheets to each other
    
    Set mainSht = Worksheets("Main")
    Set backUpSht = Worksheets("Backup")
    
    Set Sht1Rng = Worksheets("Main").Range("B6", Worksheets("Main").Range("B65536").End(xlUp))
    
    Set Sht2Rng = Worksheets("Backup").Range("B6", Worksheets("Backup").Range("B65536").End(xlUp))
    
    For Each B In Sht1Rng
    Set D = Sht2Rng.Find(B.Value, LookIn:=xlValues)
    
    'If same value found in col B of "Backup" sheet then copy
    If Not D Is Nothing Then
                
                'Sheets("Backup").Select
                backUpSht.Range(backUpSht.Cells(D.Row, 1), backUpSht.Cells(D.Row, 58)).Copy
                
                'Sheets("Main").Select
                Set rngToCopyTo = mainSht.Range(mainSht.Cells(D.Row, 1), mainSht.Cells(D.Row, 58))
                rngToCopyTo.PasteSpecial xlPasteFormats
                            
                'B.Resize(, 58).Copy Worksheets("Main").Range(Cells(B.Row, 1), Cells(B.Row, 58).PasteSpecial XlPasteFormats
    
    End If
    Set D = Nothing
    
    Next B
    
    End Sub
    Thanks,
    Vikas

  3. #3
    Registered User
    Join Date
    07-09-2010
    Location
    Delhi NCR, India
    MS-Off Ver
    Excel 2003
    Posts
    46

    Re: Match column between two sheets and copy row format once column number matches

    Thank You Thank You Thank You so much Vikas.

    Really Appreciate your help

  4. #4
    Forum Contributor vikas.bhandari's Avatar
    Join Date
    04-07-2006
    Location
    Delhi, India
    MS-Off Ver
    Office 2007 and 2010
    Posts
    303

    Re: Match column between two sheets and copy row format once column number matches

    Anytime

    Please ensure to mark it solved!!!!!!

    Vikas

+ 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