Below is a copy of my data. I've been trying to find the last column + 1 (so in this example, it should be 4, since last column is C and plus 1). The code I'm using is to determine last column is:
lastColumn = Cells(lastRow, Columns.Count).End(xlToLeft).Column
But I keep getting 2?
lastColumn = Cells(5, Columns.Count).End(xlToLeft).Column
result will be 3 (last column on row 5)
lastColumn = Cells(lastRow, Columns.Count).End(xlToLeft).Column
result will be 2 (last column on row 10)
lastColumn = Cells(**5**, Columns.Count).End(xlToLeft).Column
Was using lastrow and in that row, only two columns were filled. Used the header row instead.
Related
VBA question using Excel 2016 (64 bit)
I have header data in the first two columns (A:B) and the top row (1). I would like to dynamically paste a formula downward, starting from cell C2, to the last row and the last column. The working prototype is as follows:
Dim LstCol As Long
LstCol = Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cells(2, "C"), Cells(2, LstCol)).Value = "=INDEX(RC1,MATCH(R1C,RC2,0))"
Dim LstRow As Long
LstRow = Cells(1, Rows.Count).End(xlUp).Row
Range(Cells(2, "C"), Cells(2, LstRow)).Value = "=INDEX(RC1,MATCH(RC1,R2C,0))"
The first Dim works as intended. The formula is successfully deployed throughout the second row to the final column (or perhaps it starts at the last column and moves leftward). However, the second Dim stops on an error. I'm unsure how to spread the data from the last row (to the last column) upwards to the second row (and the corresponding last column).
In my example, the first two columns have header data from 1 to 5330 (A1:B5330). I also have header data in the first row (C1:AX1). My target is to fill the formula =INDEX($A2,MATCH(D$1,$B2,0)) from C2:AX5330 dynamically.
Where am I going wrong?
You need to properly qualify your instances of Range and Cells with a worksheet. You can either directly do this, dim a worksheet variable, or use a With block like so:
Dim LRow as Long
With ThisworkBook.Sheets("????")
LRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("C2:AX5330").Formula = "=INDEX($A2,MATCH(D$1,$B2,0))" 'Make sure your cell references are correct here
.Range("C2:AX5330").Value = .Range("C2:AX55330").Value 'Place as values instead of formula
End With
I have a VBA code with the following.
Selection.AutoFill Destination:=Range("H2:J2002")
This can select till the last entry in column J when it has exactly 2002 entries. What I want is a general one which can select between H2 and the last entry in J column. I found the following
Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
But I am not understanding, how I can use the value of Lastrow to select the last cell in column J.
Lastrow = ActiveSheet.Cells(Rows.Count, 10).End(xlUp).Row
'lastrow now holds last occupied cell in column 10 ie J
Selection.AutoFill Destination:=Range("H2:J" & lastrow)
I am trying to change this code to only go from column 3 to a certain column farther down the workbook. Instead of what I have now, I am drawing a blank on how the syntax needs to change in order for me to be allowed to do this. Thanks!
For chartdatacol = 3 To Worksheets("Chart Data").Cells(Rows.Count, 2).End(xlUp).row
If you want to loop through the last column, use
Worksheets("Chart Data").Cells(1, Worksheets("Chart Data").Columns.Count).end(xlToLeft).Column
This assumes that row 1 has data in the last column you are interested in.
So your loop would look like
For chartdatacol = 3 To Worksheets("Chart Data").Cells(1, Worksheets("Chart Data").Columns.Count).End(xlToLeft).Column
For clarity, I would take the number fetching out of your for statement
Dim i as Integer,
lastCol as Integer,
firstCol as Integer
firstCol = 3
lastCol = mySheet.UsedRange.Columns.Count
For i = firstCol to lastCol
--Do something
Next i
I need to delete a range of rows using two named ranges
Current find the column numbers for the last column (lastColumn) and then the column 6 spots behind it (colrange)
Dim lastColumn As Integer
With ActiveSheet
lastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
colrange = lastColumn - 6
I want to delete the columns in the range colrange:lastColumn but not sure how to do this?
Range(Cells(1,colRange),Cells(1,lastColumn)).EntireColumn.Delete
you can simply use:
Range(Columns(colRange),Columns(lastColumn)).Delete
or directly:
With ActiveSheet
.Range(.Columns(.Cells(1, .Columns.Count).End(xlToLeft).Column - 6), .Columns(.Cells(1, .Columns.Count).End(xlToLeft).Column)).Delete
End With
I'm unsuccessfully trying to create a macro to insert blank row based on cell value.
I have a bulk data, in which one column has differing numbers. Based on the column value, I need to insert a blank row below it.
If I understand you properly this should do what you want.
Just change "A:A" to the range your working with and the If cell.Value = 1 Then to the condition you need to find the cell you want to add a blank row under.
Dim i As Range
Dim cell As Range
Set i = Range("A:A")
For Each cell In i.Cells
If cell.Value = 1 Then
cell.Offset(1).EntireRow.Insert
End If
Next
The below is an example for if you are looking to insert a blank row based on a sudden change of value in a column (in this case column "C"):
Dim lRow As Long
For lRow = Cells(Cells.Rows.Count, "C").End(xlUp).Row To 3 Step -1
If Cells(lRow, "C") <> Cells(lRow - 1, "C") Then Rows(lRow).EntireRow.Insert
Next lRow
You can change Cells(lRow - 1, "C") to whatever value you want to trigger your row insert and, of course, what column this is being applied to.