VBA Macro to select area not working

Browneh89

Board Regular
Joined
Mar 8, 2019
Messages
55
Office Version
  1. 365
Platform
  1. Windows
Hello all!

I'm having a little trouble with a macro I am attempting to record. The macro is supposed to filter a table of data (removing all blanks rows) then select an area in prep to print. when I record the macro I use Ctrl + Shift + arrow key to select the area which I have used before to write a similar macro (which works perfectly) however this time it won't select past the coloums which filter the data. Does anyone know what could be causing this?

Example of the macro that's failing below;

Sub Macro4()
'
' Macro4 Macro
'

'
ActiveSheet.Range("$B$4:$V$99").AutoFilter Field:=5, Criteria1:="<>"
Range("O1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Range(Selection, Selection.End(xlToLeft)).Select
End Sub

Thank you in advance for any help you can offer!
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
If any of the columns on the row have visible data, the arrow method stops and has to be initiated again to continue on. It will only ignore hidden rows or columns, not those which are visible with data. The same rules apply to the range of movement for Ctrl + Shift + Arrow as for End + Arrow.
 
Upvote 0
Hey,

Yeah I know that, it's why "Range(Selection, Selection.End(xlDown)).Select" is repeated multiple times within the macro but how does that explain it not working?
 
Upvote 0
however this time it won't select past the coloums which filter the data.

This statement from post #1 indicates that it is working as designed, or have I misunderstood what is meant by the statement? It also stops at cells with formulas that = NullString but appear to be blank.
 
Upvote 0
Yes I'm a little confused as well. So how would I write this so that it detected the last cell with data in and selected the entire range for my table?
 
Upvote 0
Does this help to remove the blank rows ???
VBA Code:
Sub MM1()
  With Range("O1", Cells(Rows.Count, "O").End(xlUp))
    .Replace "", "#N/A", xlWhole, , False, , False, False
    Columns("O").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
  End With
End Sub
 
Upvote 0
Yes I'm a little confused as well. So how would I write this so that it detected the last cell with data in and selected the entire range for my table?
Something like this:
VBA Code:
ActiveSheet.Range("$B$4:$V$99").AutoFilter Field:=5, Criteria1:="<>"
With ActiveSheet
    Intersect(.Range("B:O"),.UsedRange).SpecialCells(xlCellTypeVisible).copy
    'Code to paste it somewhere goes here
End With
I assume you intend to copy and paste the data to another arear or another sheet to make your table since it is in a filtered range. I note that omit Column A and you include columns P:V in your filter but omit P:V in your selection. Also, Michael M has an alternative to filtering for removing the blanks.
 
Upvote 0
Hi, I apologize I don't think I explained my issue very well.

So below is the sheet I'm working on I want to be able to select from Cell A1 to N1 and select down to the black highlighted black row at the bottom but whenever I attempt to build a macro using Ctrl - Shift - Down arrow, it gets stuck at row 4 (which has the filters). I only want to select it in prep to print and I don't want to copy the data to another area as after the sheet has been printed out it needs to be filled in manually within the sheet.

I just don't understand why it keeps getting stuck as I have used this to create macros which automatically print sheets before and it's worked totally fine!

I usually select N1 and go down to the bottom of the table than across as the sheet goes further past N1, but I don't require those columns to be printed off.

Cell Formulas
RangeFormula
H2:I2H2=SUM(I5:I20)
K2K2=SUMIFS($I$5:$I$20,$B$5:$B$20,">="&TIME(22,0,0))+SUMIFS($I$5:$I$20,$B$5:$B$20,"<"&TIME(6,0,0))
L2L2=SUMIFS($I$5:$I$20,$B$5:$B$20,">="&TIME(6,0,0),$B$5:$B$20,"<"&TIME(14,0,0))
M2M2=SUMIFS($I$5:$I$20,$B$5:$B$20,">="&TIME(14,0,0),$B$5:$B$20,"<"&TIME(22,0,0))
A5:A20A5=LEFT(Gate!A2,10)
B5:B20B5=RIGHT(Gate!A2,5)+0
C5:C20C5=Gate!U2
D5:D20D5=Gate!T2
E5:E20E5=IFNA(INDEX(Gate!B:B,MATCH(Sheet1!D5,Gate!T:T,0)),"")
G5:G20G5=INDEX(Gate!C:C,MATCH(D5,Gate!T:T,0))
H5:H20H5=IF(G5=0,INDEX(Gate!E:E,MATCH(D5,Gate!T:T,0)),Sheet1!G5)
I5:I20I5=IFNA(INDEX(Gate!J:J,MATCH(Sheet1!D5,Gate!T:T,0)),"")
F5:F21F5=IF(LEFT(E5,3)="153","A","S")
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A5:I20Expression=$F5="S"textYES
 
Upvote 0
Maybe this
VBA Code:
Sub t()
ActiveSheet.Range("A1", Cells(Rows.Count, 1).End(xlUp)).Resize(, 14).Select
Selection.PrintOut
End Sub

Or just
VBA Code:
Sub t()
ActiveSheet.Range("A1", Cells(Rows.Count, 1).End(xlUp)).Resize(, 14).PrintOut
End Sub
 
Upvote 0
Thank you mate this worked perfectly!! Do you know why my original macro did not work?
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,289
Members
449,077
Latest member
Rkmenon

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