Rows counts issue in VBA

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
857
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello - having a stubborn issue with my VBA that I cannot quite solve for and/or maybe understanding what is going on incorrectly and looking for some guidance.

My VBA will input a formula based off data in column A as shown below. This item I am trying to solve for is say there is no data below the header (row 2), so data starting on row 3. my formula is still applying. Is anyone able to help how I can avoid this. See below for what i mean

VBA Code:
'formatting and formulas for recon sheet
With WsNAVF
lr1 = .Cells(rows.count, "A").End(xlUp).row
    If lr1 < 3 Then lr1 = 3
    .Activate
    .Range("A:B").RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes
    .Range("A:C").HorizontalAlignment = xlCenter
    .Range("C3:C" & lr1).Formula = "=$A3&B3"
    .Range("C3:C" & lr1).Value = .Range("C3:C" & lr1).Value
    .Range("D3:D" & lr1).Formula = "=iferror(VLOOKUP(C3,'Trend_NAX'!$C$11:$E$10000,3,FALSE),"""")"
    .Range("E3:E" & lr1).Formula = "=iferror(VLOOKUP(C3,'Trend'!$C$11:$F$10000,4,FALSE),"""")"
    .Range("F3:F" & lr1).Formula = "=SUMIF('ACCRUAL'!$C$10:$C$10000,$A3,'ACCRUAL'!$G$10:$G$10000)"
    .Range("G3:G" & lr1).Formula = "=iferror(round($F3/$E3,5),"""")"
    .Range("H3:H" & lr1).Formula = "=iferror(round(($F3/$D3)*10000,2),"""")"
    .Range("I3:I" & lr1).Formula = "=SUMIFS('Analysis_'!$J$10:$J$10000,'Analysis_'!$B$10:$B$10000,$A3,'Analysis_'!$D$10:$D$10000,$B3,'Analysis_'!$G$10:$G$10000,$I$1)"
    .Range("J3:J" & lr1).Formula = "=iferror(round($I3/$E3,5),"""")"
    .Range("K3:K" & lr1).Formula = "=iferror(round(($I3/$D3)*10000,2),"""")"
    .Range("L3:L" & lr1).Formula = "=SUMIFS('Analysis_'!$J$10:$J$10000,'Analysis_'!$B$10:$B$10000,$A3,'Analysis_'!$D$10:$D$10000,$B3,'Analysis_'!$G$10:$G$10000,$L$1)"

1687469882226.png
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
How about changing:
VBA Code:
    If lr1 < 3 Then lr1 = 3

to:
VBA Code:
    If lr1 > 2 Then

Then after all of your code that you want to run if row 3 is not blank, add a line of code:
VBA Code:
    End if
 
Upvote 0
Solution
ok so just apply a IF/THEN and skip all those formulas if its met and if not to continue after the END IF
 
Upvote 0
The If/End If I suggested will apply the formulas if There is data in column A after row 2.

If there is no data in column A after row 2 then the formulas will not be applied, and the code will skip to the End If that was added. This is my understanding of what you were asking for.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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