I am trying to select a subset of data in a range and copy it to another sheet in Excel. My thought process was as follows
- Find the first and last instance of the value in a column
- Copy from the first instance down to the last instance and all the way to the Right
- Paste the range of data into my sheet
I am having trouble finding the first and last instance of the value in a column and setting the range to copy.
This is what I have so far:
Sub Button()
'Application.ScreenUpdating = False
Dim wb As Workbook
Dim ws_temp, ws_data As Worksheet
Dim data_rng As Range
Set wb = ThisWorkbook
Set ws_temp = wb.Sheets("Template")
Set ws_data = wb.Sheets("data")
'declare id
Dim id As Integer
Set id= ws_temp.Range("B4").Value 'this is where the value is stored
'find first instance of id in data.
' The data starts in cell A6 and extends to column O. The number of rows will be variable
'find last instance of pitcher id
'Set data_rng
Set data_rng = wb.ws_data.Range("first instance":O&"second instance")
'Copy data_rng
data_rng.Copy wb.ws_temp.Range("A8") 'the data will always go in cell A8 on the Template sheet
'Application.ScreenUpdating = True
End Sub
Find, specifying theAfterparameter?Set id=will give you an error. Only Object variables need to be Set. Value variables like Integers dont need to be Set. Remove Set, just doid=.With wb.ws_data: Set data_rng = .Range(FirstInstance, .Cells(SecondInstance.Row,.Columns.Count)): End Withwhich will get you the range you described in your post.