I have the following code that I hoped would fill the values of the textboxes from a range. I first need to search for the column in a data sheet based on the value of a specific cell of another sheet. I need to then use the data in that column to fill the values of the textboxes. I thought I had worked it out but have received the following error I can't seem to figure out why. below is screenshot of the error and my code. Thank you.
Edit: I tried to upload screenshot but it didn't seem to work. The error is "Runtime error 1004: Application-defined or object-defined error"
Private Sub UserForm_Initialize()
Dim unit As Range
With Range("A1:G1")
Set unit = .Find(What:=ThisWorkbook.Worksheets("Shift Report").Range("A1"),
LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
End With
With ThisWorkbook.Worksheets("Data")
Dim cn As Long
cn = unit.column
Dim tbcounter As Long
tbcounter = 1
Dim rw As Long
For rw = 2 To 30
If .Range(cn & rw).Value <> "" And tbcounter <= 20 Then
Me.Controls("Textbox" & tbcounter).Text = .Range(cn & rw).Value
tbcounter = tbcounter 1
End If
Next rw
End With
End Sub
CodePudding user response:
I was given an answer and am trying to post it as answered. Here is the updated code that works.
Private Sub UserForm_Initialize()
Dim unit As Range
With ThisWorkbook.Worksheets("Data").Range("A1:G1")
Set unit = .Find(What:=ThisWorkbook.Worksheets("Shift Report").Range("A1"), LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
End With
With ThisWorkbook.Worksheets("Data")
Dim cn As Long
cn = unit.column
Dim tbcounter As Long
tbcounter = 1
Dim rw As Long
For rw = 2 To 30
If .Cells(rw, cn).Value <> "" And tbcounter <= 20 Then
Me.Controls("Textbox" & tbcounter).Text = .Cells(rw, cn).Value
tbcounter = tbcounter 1
End If
Next rw
End With
End Sub
