Error when trying to loop through worksheets and cell ranges

Dreamteam

New Member
Joined
Feb 22, 2018
Messages
28
Office Version
  1. 365
Platform
  1. Windows
Hi

I am trying to loop through worksheets - all with numerical worksheet names and then loop through a range of cells and add some prices. However, I am getting an error each time. I will say though that if I start to run the code when I am actually in one of the relevant worksheets then it completes the task but then can't move on to the next relevant worksheet.

Any help would be much appreciated.

Dt



Code:
Option Explicit


Sub addprices()
    
    Dim startcell As Range
    Dim startrange As Range
    Dim ws As Worksheet
    Dim wb As Workbook
    
    Set wb = ThisWorkbook
    
 
   
    For Each ws In Worksheets
    
        If IsNumeric(ws.Name) Then
    
    
        Set startcell = ws.Range("d2")
        Set startrange = ws.Range("d2", Range("d2").End(xlDown))
        
    
        For Each startcell In startrange
    
        If startcell = "SWAN MONG" Then
            startcell.Offset(0, 1).Value = "12"
            
            Else
            
         If startcell = "COSAWES" Then
            startcell.Offset(0, 1).Value = "20"
            
            Else
            
        If startcell = "BASS ACC" Then
            startcell.Offset(0, 1).Value = "12"
            
            Else
            
         If startcell = "PL PL SAINS" Then
            startcell.Offset(0, 1).Value = "40"
            
            Else
            
         If startcell = "PL PL DRAC" Then
            startcell.Offset(0, 1).Value = "19"
            
            Else
            
            
         If startcell = "TREGEW" Then
            startcell.Offset(0, 1).Value = "15"
            
            Else
            
         If startcell = "BERKLEY COTT" Then
            startcell.Offset(0, 1).Value = "25"
            
            
            
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    
  
            
    Next startcell
    
    End If
    


    Next ws
    
    
    
End Sub
 
Last edited by a moderator:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You need to add the ws. to the second Range on this line
Code:
ws.Range("d2", Range("d2").End(xlDown))
Also when posting code please use code tags, the # icon in the reply window.
 
Upvote 0
Fluff - thank you very much for this. And noted about the tags - apologies
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,216,460
Messages
6,130,765
Members
449,589
Latest member
Hana2911

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