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?
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

0

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

0
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"
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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
0
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
0
Roy CoxGroup Finance ManagerCommented:
The error in your example is because firstemptystatus is 0. Your code does not actually give it a value
0
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?
0
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
0
dabug80Author Commented:
Thanks.

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

Cheers.
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Glad your issue has been resolved. :)
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.