+ Reply to Thread
Results 1 to 8 of 8

How to apply the codes for 2 columns in For each... in...

Hybrid View

  1. #1
    Registered User
    Join Date
    07-01-2015
    Location
    Singapore
    MS-Off Ver
    2013
    Posts
    66

    How to apply the codes for 2 columns in For each... in...

    I want to apply the code below for Column T without Copy the same codes.
    Apply for both Column S and Column T

    Any way to do this ?
    Thanks in advance .


    For Each rng In Wkb1.ActiveSheet.UsedRange.Columns("S").Cells
            str = WorksheetFunction.Vlookup(rng.Value, Wkb2.ActiveSheet.UsedRange.Columns("J:P"), 7, False)
            str2 = WorksheetFunction.Vlookup(rng.Value, Wkb2.ActiveSheet.UsedRange.Columns("J:N"), 5, False) 'reject column
                    
            If Err.Description = "" Then ' ID found...
                    If str = "Open A/R" Or str = "Merged" Then
                        rng.Interior.Color = RGB(0, 0, 255) ' blue
                    Else
                        rng.Interior.Color = RGB(0, 255, 0) ' green
                    End If
                    
                 ' Reject column
                    If str2 = "2" Then
                        rng.Interior.Color = RGB(255, 255, 0) ' yellow
                    Else
                    'do nothing
                    End If
            Else ' no ID found...
                Err.Clear
            End If
        Next rng
    Last edited by fluffyvampirekitten; 07-31-2015 at 01:28 AM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,449

    Re: How to apply the codes for 2 columns in For each... in...

    Try changing
    For Each rng In Wkb1.ActiveSheet.UsedRange.Columns("S")Cells
    to
    For Each rng In Wkb1.ActiveSheet.UsedRange.Columns("S:T")Cells

    See what happens.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    07-01-2015
    Location
    Singapore
    MS-Off Ver
    2013
    Posts
    66

    Re: How to apply the codes for 2 columns in For each... in...

    Quote Originally Posted by MarvinP View Post
    Try changing
    For Each rng In Wkb1.ActiveSheet.UsedRange.Columns("S")Cells
    to
    For Each rng In Wkb1.ActiveSheet.UsedRange.Columns("S:T")Cells

    See what happens.
    I tried using this method but it doesn't work.

  4. #4
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: How to apply the codes for 2 columns in For each... in...

    Hi there,

    Assuming the above code works, make this line of code the very first line in your above procedure...

    For Each varMyCol In Array("S", "T")
    ...and this line the very last piece of code in your above procedure:

    Next varMyCol
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  5. #5
    Registered User
    Join Date
    07-01-2015
    Location
    Singapore
    MS-Off Ver
    2013
    Posts
    66

    Re: How to apply the codes for 2 columns in For each... in...

    Quote Originally Posted by Trebor76 View Post
    Hi there,

    Assuming the above code works, make this line of code the very first line in your above procedure...

    For Each varMyCol In Array("S", "T")
    ...and this line the very last piece of code in your above procedure:

    Next varMyCol
    Regards,

    Robert
    How to do this ?

    For Each rng In Wkb1.ActiveSheet.UsedRange.Columns("S")Cells

  6. #6
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: How to apply the codes for 2 columns in For each... in...

    Try this:

    Sub Macro2()
    
        For Each varMyCol In Array("S", "T")
            For Each Rng In Wkb1.ActiveSheet.UsedRange.Columns(varMyCol).Cells
            
                On Error Resume Next
                    str1 = WorksheetFunction.VLookup(Rng.Value, Wkb2.ActiveSheet.UsedRange.Columns("J:P"), 7, False)
                    str2 = WorksheetFunction.VLookup(Rng.Value, Wkb2.ActiveSheet.UsedRange.Columns("J:N"), 5, False) 'reject column
                On Error GoTo 0
                
                If Err.Description = "" Then ' ID found...
                    If str1 = "Open A/R" Or str1 = "Merged" Then
                        Rng.Interior.Color = RGB(0, 0, 255) ' blue
                    Else
                        Rng.Interior.Color = RGB(0, 255, 0) ' green
                    End If
                         
                    'Reject column
                    If str2 = "2" Then
                        Rng.Interior.Color = RGB(255, 255, 0) ' yellow
                    Else
                        'do nothing
                    End If
                Else 'no ID found...
                    Err.Clear
                End If
            Next Rng
        Next varMyCol
        
    End Sub

  7. #7
    Registered User
    Join Date
    07-01-2015
    Location
    Singapore
    MS-Off Ver
    2013
    Posts
    66

    Re: How to apply the codes for 2 columns in For each... in...

    Quote Originally Posted by Trebor76 View Post
    Try this:

    Sub Macro2()
    
        For Each varMyCol In Array("S", "T")
            For Each Rng In Wkb1.ActiveSheet.UsedRange.Columns(varMyCol).Cells
            
                On Error Resume Next
                    str1 = WorksheetFunction.VLookup(Rng.Value, Wkb2.ActiveSheet.UsedRange.Columns("J:P"), 7, False)
                    str2 = WorksheetFunction.VLookup(Rng.Value, Wkb2.ActiveSheet.UsedRange.Columns("J:N"), 5, False) 'reject column
                On Error GoTo 0
                
                If Err.Description = "" Then ' ID found...
                    If str1 = "Open A/R" Or str1 = "Merged" Then
                        Rng.Interior.Color = RGB(0, 0, 255) ' blue
                    Else
                        Rng.Interior.Color = RGB(0, 255, 0) ' green
                    End If
                         
                    'Reject column
                    If str2 = "2" Then
                        Rng.Interior.Color = RGB(255, 255, 0) ' yellow
                    Else
                        'do nothing
                    End If
                Else 'no ID found...
                    Err.Clear
                End If
            Next Rng
        Next varMyCol
        
    End Sub
    I got this run-time error '91':
    Object variable or With block variable not set.
    Then this was highlighted
    rng.Interior.Color = RGB(0, 255, 0) ' green

  8. #8
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: How to apply the codes for 2 columns in For each... in...

    Like I said at the beginning - my examples are based on your code working

    From that error message it would seem that the rng variable hasn't been set to anything.

    Please don't quote entire threads - only specific parts to support your request.

    Good luck with it.

    Robert

+ 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. Replies: 0
    Last Post: 02-05-2014, 12:34 AM
  2. [SOLVED] APPLY Header Codes
    By diakonos1984 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-03-2013, 03:48 PM
  3. [SOLVED] How to transpose columns to row using vba codes
    By ricardo2012 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-04-2012, 04:55 PM
  4. I have two columns (A) and (G) that have like product codes however
    By genesisoxygen@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-07-2006, 01:05 PM
  5. I have two columns (A) and (G) that have like product codes however
    By genesisoxygen@gmail.com in forum Excel General
    Replies: 1
    Last Post: 08-04-2006, 03:25 PM
  6. I have two columns (A) and (G) that have like product codes however
    By genesisoxygen@gmail.com in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-04-2006, 03:25 PM
  7. Replies: 0
    Last Post: 04-28-2006, 01:00 PM

Tags for this Thread

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