If statements in VBA includes code to go to lastrow + 3 rows down

dmekwunye

New Member
Joined
Feb 5, 2018
Messages
5
I have a spreadsheet that has the following:

  • Column AB has code to go to the lastrow + 3 rows and uses a SUMIF function; lastrow + 4 rows and uses a SUMIF function and lastrow + 5 rows and uses a SUMIF function:

Range("AB" & lastrow + 4).FormulaR1C1 = "=SUMIF(R11C1:R[-4]C[-27],""NQ"",R11C28:R[-4]C)"
Range("AB" & lastrow + 4).Select
Selection.Font.Size = 8
Selection.Font.Bold = True
Selection.NumberFormat = "_(* #,##0_ );_(* (#,##0);_(* ""0""_);_(@_)"
Range("AB" & lastrow + 5).FormulaR1C1 = "=SUMIF(R11C1:R[-5]C[-27],""PSU"",R11C28:R[-5]C)"
Range("AB" & lastrow + 5).Select
Selection.Font.Size = 8
Selection.Font.Bold = True
Selection.NumberFormat = "_(* #,##0_ );_(* (#,##0);_(* ""0""_);_(@_)"
Range("AB" & lastrow + 6).FormulaR1C1 = "=SUMIF(R11C1:R[-6]C[-27],""RSU"",R11C28:R[-6]C)"
Range("AB" & lastrow + 6).Select
Selection.Font.Size = 8
Selection.Font.Bold = True
Selection.NumberFormat = "_(* #,##0_ );_(* (#,##0);_(* ""0""_);_(@_)"
Range("AB" & lastrow + 7).FormulaR1C1 = "=SUM(R[-3]C:R[-1]C)-R[-5]C"
Range("AB" & lastrow + 7).Select
Selection.Font.Size = 8
Selection.Font.Bold = True
Selection.NumberFormat = "_(* #,##0_ );_(* (#,##0);_(* ""0""_);_(@_)"
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With

The code works just fine.

Next, I'm trying to enter the following function in column AG: =IF(A11="NQ",0,IF(A11="PSU",(AB11/AB3802*X11,IF(A11="RSU",(AB11/AB3803)*X11))))

The problem that I am having is that the cell reference of AB3802 and AB3803 will change depending on the data. It will always be the 5th row down from the last row based on Column A entries. The code for the formulas for column AB (above), works with any data - regardless of the number of rows in the spreadsheet. How can I get the formula for AG to work with spreadsheets of different number of rows?

Thanks,
De Anna
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi there,
This bit of code should do what you want (change AG1 to whichever line you want the code in):

Code:
nearend = lastrow + 5
atend = lastrow + 6
Range("AG1").Formula = "=IF(A11=""NQ"",0,IF(A11=""PSU"",(AB11/AB" + Format(nearend) + "*X11,IF(A11=""RSU"",(AB11/AB" + Format(atend) + ")*X11))))"
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,822
Members
449,470
Latest member
Subhash Chand

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