Help With Object Require Error

Ivy_1011

New Member
Joined
Mar 19, 2021
Messages
18
Platform
  1. Windows
Hi,

The code below allows me to add zeros to a specific row. I am trying to change the code to reference a particular cell value where I specify the range I want to use, but I am getting the Object Required 424 Error. I have added both lines of code the one that works well and the one that has the error.



Sub Empty_Cells()


Dim i As Long
Dim c As Long
Dim myRange As Range
Dim myCell As Range



'This line is giving me a 424 error object required
Set myRange = Worksheets("Data").Range("E1").Value

'This line of code below works well
'Set myRange = Worksheets("Data").Range("A4:H4")



For Each myCell In myRange
c = c + 1
If IsEmpty(myCell) Then

myCell = 0

i = i + 1
End If
Next myCell


MsgBox _
"There are total " & i & " empty cell(s) out of " & c & "."
'

'
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
If I understand your needs correctly, this is the right syntax:

VBA Code:
Set myRange = Worksheets("Data").Range(Worksheets("Data").Range("E1").Value)
 
Upvote 0
Solution
Hi there... Code untetsted but maybe try

VBA Code:
Sub Empty_Cells()
    
    Dim i           As Long
    Dim c           As Long
    Dim myRange     As Range
    Dim myCell      As Range
    
    'Use the cell value to define the range object directly
    Set myRange = Range(Worksheets("Data").Range("E1").Value)
    
    For Each myCell In myRange
        c = c + 1
        If IsEmpty(myCell) Then
            myCell = 0
            i = i + 1
        End If
    Next myCell
    
    MsgBox "There are total " & i & " Empty cell(s) out of " & c & "."
End Sub
 
Upvote 0
jkpieterse:

Yes, this works perfectly. Thank you!
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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