Selecting data within a recorded macros - excel

I am trying to record a macro to automate formatting usage reports. The reports don't all have the same number of rows or columns.
What I want to do is select all columns and rows with data. When recording if I click Ctrl+Shift+Down+Right it will select the data on that report. However, if I run it on a set of data with more rows or columns it won't include it in the selection.
Is there a way to select from a starting cell to the end of the rows or columns of available data?

Using relative reference will definitely get you the correct coding. Or you can just put this code Range("A1").CurrentRegion.Select into the appropriate place in the macro (where you put the top leftmost cell in the place of the "A1").
If you want to do more advanced work you can save the current region as a variable and use different properties on it to do things other than simply select the area.
For example:
sub test()
dim rng as range
dim rw as integer
dim col as integer
set rng = Range("A1").CurrentRegion 'choose the top leftmost cell in the range
rw = rng.rows.count 'counts the number of rows in the selection
col = rng.columns.count 'counts the number of columns
rng.select 'selects the range in an efficient manner
rng.columns(2).select 'selects the second column in the range
rng.rows(2).select 'selects the second row in the range
rng.cells(1,2).select 'selects the cell on the first row and second column in the range
end sub
Hopefully this is helpful. I just wanted to expand on some more things you could do to work with the ranges you have.

Related

How do I make a Range of a certain area, that has been filtered out?

So first of all, I wanna write a VBA Code, which allows me to pick a certain range of an Excel sheet, to then copy SOME of the needed values to another worksheet. The issue with this is, that in the Excel sheet of which i take Information from, has some filters applied.
So i found the solution with the method (?) .SpecialCells(xlCellTypeVisible) but the Problem again is, that it works for 1 column, but not for Ranges with more than one column. For Ranges with more than one column, it only picks the first row
Dim rng As Range
Set rng = src.Worksheets("l04").Range(src.Worksheets("l04").Range("Z7:AK7"), src.Worksheets("l04").Range("Z7:AK7").End(xlDown)).SpecialCells(xlCellTypeVisible)
My expected result from this Line of Code should be, that the Range rng is set from Z7 to AK7 all the way down to the maximum number of rows, but only those which are visible.
Edit1: Changed SpecialCell --> SpecialCells
Dim cell As Range
Dim lastRow As Long
With src.Worksheets("104")
lastRow = .Cells(.Rows.Count, "Z").End(xlUp).row
With .Range("Z7:AK" & lastRow)
For Each cell In .Columns(1).SpecialCells(xlCellTypeVisible)
Debug.Print Intersect(.Cells, cell.EntireRow).Address ' change this to what you actually need to grab from each visible cell
Next
End With
End With
Based on some clues in your question, you may find that using the Intersect Method is advantageous.
Dim rng as Range
With src.Worksheets("l04")
'gets all visible cells within the used range on your sheet
set rng = .UsedRange.SpecialCells(xlCellTypeVisible)
'Use the Intersect method to select specific columns
set rng = Intersect(rng, .range("AB:AB, AD:AD"))
End With
Note: This will not select down to last row (i.e. row 1,048,576), only to the last row with data in the specified range.

Copy data to sheet named in column header and cell named at beginning of row, then loop

I have some production data in a table on my Source worksheet. In the header of each column is the name of the Destination worksheet, where the data in that column is to be pasted. In Column A at the beginning of each row is the destination column on the destination worksheet; and in Column B is the destination row. Column C concatenates the two to display the cell name, e.g, T138, which would be the destination cell on the destination worksheets. I haven't been a member long enough to embed an image in this post, but there is an pic of the table here:
So each datum on the Source worksheet is to be copied and then pasted to the worksheet named in the column header and the cell named in column C at the beginning of the row (or alternatively, in the column named in Col.A and the row named in Col.B) Then the script should loop back through the rest of the data cells and do the same for each of them: copy data, paste to sheet specified in column header and cell specified at the beginning of the row in Col.C.
Even though I am a total beginner, I actually thought this would be a fairly simple matter. But I haven't been able to figure out how to do this. I've tried various scripts, but none of them even began to do the job, and they really aren't even worth displaying here. I was hoping someone could point me in the right direction. Worse yet, none of my extensive searches have turned up anything like what I want to do. Maybe I just haven't used the right search terms. "Variable" seems to have gotten the closest to useable search results, but not exactly.
Here is an image of what one of the destination sheets looks like, in relevant part:
Here's a better image:
This code will Loop over each cell in the range of G3 to J and LastRow. Copying the cell if it's numeric to the sheet based on the column header and the cell from column C.
Public Sub MoveData()
Dim rcell As Range, rng As Range
LastRow = ActiveSheet.UsedRange.Rows.Count + ActiveSheet.UsedRange.Rows(1).Row - 1
'MsgBox LastRow
Set rng = Application.ActiveSheet.Range("G3:J" & LastRow)
For Each rcell In rng.Cells
If Not Len(rcell.Value) = 0 Then
'MsgBox rcell.Value
Header = rcell.Offset(1 - rcell.Row).Value
'MsgBox Header
Set DestSheet = ThisWorkbook.Sheets(Header)
Set DestCell = ActiveSheet.Range("C" & rcell.Row)
'MsgBox DestCell
Application.ActiveSheet.Range(rcell.Address).Copy Destination:=Sheets(Header).Range(DestCell)
End If
Next rcell
End Sub

