VBA Fails When Only 1 Line of Data

Small Paul

Board Regular
Joined
Jun 28, 2018
Messages
118
Hi

I have a vba which starts with the following:

HTML:
ActiveSheet.Name = "Salesforce Download"    
Dim lastrow As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Setmyrange = range("A2", "A" & Cells(Rows.Count, 1).End(xlUp).row)    
Application.ScreenUpdating = False    
Columns("A:A").Select    
Selection.Insert shift:=xlToRight    
Selection.Insert shift:=xlToRight    
range("A1").Select    
ActiveCell.FormulaR1C1 = "Worksheet Identifier: IFA Group, Account Name, Currency"    
range("A2").Select    
ActiveCell.FormulaR1C1 = "1"    
range("A3").Select
lastrow = Cells(Rows.Count, "L").End(xlUp).row
'this following line changes column A to #VALUE! if only 1 line of data!!!!!
range("A3:A" & lastrow).FormulaR1C1=IF(AND(RC[30]>""None"",RC[30]=R[-1]C[30],RC[32]=R[-1]C[32]),R[-1]C,IF(AND(RC[30]=""None"",RC[31]=R[-1]C[31],RC[32]=R[-1]C[32]),R[-1]C,R[-1]C+1))"

The idea is that A2 will be numbered 1. A3 will be numbered 1 or 2 depending on set criteria.
A2 numbers correctly.
However, when there is only 1 row of data, the VBA adds #VALUE ! to cell A3 and also changes cell A2 to the same error.

What change do I need to make so the VBA stops if there is only 1 row of data?

Note: Works perfectly when 2+ rows of data!

Any thoughts would be most welcome.

Cheers
Small Paul.
 
Last edited:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Add this line
Code:
lastrow = Cells(Rows.Count, "L").End(xlUp).Row
[COLOR=#ff0000]If lastrow = 1 Then Exit Sub[/COLOR]
In future can you please post code in a readable format, rather than one long line.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

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