Using XlDown and select a defined column range - excel

I have this code that is using xlDown to determine the last row of data (minus 1) but I need to go to a defined column. So the selection would be as follows:
Go to A6 then do an xlDown -1
Then select the resulting row from xlDown and combine it with column BU
As an example, if the result from the xlDown is row 89 then the range to select should be A6:BU89
I tried using the xlRight but the data is inconsistent and can have blank columns in different places but I always want to go to column BU.
I can't seem to get it right, can someone help me out?
Sub AAPrepare_Pipeline_Data()
Range("A6").Select
Range(Selection, Selection.End(xlDown).Offset(-1)).Select
'Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
End Sub

Use the same code to gain the cells in column A but Resize the columns to 73 columns wide before the Select or Copy command.
Sub AAPrepare_Pipeline_Data()
Range(Range("A6"), Range("A6").End(xlDown).Offset(-1)).Resize(, 73).Copy
End Sub
You don't need to Select something in order to reference or copy it.

You can get the row number you desire, use it to create the range, and then select that range. Something like this should work:
Sub AAPrepare_Pipeline_Data()
Range("A6").Select
Dim desiredRow As Integer
desiredRow = Selection.End(xlDown).Offset(-1).Row
Range("A6:BU" & desiredRow).Select
Selection.Copy
End Sub

Related

Copy a specific range every nth number of rows in Excel VBA

I'm very new to VBA coding and cannot figure out this issue. I tried another solution but found it wouldn't work later down the road. I'm currently testing out this one.
On worksheet "BS growth" I need to read Column A to make sure it says "Asset" If it does I then need to copy and paste the entire rows, excluding A,B & C starting from D5 until D30. The loop then needs to skip 15 cells down and copy the next range of 25 rows (D45:D70, D85:D110, etc) until it no longer says Asset in column A. The first 402 rows in Column A say "Asset," and then the next 300 say "Liability" and the final 200 says "Equity."
I'll then need to paste the data into worksheet "Chart Ref" starting at row A2.
Here is a sample of the data. I am not sure if it is working/embedded properly.
enter image description here
I do not know how to make a loop repeat as needed.
Sub ChartReference4()
Dim ws As Worksheet
Set ws = ActiveWorkbook.Worksheets("BS growth")
For Each cell In ActiveWorkbook.Worksheets("BS growth").Range("A:A")
If cell.Value = "Asset" Then
Range("D5:D30").Select
'I need this to not be hard coded and only copy from D5 down 25 and then skip 15 cells and repeat the loop
Range(Selection, Selection.End(xlToRight)).Select 'Need all information to right
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Worksheets
End If
Next cell
End Sub
Currently this code just copies everything in the sheet from column D to the right.
Thanks so much, please comment if this doesnt make sense or I need more information.

How to copy an entire column based on the cell value in the first row?

I have a code sheet for different digits that is being used for certain calculations. I need to select the entire column based on the first value in the top row. I'm able to select the entire column based on the range but is there a VBA lookup function that looks up for a value specified in it, and compares with the value in defined cell range, and allows us to copy the entire column? Here's what I have done so far:
Columns("F:F").Select
Selection.Copy
Columns("A:A").Select
Selection.Insert Shift:=xlToRight
Suppose I have a value called "string1", so in my case, "string1" is in the address "F1" which is why I have copied the "F" column & pasting it in front of all other columns. Similarly, if the value is changed to "string2" which is at the address "G1" then column "G" needs to be pasted in the front of the sheet.
Based on BigBen's suggestion here's what did my work. I hope it helps all the newbies in VBA Macros.
Dim c As Range
With Worksheets(1)
Set c = .Range("A1:XFD1").Find("string1", LookIn:=xlValues)
Range(c.Address).EntireColumn.Select
Selection.Copy
Columns("A:A").Select
Selection.Insert Shift:=xlToRight
End With
Because I only want to search on the first row of the sheet so I used that range but it can be changed to any value depending on the requirement. The Find function returns the range (variable c) which can be used for desired selection & then it can be used in a way we want.
Edit: (To avoid selecting cells)
Dim c As Range
Set c = Worksheets(1).Range("A1:XFD1").Find("string1", LookIn:=xlValues)
c.EntireColumn.Copy
Columns("A:A").Insert Shift:=xlToRight

Excel vba Autofilter top 10 rows, copy result based on criteria

I have data in my spreadsheet in range A6:H105
I want to filter the first ten rows (A6:H15) with fixed criteria on the H-column and copy only column A, D, E from result
So far I have the following code:
Sub Filter()
ActiveSheet.Range("A6:H15").AutoFilter Field:=8, Criteria1:="xxx"
End Sub
Question 1: Why is first row not filtered?
Question 2: How to copy only column A,D,E from range after applied filter?
Answer 1: It is bacuse Autofilter treats the first row as table header. Set the range to start from one row higher, from row 5.
Answer 2: You can copy a combined range like this
Range("A6:A15, D6:D15, E6:E15").Copy
and then paste it into three adjacent columns wherever you like.
You may need to modify the range to select only filtered or non-blank cells first.
You can do something like this:
Sub Filter()
ActiveSheet.Range("A6:H15").AutoFilter Field:=8, Criteria1:="xxx"
Range("A:A,B:B,D:D").Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Range("J1").PasteSpecial Paste:=xlPasteValues
End Sub

Excel VBA Select all cells down

I'm looking to select every cell below a certain cell (not only in my list).
I know of xldown, but this doesn't work in the instance of blanks, which occur in my list.
Is this possible?
The follow will select all cells from A1 down to the last cell in column A that's not empty:
Sub select_all()
Range(Cells(1, 1), Cells(Range("A1000000").End(xlUp).Row, 1)).Select
End Sub
You can modify the column / range to fit in your situation. Noted that it is hardcoded to assume 1000000 or less rows being used.
This will select A9 to column A the last row with data:
Range("A9:A" & range("A" & rows.Count).End(xlUp).Row).select
Just change A and 9 to whatever column and row you want.
I am interested to know what your next line of code is though as .select can most probably be ommited and the command can be performed directly without a select.

Select range from A1 to end of file using VBA in Excel

I want to select all values in Excel 2007 worksheet between A1 and end of file (effect of ctrl End). There are always 4 columns but the rows will range from 2 to possibly hundreds. There will possibly be lots of blank cells throughout the selection, including the last cell.
The following just goes to the last cell to be selected, not the entire range. How can I modify this to accomplish what I want?
ActiveSheet.Range("A1", SpecialCells(xlLastCell)).Select
Many thanks.
You almost have it. The SpecialCells method needs a qualifier:
ActiveSheet.Range("A1", ActiveCell.SpecialCells(xlLastCell)).Select
If you always want the first four columns, then perhaps:
Sub dural()
Intersect(ActiveSheet.UsedRange, Range("A:D")).Select
End Sub
Record a macro doing it then review the code:
Something like this may work.
Range("A1").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Running this macro selected the below in my example:

Resources