title: VBA Vlookup within loop
body: <p>The user enters usernames in inputboxes ("Enter username"), which appear in the first column from row 4 onwards (specified as cells (4+k-1,1) in the code). The number of usernames he enters corresponds to the number of users he indicates were active on a certain date, specified in the first inputbox ("How many users active on this date?"). </p>
  
  <p>Now, I want the next column (cells(4+k-1,2)) to execute a vlookup or similar function that takes the username in the first column (same row) and finds a corresponding value in a table in a different worksheet (same workbook!). The problem is that no matter what I try, I can't find a code that makes this work. My most simple attempt is presented in the code as follows, and returns a message "run time error 1004: application-defined or object-defined error".</p>
  
  <pre><code>Sub Makro4()
      Dim dates As Date
      dates = InputBox("Enter the date of activity in the format dd/mm/yyyy:.")  ' User chooses date
  
      Dim n As Integer
      Dim k As Integer
  
      n = InputBox("Number of users active on this date:.")
  
      For k = 1 To n + 1                                       'Inserts rows accoring to number of users active on specified date
          Rows(4).Insert Shift:=xlDown, _
                CopyOrigin:=xlFormatFromLeftOrAbove
      Next
  
      For k = 1 To n
          Cells(4 + k - 1, 1) = InputBox("Enter username")
          Cells(4 + k - 1, 2) = Application.WorksheetFunction.VLookup(ActiveWorkbook.Sheets("Users by date").Range(4 + k - 1, 1), ActiveWorkbook.Sheets("Userlist").Range("B:j"), 9, False)
      Next
  
      Range(Cells(4, 3), Cells(4 + n - 1, 3)) = dates ' blank row between each date
  
  End Sub
  </code></pre>