VBA to copy data displayed in a FILTER function?

helpneeded2

Board Regular
Joined
Jun 25, 2021
Messages
110
Office Version
  1. 365
Platform
  1. Windows
I am not very experienced with VBA, and I am trying to use code from another sheet I made in which it was coping only the data that is visible on the sheet.

Excel Formula:
Sub rangeSelection()
    Dim endOfRows As Long
    endOfRows = Range("A25003").Value
    Range("A4").Resize(endOfRows, 69).Select
    Selection.Copy
End Sub

On the new sheet I am creating, the sheet may have up to 25000 rows of data, across 69 columns. There will be no blanks rows of data in between the rows of data. The raw data pasted in will vary between 1000 rows to 25000 rows.

When I try to run the above formula I get an error message:
Excel Formula:
Run-time error '1004':

Application-defined or object-defined error

The source sheet has the following filter which I thought might somehow be related to the problem, however to test it out, I copied and pasted the data shown there as values, and still got the same error.

Excel Formula:
=FILTER(INDEX(table_rawData,SEQUENCE(ROWS(table_rawData)),XMATCH(A1:BQ1,table_rawData[#Headers])),(table_rawData[BR]="Yes")+((table_rawData[PatronRiskLevel]="POI")*(table_rawData[BR]="No")))

When I click on "debug", it highlights the fourth line of code: Range("A4").Resize(endOfRows, 69).Select . This code works fine on my other sheet, so I just can't figure out why it is not working on this sheet?
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try
VBA Code:
endOfRows = Range("A" & Rows.count).End(xlUp).Row
 
Upvote 0
Try
VBA Code:
endOfRows = Range("A" & Rows.count).End(xlUp).Row

Thank you. This does seem to work well, although there is one oddity - it also selects 3 blank rows under where my data is showing. To make sure there was no "ghost data" I delete all the rows on the sheet even though I knew it was not the issue. Since the data it is copying from is from a FILTER function, the entire range of data is displayed with a blue line around it. This code, copies all the data, plus 3 rows below the blue line. Any idea why it does that?

I'm fine with it copying 3 blanks lines and can use the code now as is, but I was just curious as to why it does this.
 
Upvote 0
You need to use
Rich (BB code):
Range("A4").Resize(endOfRows -3, 69).Select
because you are copying from row 4
However as it's a spill range you can simply use
VBA Code:
Sub rangeSelection()
   Range("A4").SpillingToRange.Copy
End Sub
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0
Oddly enough, this is no longer working.

Excel Formula:
Sub CopyData()
    Range("A4").SpillingToRange.Copy
    Selection.Copy
End Sub

The filter for the sheet, is in A4.

When I run the macro, it only copies what ever the current active cell is on the sheet. If I click the A4 cell to make it the active cell, then this macro only copies the data that is in that 1 cell.
 
Upvote 0
You need to remove this line
VBA Code:
    Selection.Copy
 
Upvote 0
Ah yes. Thanks again. I rarely use VBA so I am not at all familiar with it's syntax.
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,071
Latest member
cdnMech

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top