If a cell in column A is not blank then column B equals

uvela

New Member
Joined
Feb 18, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm having trouble with something that seems pretty straight forward.

I have a worksheet with Dates and Blank cells in column A and would like to put "Open" if column A has a Date and "Closed" if column A has a Blank in column B.

I'm using the following code but am getting a 400 error.

VBA Code:
Sub openclose()

    Dim rng As Range
    Dim i As Long

    'Set the range in column A you want to loop through
    Set rng = Range("Z1:Z" & LastRow2)
    For Each cell In rng
        'test if cell is empty
        If cell.Value <> "" Then
            'write to adjacent cell
            cell.Offset(0, -1).Value = "Closed"
        Else
            cell.Offset(0, -1).Value = "Open"
        End If
    Next

End Sub

Any help or suggestions would be appreciated.

Thanks.:)
 

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)
is there code you haven't posted? You dim i as long but then its not referred to in the code.
You also have a reference to lastrow2 but dont appear to have defined it anywhere
 
Upvote 0
VBA Code:
Set rng = Range("Z1:Z" & LastRow2)

Your code is checking column Z
 
Upvote 0
is there code you haven't posted? You dim i as long but then its not referred to in the code.
You also have a reference to lastrow2 but dont appear to have defined it anywhe
Apologize, the dim i was leftover code.
Lastrow 2 is defined as

Lastrow2 = Range(Rows.count, "Z").End(xlUp).row

VBA Code:
Set rng = Range("Z1:Z" & LastRow2)

Your code is checking column Z
Yes, apologize for that, when I mentioned column A and B I meant column Z and X respectively.
 
Upvote 0
Lastrow2 = Range(Rows.count, "Z").End(xlUp).row
That is not valid code.

It should be:
VBA Code:
Lastrow2 = Cells(Rows.Count, "Z").End(xlUp).Row
or
VBA Code:
Lastrow2 = Range("Z" & Rows.Count).End(xlUp).Row
 
Upvote 0
Apologize, the dim i was leftover code.
Lastrow 2 is defined as

Lastrow2 = Range(Rows.count, "Z").End(xlUp).row
Also, do you have the 'Lastrow2=' in a different subroutine?
 
Upvote 0
Also, do you have the 'Lastrow2=' in a different subroutine?
To add on to what Johnny asked, if "LastRow2" is being set somewhere else in another procedure, unless you have declared "LastRow2" to be a global variable and already run the code to set its value before running this code you posted, it won't work.

If it is NOT a global variable. you should define it and do the calculation in this procedure you posted BEFORE you try using it in the code.
 
Upvote 0
Solution
Thank you for your replies it helped me troubleshoot the issue.
 
Upvote 0
You are welcome.
Glad we could help!
 
Upvote 0

Forum statistics

Threads
1,215,620
Messages
6,125,876
Members
449,268
Latest member
sGraham24

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