0

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

  1. Find the first and last instance of the value in a column
  2. Copy from the first instance down to the last instance and all the way to the Right
  3. 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
7
  • Did you consider using Find, specifying the After parameter? Commented May 18, 2021 at 18:59
  • 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 do id=. Commented May 18, 2021 at 19:05
  • After you have the first and second instance (from Range.Find like BigBen suggested), you can define your finished range like With wb.ws_data: Set data_rng = .Range(FirstInstance, .Cells(SecondInstance.Row,.Columns.Count)): End With which will get you the range you described in your post. Commented May 18, 2021 at 19:12
  • No I have not used Find before. Are these full answers? I would like to give credit where credit is due. Commented May 18, 2021 at 20:18
  • @JackArmstrong Is the data in the column sorted so that all the instances you are interested in are in a contiguous range? Commented May 18, 2021 at 20:20

1 Answer 1

1

What you could do is use Application.Match to find the row of the first instance then use Application.CountIf to count the no of occurrences of the search term.

With that information you should be able to get the subset of the data you want to copy using Resize.

Sub Button()

Dim wb As Workbook
Dim ws_temp, ws_data As Worksheet
Dim data_rng As Range
Dim id As Long
Dim lngFirstRow As Long
Dim lngNoRows As Long

    Application.ScreenUpdating = False
    
    Set wb = ThisWorkbook
    Set ws_temp = wb.Sheets("Template")
    Set ws_data = wb.Sheets("data")
    
    ' get search id
    id = ws_temp.Range("B4").Value

    ' find row of first occurence of id
    lngFirstRow = Application.Match(id, ws_data.Columns(2), 0)
    
    ' find count of id
    lngNoRows = Application.CountIf(ws_data.Columns(2), id)
    
    Set data_rng = ws_data.Range("B" & lngFirstRow).Resize(lngNoRows, 14)
      
    'Copy data_rng
    data_rng.Copy ws_temp.Range("A8")    'the data will always go in cell A8 on the Template sheet

    Application.ScreenUpdating = True
    
End Sub
Sign up to request clarification or add additional context in comments.

6 Comments

There is a type mismatch error when finding row of first occurence of id. I think this is cause it returns a double not a long
Is the value you are looking for definitely in column A on the sheet 'data'? Is the value from B4 on 'Template' the same data type as the values in column A on 'data'?
The value I am looking for is on 'data' in column B. I need to return columns B-O or A-O. I can live without returning column A. I am receiving an error when setting lngFirstRow as Error 2042. I have declared them as Variant's, but still receive mismatch error.
I'll update the code to look in column B rather than column A.
I see what is happening. So it does select the data from column B, but it does not copy and paste it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.