How to stop macro when reached a named cell?

Ingemar

New Member
Joined
May 8, 2017
Messages
23
Office Version
  1. 365
Platform
  1. Windows
Hello everybody,

I have tried to write a macro checking if cells are empty or not between the named cells "First_sample" and "Last_sample". I start checking the cell below "First_sample" and I want the macro to run until reaching the cell named "Last_sample". The loop works fine first time but second time I get the error message Run-time error '1004': Application defined or object defined error. The code is as below. I am greatful for any help or suggestion.

Kind regards

Ingemar

Sub Macro1()

Worksheets("Analysis sheet").Range("First_sample").Offset(1, 0).Select

Do
On Error GoTo Section2
If ActiveCell.Name.Name = "Last_sample" Then
Exit Sub
End If

Section2:

If IsEmpty(ActiveCell) Then
Selection.Offset(1).Select

Else

'Macro process to be done
End If

Loop

End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
What do you want to do if the are blank cells in the range?
 
Upvote 0
Why not loop through the entire range like this?
VBA Code:
For Each cel In Worksheets("Analysis sheet.Range("First_sample, Last_Sample").Cells

Next cel
 
Upvote 0
Hello Fluff, If a cell is empty, then the macro will select next cell below. Code:
If IsEmpty(ActiveCell) Then
Selection.Offset(1).Select
 
Upvote 0
Are you simply trying to find the first blank cell in that range?
If so what should happen if there are no blank cells?
 
Upvote 0
Hi again, No, the question is not to find first blank cell. The macro shall check cells, disregard them by selecting next cell below. If cell is not empty, the macro shall do a process, but this is not my question. The question is how to stop the macro when reaching the cell named "Last_sample". I have tried the code below. It works the first loop but not the second loop.
Sub Macro1()
'
'

Worksheets("Analysis sheet").Range("First_sample").Offset(1, 0).Select


Do
On Error GoTo Section2
If ActiveCell.Name.Name = "Last_sample" Then
Exit Sub
End If

Section2:

If IsEmpty(ActiveCell) Then
Selection.Offset(1).Select

Else

'Macro process to be done
End If

Loop

End Sub
 
Upvote 0
In that case try
Code:
Sub Ingemar()
    Dim Cl As Range
    
    With Range("first_sample", "last_sample")
        For Each Cl In .Offset(1).Resize(.Count - 2)
            If Cl.Value <> "" Then
                'do something
            End If
        Next Cl
    End With
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,509
Members
449,089
Latest member
RandomExceller01

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