Excel: Simple macro coding fix

Hi,

Below is my macro. I get an error on line 14.

I am trying to select the first blank cell in column A - so I can populate it with "Open". What am I doing wrong?


Sub mac_1pasteallocation()

 Dim firstEmptyRow As Long
    Dim firstEmptyStatus As Long
    
    firstEmptyRow = Sheets("Data").Range("B" & Rows.Count).End(xlUp).Row + 1
    Sheets("Master").Range("B3:B12").Copy
    Sheets("Data").Range("B" & firstEmptyRow).PasteSpecial Paste:=xlPasteAll, Transpose:=True
    Application.CutCopyMode = False
    
    'This sets the status to open
    
    firstEmptyStatis = Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row + 1
    Sheets("Data").Range("A" & firstEmptyStatus).Select
    ActiveCell.FormulaR1C1 = "Open"

End Sub

Open in new window


Thanks.
LVL 1
dabug80Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You cannot select a cell on a sheet if that is not currently active. So before that line of code select the sheet first.

Sheets("Data").Select
Sheets("Data").Range("A" & firstEmptyStatus).Select

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Roy CoxGroup Finance ManagerCommented:
It is usually not necessary to select ranges, as in the start of your code.

Option Explicit

Sub mac_1pasteallocation()

    Dim firstEmptyRow As Long
    Dim firstEmptyStatus As Long

    firstEmptyRow = Sheets("Data").Range("B" & Rows.Count).End(xlUp).Row + 1
    Sheets("Master").Range("B3:B12").Copy
    Sheets("Data").Range("B" & firstEmptyRow).PasteSpecial Paste:=xlPasteAll, Transpose:=True
    Application.CutCopyMode = False

    'This sets the status to open

    firstEmptyStatis = Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row + 1
    Sheets("Data").Range("A" & firstEmptyStatus).Value = "Open"

End Sub

Open in new window

dabug80Author Commented:
The first empty cell in column A is a dropdown box. I would like the first option (open). To be selected.

So do I have to add the following:

ActiveCell.FormulaR1C1 = "Open"

Instead of .value = "Open"
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Roy CoxGroup Finance ManagerCommented:
Do you mean a Data validation list? If so then .Value works as well. You can still use FormulaR1C1 but the main thing is that you do not need to Select the Sheet or the Cell
dabug80Author Commented:
Yes. A data validation list.

Attached is my sample spreadsheet. To activate the macro and see the error - click the 'Add lead button'.

eeTemplate-Integrated-Webleadsv8.xlsm

Cheers
Roy CoxGroup Finance ManagerCommented:
The error in your example is because firstemptystatus is 0. Your code does not actually give it a value
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You have miss spelled the variables see the below line of codes.....

firstEmptyStatis = Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row + 1

And

Sheets("Data").Range("A" & firstEmptyStatus).Select

Therefore both should be either firstEmptyStatis or firstEmptyStatus.

Does this help?
Roy CoxGroup Finance ManagerCommented:
I've found the cause, you have a typo

   
 firstEmptyStatis = Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row + 1

Open in new window


Should be

firstEmptyStatus = Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row + 1

Open in new window


And so

Option Explicit

Sub mac_1pasteallocation()

    Dim firstEmptyRow As Long
    Dim firstEmptyStatus As Long

    firstEmptyRow = Sheets("Data").Range("B" & Rows.Count).End(xlUp).Row + 1
    Sheets("Master").Range("B3:B12").Copy
    Sheets("Data").Range("B" & firstEmptyRow).PasteSpecial Paste:=xlPasteAll, Transpose:=True
    Application.CutCopyMode = False

    'This sets the status to open

    firstEmptyStatiu = Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row + 1
    Sheets("Data").Range("A" & firstEmptyStatus).Value = "Open"

End Sub

Open in new window


If you use Option Explicit as I have you will be warned of such errors, see this
dabug80Author Commented:
Thanks.

I see the mispelling. Also, I removed the +1 row reference for firstemptystatus due to the '0' pasting.

Cheers.
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Glad your issue has been resolved. :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.