• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Headers for loops

jassybun

Member
Hello amazing people, I am learning so much, thank you - I have another question - how to I add a header name to each of these loops? altogether I am creating 4 columns but they don't have header names.

Screen Shot 2018-12-20 at 13.32.42.png
Instructions: https://chandoo.org/forum/threads/posting-a-sample-workbook.451/

Code:
Sub Lookup1()
  Dim rng  As Range
  Dim c  As Range
  Dim result  As Variant

  Set rng = Range("B2:B" & Cells(Rows.Count, 2).End(xlUp).Row)

  For Each c In rng.Cells
  result = Application.WorksheetFunction.VLookup(c.Value, Workbooks("Lookup1.xlsx").Worksheets("Sheet1").Range("A2:C350"), 2, False)
  If IsError(result) Then
  c.Offset(, 12) = "Not Found"
  Else
  c.Offset(, 12).Value = result
  End If
  Next c

  For Each c In rng.Cells
  result = Application.WorksheetFunction.VLookup(c.Value, Workbooks("Lookup1.xlsx").Worksheets("Sheet1").Range("A2:C350"), 3, False)
  If IsError(result) Then
  c.Offset(, 13) = "Not Found"
  Else
  c.Offset(, 13).Value = result
  End If
  Next c

For Each c In rng.Cells
  result = Application.WorksheetFunction.VLookup(c.Value, Workbooks("Lookup1.xlsx").Worksheets("Sheet1").Range("A2:d350"), 4, False)
  If IsError(result) Then
  c.Offset(, 14) = "Not Found"
  Else
  c.Offset(, 14).Value = result
  End If
  Next c

  With rng.Offset(, 15)
  .Value = Evaluate("IF(" & .Offset(, -13).Address & "-(" & .Offset(, -14).Address & "+TIMEVALUE(" & .Offset(, -5).Address & ")+IF(" & .Offset(, -4).Address & "=""PM"",0.5,0))>1,""Yes"",""No"")")
  End With

End Sub
 
Last edited by a moderator:
I am uploading a sample file, also you will need to have both files open for the "command button" to work.
 

Attachments

  • New folder.zip
    24.2 KB · Views: 4
How about adding this to the beginning or end of your code
Code:
  Range("N1:Q1").Value = Array("Title1", "Title2", "Title3", "Title4")
 
Back
Top