Consulting

Results 1 to 2 of 2

Thread: vba Double for each Loop combining task

  1. #1
    Banned VBAX Contributor
    Joined
    Aug 2017
    Posts
    144
    Location

    vba Double for each Loop combining task

    Hi Team,

    I have two for each loop , is it possible to combine it. to shorten the code.


    For Each ws in wbk1.Worksheets
        ws.Columns("A").NumberFormat = "General"
        ws.Columns("A").TextToColumns , xlDelimited, , , , , False, False, False
      Next ws
    
    
    For Each ws In wbk1.Worksheets 
            Select Case ws.Name 
            Case "Sheet1", "Sheet2", "Sheet4", "Sheet6" 
                ws.Range("B1").EntireColumn.Insert 
                 ws.Range("B1").Value = "Historic/New" 
                lr = ws.Cells(Rows.Count, "A").End(xlUp).Row 
                ws.Range("B2:B" & lr).Formula = "=IF(ISERROR(VLOOKUP(A2,'[" & wbk2.Name & "]Sheet1'!$A$1:$A$10000,1,False)),""New"",""Historic"")"
            End Select 
     Next ws
    Last edited by Bob Phillips; 08-21-2017 at 01:34 PM. Reason: Added code tags

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
        For Each ws In wbk1.Worksheets
            
            With ws
            
                With .Columns("A")
                
                    .NumberFormat = "General"
                    .TextToColumns , xlDelimited, , , , , False, False, False
                End With
            
                Select Case .Name
                    Case "Sheet1", "Sheet2", "Sheet4", "Sheet6"
                        .Range("B1").EntireColumn.Insert
                        .Range("B1").Value = "Historic/New"
                        lr = .Cells(Rows.Count, "A").End(xlUp).Row
                        .Range("B2:B" & lr).Formula = "=IF(ISERROR(VLOOKUP(A2,'[" & wbk2.Name & "]Sheet1'!$A$1:$A$10000,1,False)),""New"",""Historic"")"
                End Select
            End With
        Next ws
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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