Selecting a specific visible range After Filtering dataset

I'm trying to copy the one specific column(excluding the header) after filtering my dataset. If there are multiple rows after filtering, my selection of range works fine and I am able to copy that column into another sheet. However, if there is only one row after filtering, when I specify the range, it selects all the empty cells along with the non-empty cell and my code is malfunctioning. How do I fix this issue?
I have tried using different range properties but couldn't get the desired outcome
'''Finding the Pack Test Category from the filtered 1st column'''
Set RngA = ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Areas(2)(1, 1)
''Here the selection of range includes all the empty cells as well!
Set RngA = Range(RngA, RngA.End(xlDown))
'''Copy & Pasting in the Forecast Sheet for temporary use'''
RngA.SpecialCells(xlCellTypeVisible).Copy Destination:=wbA.ActiveSheet.Range("L1")
I expect the range to be selected with only the visible cells having data not the empty cells.
Say we start with:
and filter for age above 45:
we want to copy filtered column A to another sheet:
Sub KopyOneKolumn()
Dim r1 As Range
Dim r2 As Range
Dim r3 As Range
Set r1 = ActiveSheet.AutoFilter.Range ' the total visible range
Set r2 = Intersect(r1.Offset(1, 0), r1) ' clip off the header
Set r3 = Intersect(r2, Columns(1)) ' pick only column A
r3.Copy
Sheets("Sheet2").Paste
End Sub
Result in Sheet2:
NOTE:
The key point is that r1 represents a "copy-able" block of visible cells in the autofilter table.
Relying on Areas(2) is not going to work if the first row under the header is part of the filtered (visible) cells.
'at this point, AutoFilter has been applied
'I have no idea what the range is
'or what column you are interested in
with ActiveSheet.AutoFilter.Range
with .cells.resize(.rows.count-1, 1).offset(1, 0)
set RngA = .SpecialCells(xlCellTypeVisible)
end with
end with
RngA.copy Destination:=wbA.ActiveSheet.Range("L1")
I disagree with your use of ActiveSheet as the parent worksheet references. The source and destination worksheets should be explicitly referenced.

How to use Rows(x:x) .select in IF statement

I have a spreadsheet where I have a macro built to whittle down the amount of data to something manageable. The next step is to compare the value of a column cell to a predefined number determined in the macro and placed in another cell away from the data. If the value is less than the predetermined value, I want to delete the entire row, then go on to the next row and do it again. In the end, all I should have left is the rows where that column is greater than the predetermined value.
Let's say the column is C and the predetermined value is in M1. How would I do this?
BTW, the macro to date has filtered the data based on Column C from largest to smallest, so I'm basically looking to find the first value in C to be less than the other value, then highlight everything underneath and delete it.
Thanks for any help.
You didn't specify if you had a Header row or not, but for your exact request here you go.
Option Explicit
Sub DeleteRows()
Dim ws As Worksheet, myVal As Double
Dim Cell As Range, RngC As Range, CompRng As Range
Set ws = ThisWorkbook.Worksheets(1)
Set RngC = ws.Range("C:C")
Set CompRng = ws.Range("M1")
myVal = CDbl(CompRng.Value)
For Each Cell In RngC
If CDbl(Cell.Value) < myVal Then
Cell.EntireRow.Delete
End If
Next Cell
End Sub

Select discontinuous range based on column A

I have data in columns A:I. The data in column A will always go through to the last row, but other rows in other columns will sometimes be blank. How do I select the range based on the last row in column A? For example, sometimes column A will have 40 rows of data but column I will be blank after row 3. I would still want to select A1:I40.
Ultimately, I want to use VBA to format and put a filter on this range, so I am hoping to not include any blank rows after the last used row in column A.
Consider:
Sub qwerty()
Dim N As Long
N = Cells(Rows.Count, "A").End(xlUp).Row
Range("A1:I" & N).Select
End Sub
Gary's Student gave you the answer
maybe you're interested in expanding the feature and want to consider the last row with at least one non blank cell in the whole columns range (columns "A:I" in your example), irrespective of which column has it, than you could use:
Function LastRow(sht As Worksheet, columnsStrng As String) As Long
With sht
With Intersect(.UsedRange, .columns(columnsStrng)).SpecialCells(xlCellTypeConstants)
LastRow = .Areas(.Areas.Count).Row
End With
End With
End Function
and here follows an example of how to use it
Option Explicit
Sub main()
Dim ws As Worksheet
Set ws = Worksheets("mysheet1")
ws.columns("A:I").Resize(LastRow(ws, "A:I")).Select
End Sub
This assumes your data are constants (i.e. actual cells content is not a "formula").
But it can be easily enhanced to consider "formula" data as well

Resources