I have an excel sheet in which the first 3 columns are a pivot table and whenever I refresh it, the no. of rows may change. Columns E,F,G,H,I use a VLOOKUP formula based on Columns A,B,C.
Since the no. of rows are changing, how can I make sure that the vlookup formula also automatically adjusts for columns E,F,G,H,I based on no. of rows of A,B,C ?
Attached is an image in which you can see that the formula didn't apply for last 3 rows and I have manually drag the cells if it were to work.
Somewhat brute force would be to do something like
' Just Refreshed Pivot Table
'
' Assumes variable ws refers to this worksheet
' Assumes your formulas start on row 2
ws.Range("E2:I2").Copy ws.Range("E3:E" & ws.UsedRange.Rows.Count)
Then put an if statement around your formula in column E (and similar if statements on the other columns):
=If(C2="","",VLookup(...))
You can always do a more robust way to determine how many rows are on the left versus the right. Or step through like:
For i = 1 to ws.UsedRange.Rows.Count
If(ws.Cells(i,3).Value = "")
ws.Range("E" & i & ":I" & i).ClearContents
Else
''' Apply your formulas / values in columns E through I of row i
End If
Next i
Related
I've got 2 sheets in a workbook with different sets of data. Sheet 1 has a set of data not formatted as a table. The header row for this data is on Row 4. When I try doing a count using Range("A" & Rows.Count).End(xlUp).Row, I get the last row of that entire sheet, not the count of how many rows of data there are from my starting point.
Sheet 2 has its header row in Row 1. So when I try to use the same count of rows I mentioned above to AutoFill, I always get 3 extra rows because the Sheet 1 count is just looking at what the last row of data is.
I don't want to have to shift things around in either sheet. I just want to be able to autofill based on the same number of rows as there are in Sheet 1, beginning the count at A5 and going down to the last row of data. Is there a different count formula to start at a specific cell and only count the rows below it, and then telling the other sheet to only AutoFill based on that number that I'm missing?
You may be over thinking this. Just discount the row count by 3
Range("A" & Rows.Count).End(xlUp).Row - 3
I would qualify the objects & properties (Range & Row) as well for good measure.
could you use either:
set rng = Range("A1:A1000").SpecialCells(xlCellTypeConstants, 2)
or
set rng = Range("A5").CurrentRegion.Resize(,1)
assuming no blanks in the data range
I have a current Sub that organizes data certain way for me and even enters a formula within the first row of the worksheet, but I am running into the issue of wanting it to be able to adjust how far down to fill the formula based on an adjacent column's empty/not empty status. For example, each time I run a report, I will get an unpredictable amount of returned records that will fill out rows in column A, however, since I want to extract strings from those returned records into different Columns, I have to enter formulas for each iteration within the next three columns (B, C, and D). Is there a way to insert a line that will evaluate the number of rows in Column A that are not blank, and then fill out the formulas in Columns B, C, and D to that final row? (I know that tables will do this automatically once information is entered in the first row, but for logistical reasons, I cannot use tables).
My current code that fills out the formula in Column B, Row 2 is:
Range("B2").Select
ActiveCell.FormulaR1C1 = "=MID(RC[-1],FIND(""By:"",RC[-1])+3,22)"
Thanks!
The formula that you actually need is
=IF(A2="","",MID(A2,FIND("By:",A2)+3,22))
instead of
=MID(A2,FIND("By:",A2)+3,22) '"=MID(RC[-1],FIND(""By:"",RC[-1])+3,22)"
This checks if there is anything in cell A and then act "accordingly"
Also Excel allows you to enter formula in a range in one go. So if you want the formula to go into cells say, A1:A10, then you can use this
Range("A1:A10").Formula = "=IF(A2="","",MID(A2,FIND("By:",A2)+3,22))"
Now how do we determine that 10? i.e the Last row. Simple
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
'~~> Change the name of the sheet as applicable
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> Find Last Row in Col A
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("B2:B" & lRow).Formula = "=IF(A2="""","""",MID(A2,FIND(""By:"",A2)+3,22))"
End With
End Sub
More About How To Find Last Row
You can use this to populate columns B:D based on Column A
Range("B2:D" & Range("A" & Rows.Count).End(xlUp).Row).Formula = _
"=MID($A2,FIND(""By:"",$A2)+3,22)"
I have the following code that:
copies the column headings (in row 1) from column C across to the second last column
pastes these column headings in row 1 in the column 2 across from the last column with data
pastes the column headings alongside each row of data through to the bottom row
Sub GLDR()
'use End(xlUp) to determine Last Row with Data, in column A of the GLDRYYPP tab
Dim lastRowDR As Long
lastRowDR = Sheets("GLDRYYPP").Range("A" & Rows.Count).End(xlUp).Row
'copy the cost type categories and paste alongside the cost centres
CTNameCol = "S2:AF" & lastRowDR
Sheets("GLDRYYPP").Range("C1", Range("C1").End(xlToRight).Offset(0, -1)).Copy
Sheets("GLDRYYPP").Paste Destination:=Sheets("GLDRYYPP").Range("C1").End(xlToRight).Offset(0, 2)
Sheets("GLDRYYPP").Range(Range("C1").End(xlToRight).Offset(0, 2), Range("C1").End(xlToRight).Offset(0, 2).End(xlToRight)).Copy
Sheets("GLDRYYPP").Paste Destination:=Sheets("GLDRYYPP").Range(CTNameCol)
End Sub
The first two steps have been set to be dynamic for any additional columns added but I am having trouble writing some code that will paste the data through to the bottom row. As you can see the range "S2:AF(last row)" has been written to make use of the result from the lastRowDR dimension.
Is there a way to write the code which will make the copy dynamic across the columns and rows?
Yes there is a way to write the code which will make the copy dynamic across the columns and rows. What you need to do is to identify a distinct quality about the cost centres so that you locate it using something like ActiveSheet.Cells.Find(Txt).
Then you'll be able to determine what CTNameCol should be. There is some helpful info here: Range.Find Method.
If you add add a picture of your spreadsheet I will update this answer with more precise information.
I have been working on a code to copy the data from one specific range(always the same) and paste in another spreadsheet always in the row below. So basically, it starts pasting on row 11, but if I run again it will paste on the row 12 and there it goes.. The code has been working fine, but there is only one problem. It identifies the next empty row(to paste) based on the value of the column AP, but i want it to identify based on the values of all the columns between AP:BA. Thus, if there is any value on those cells, it should copy on the row below, not only if there is a value on AP. Does someone know how to change my code in order to solve this problem? Thank You very much
Sub Copy_Shanghai()
Dim count As Integer
count = 11
Do While Worksheets("Time Evolution").Range("AP" & count).Value <> ""
'<>"" means "is not empty", as long as this happens we go down looking for empty cell
count = count + 1
Loop
'Now count is row with first empty cell outside of top 10 rows in column C
Worksheets("Fill").Range("E5:P5").Copy
Worksheets("Time Evolution").Range("AP" & count).PasteSpecial xlPasteValues
End Sub
I am trying to have a macro add in a specific number of rows to a table based on the value of A2.
For example:
If A2=10, then when I run the macro it should insert 10 rows.
also, as a second part to the macro I want it to copy the formula that the previous cells have.
Example:
A3= "='Sheet2'!A1"
and if I add 10 rows then the following rows should be sequential:
A4= "='Sheet2'!A1"
A5= "='Sheet2'!A2"
A6= "='Sheet2'!A3"
A7= "='Sheet2'!A4"
A8= "='Sheet2'!A5"
etc...
I know that a typical drag of the table will copy the cells, I want to ensure that the formula's are copied to the next row.
This will copy and paste the formula as you showed in A3 down the number you enter in A2.
ActiveWorkbook.Sheets(1).Cells.Range("A4:A" & Rows.Count).ClearContents
For i = 1 To ActiveWorkbook.Sheets(1).Cells(2, 1).Value
ActiveWorkbook.Sheets(1).Cells(3 + i, 1).Formula = ActiveWorkbook.Sheets(1).Cells(3, 1).FormulaR1C1
Next i
Is this what you are looking for?