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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
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,224,899
Messages
6,181,629
Members
453,058
Latest member
rmd0725

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