Sad Bills Fan
New Member
- Joined
- Nov 17, 2023
- Messages
- 3
- Office Version
- 365
- Platform
- Windows
Hello, first post. I'm trying to search a set of data on a sheet called Sold Data and match it to a variable from a cell in a sheet called priceComps. I've defined the sheets as ws and reportSheet resepctively. If there's a match i want to copy the entire line to the priceComps sheet. My problem is no matter what i do, it tries to copy from the priceComps sheet instead of the Sold Data sheet. I've used multiple ws.select and ws.activate commands but it still goes back to the wrong sheet.
VBA Code:
Private Sub cmdSearch_Click()
Dim wb As Workbook
Dim ws As Worksheet 'where the data is copied from
Dim reportSheet As Worksheet 'where the data is pasted to
Dim bedrooms As String
Dim bathrooms As String
Dim zip As String
Dim test As String
Dim finalRow As Integer 'the last row of the dataset (if more than 30,000 needs to be type long instead of integer)
Dim i As Integer 'row counter
Set wb = ThisWorkbook
Set ws = Worksheets("Sold Data")
Set reportSheet = Worksheets("priceComps")
'clear old data from report sheet
reportSheet.Range("A12:AA5000").ClearContents 'adjust if it passes 5,000 lines
bedrooms = reportSheet.Cells(2, 3).Value
bathrooms = reportSheet.Cells(4, 3).Value
zip = reportSheet.Cells(8, 3).Value
'go to datasheet and start searching and copying
finalRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To finalRow
test = ws.Cells(i, 9).Value
If bedrooms = test Then
ws.Select
Range(Cells(i, 1), Cells(i, 27)).Copy
ws.Select
'reportSheet.Select
Range("A5000").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
ws.Select
End If
Next i
reportSheet.Select
Range("B2").Select
MsgBox ("Search Complete")
End Sub