I have a userform where the user can input the desired worksheet to search (representing the year) and the name of a company to search and the active worksheet is a "Report" worksheet. I can't find a way to tell VBA to search that specific worksheet range... Have the following code and any help would be appreciated!
Private Sub CommandButton1_Click()
Dim Name As String
Dim num As Integer
Dim finalrow As Integer
Dim i As Integer
Dim sht As String
Dim ws As Worksheet
Dim LookRange As Range
'clear report cells
Sheets("Report").Range("A2:E1000").ClearContents
'Set variables
sht = ComboBox1.Value
Name = ComboBox2.Value
finalrow = Cells(Rows.Count, 1).End(xlUp).Row
num = 0
Set ws = ThisWorkbook.Sheets(sht)
'Go to worksheet sht
With ws
Set LookRange = Range(.Cells(2, 3).End(xlUp).Row)
'Loop through Data
'This is working fine if I am executing the code in the worksheet that I want to search
For i = 2 To finalrow
'Find matching results
If .Cells(i, 3) = Name Then
'Count results to display
num = num + 1
'Copy results and paste them on results cells (G10:K1000)
Range(.Cells(i, 1), .Cells(i, 5)).Copy
Worksheets("Report").Range("A1000").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
End If
Next i
End With
End Sub
Bookmarks