VBA Error handling Loop

fcarboni

New Member
Joined
Aug 15, 2018
Messages
2
Hi All

I have a sheet called all data and multiple other sheets for individuals.
Based on a defined cell value the macro copies all rows from "All Data" that match the value of the individual.
This works fine when there is a sheet for each unique individual but where there is data for someone but not a sheet the macro stops on "set tws" . I have tried the below but it still fails

if it finds a cell that does not match any sheet I want it to skip the row and look at the next one
as well the below, at the same point, I have tried 'on error goto next'. this does run but copies in the row Evan if it doesn't match.


Dim sws As WorksheetDim tws As Worksheet
Dim cel As Range
For Each ws In Worksheets
Select Case ws.Range("B1")
Case Is <> "MP"
'Do nothing
Case Else
Set sws = Sheets("All Data")
For Each cel In sws.Range("H4:H" & Range("H" & Rows.Count).End(xlUp).Row)

----Here I have tried 'on error go to MP:


Set tws = Sheets(cel.Value)
cel.EntireRow.Copy tws.Range("A" & Rows.Count).End(xlUp).Offset(1)

-----MP:


Next cel
Exit For
End Select
Next ws


Thanks V much
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Welcome to the forum. Please use code tags around code; it makes it easier. You could try like this:

Code:
On Error Resume Next

Dim sws As Worksheet
Dim tws As Worksheet
Dim cel As Range

For Each ws In Worksheets
    Select Case ws.Range("B1")
        Case Is <> "MP"
            'Do nothing
        Case Else
            Set sws = Sheets("All Data")
            For Each cel In sws.Range("H4:H" & Range("H" & Rows.Count).End(xlUp).Row)
                Set tws = Nothing
                Set tws = Sheets(cel.Value)
                If Not (tws Is Nothing) Then
                    cel.EntireRow.Copy tws.Range("A" & Rows.Count).End(xlUp).Offset(1)
                End If
            Next cel
            Exit For
    End Select
Next ws

WBD
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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