Problem with 2 rows with - Dim LR As Long

Bookmaster

Board Regular
Joined
Mar 22, 2014
Messages
85
Office Version
  1. 2021
Platform
  1. Windows
Hi,

I have a problem with VBA in excel. My code works fine for every table except when I have just 2 rows then I have error. First row is filled with column names and second rows i filled with formulas for each column. So if I have more then 2 rows everything works fine, but when I have just 2 rows I have error.
Line when my VBA stops is:

Code:
Dim LR As Long    LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    Range("E2").AutoFill Destination:=Range("E2:E" & LR)
    Range("I2").AutoFill Destination:=Range("I2:I" & LR)
    Range("K2").AutoFill Destination:=Range("K2:K" & LR)
    Range("M2").AutoFill Destination:=Range("M2:M" & LR)
    Range("O2").AutoFill Destination:=Range("O2:O" & LR)
    Range("Q2").AutoFill Destination:=Range("Q2:Q" & LR)
    Range("S2").AutoFill Destination:=Range("S2:S" & LR)
    Range("U2").AutoFill Destination:=Range("U2:U" & LR)
    Range("W2").AutoFill Destination:=Range("W2:W" & LR)
    Range("Y2").AutoFill Destination:=Range("Y2:Y" & LR)
    Range("AB2").AutoFill Destination:=Range("AB2:AB" & LR)
    Range("AF2").AutoFill Destination:=Range("AF2:AF" & LR)
    Range("AH2").AutoFill Destination:=Range("AH2:AH" & LR)
    Range("AJ2").AutoFill Destination:=Range("AJ2:AJ" & LR)
    Range("AL2").AutoFill Destination:=Range("AL2:AL" & LR)
    Range("AN2").AutoFill Destination:=Range("AN2:AN" & LR)
    Range("AP2").AutoFill Destination:=Range("AP2:AP" & LR)
    Range("AR2").AutoFill Destination:=Range("AR2:AR" & LR)
    Range("AT2").AutoFill Destination:=Range("AT2:AT" & LR)
    Range("AV2").AutoFill Destination:=Range("AV2:AV" & LR)
    Range("AX2").AutoFill Destination:=Range("AX2:AX" & LR)
    Range("AZ2").AutoFill Destination:=Range("AZ2:AZ" & LR)
    Range("I2").Select
End Sub

What is wrong here please?
I am sure that it has something to do with just 2 rows
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Why not

Code:
[LEFT][COLOR=#333333][FONT=monospace]Dim LR As Long
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
IF LR >= 3 then
    Range("E2").AutoFill Destination:=Range("E2:E" & LR)
    Range("I2").AutoFill Destination:=Range("I2:I" & LR)
.......
End If
Range("I2").Select
End Sub
[/FONT][/COLOR][/LEFT]


 
Upvote 0
That is what I wanted.... thank you so much!!!
I owe you so much for this help.

Best regards
 
Upvote 0

Forum statistics

Threads
1,215,084
Messages
6,123,028
Members
449,092
Latest member
ikke

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