handling error works only once

haseft

Active Member
Joined
Jun 10, 2014
Messages
321
Hi!
On Sheet1 I have some text:
MytextA (cell A2)
MytextB
MytextC
MytextD
MytextE
MytextF

On Sheet2 I have some text:
MytextA Cells(j, 1)
MytextC
MytextE

Onerror works only once, Please help!

Code:
Sub FindText()
Dim a As String, n As String, z  As String, j As String
a = "StrgMal"
n = 1
z = a & n
j = 2
 
Application.ScreenUpdating = True
Worksheets("Sheet1").Activate
Range("A2").Select
 Do While ActiveCell.Value <> ""
        Sheets("Sheet2").Select
        Err.Clear
        On Error GoTo JumpToHere
        Columns("D").Find(What:=Sheets("Sheet1").Cells(j, 1)).Activate
        ActiveCell.Name = z
       n = n + 1
       z = a & n
 JumpToHere:
       Err.Clear
       j = j + 1
       Worksheets("Strategiskt_mål").Activate
       ActiveSheet.Cells(j, 1).Selec
Loop
End Sub
 
Last edited by a moderator:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try
Code:
Sub FindText()
   Dim a As String, n As String, z  As String, j As String
   Dim Fnd As Range
   a = "StrgMal"
   n = 1
   z = a & n
   j = 2
   
   Application.ScreenUpdating = False
   Worksheets("Sheet1").Activate
   Range("A2").Select
   Do While activecell.Value <> ""
      Sheets("Sheet2").Select
      Set Fnd = Columns("D").find(What:=Sheets("Sheet1").Cells(j, 1))
      If Not Dn Is Nothing Then
         Fnd.Name = z
         n = n + 1
         z = a & n
      End If
      j = j + 1
      Worksheets("Strategiskt_mål").Activate
      ActiveSheet.Cells(j, 1).Selec
   Loop
End Sub
If you want to know why he error handler wasn't working as expected have a look at this
http://excelmatters.com/2015/03/17/on-error-wtf/
 
Upvote 0
Solution
Tansk, its workning .

Sub FindText()
Dim a As String, n As String, z As String, j As String
Dim Dn As Range
a = "StrgMal"
n = 1
z = a & n
j = 2

Application.ScreenUpdating = False
Worksheets("Sheet1").Activate
Range("A2").Select
Do While ActiveCell.Value <> ""
Sheets("Sheet2").Select
Set Dn = Columns("D").Find(What:=Sheets("Sheet1").Cells(j, 1))
If Not Dn Is Nothing Then
Dn.Select
Dn.Name = z
n = n + 1
z = a & n
End If
j = j + 1
Worksheets("Sheet1").Activate
ActiveSheet.Cells(j, 1).Select
Loop
End Sub
 
Last edited:
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,633
Messages
6,125,928
Members
449,274
Latest member
mrcsbenson

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