Run-time error '13' Type mismatch

micfoste

New Member
Joined
Jul 17, 2008
Messages
19
I'm new to VBA and am running some code but getting a runtime error for the following, can anybody help.

Option Explicit
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 14/07/2010 by mfoster
'
Dim ws As Worksheets

For Each ws In Worksheets
Range("A1:C16").Select
Selection.AutoFormat Format:=xlRangeAutoFormatSimple, Number:=True, Font _
:=True, Alignment:=True, Border:=True, Pattern:=True, Width:=True
Range("A1:C15").Select
Charts.Add

ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=ws.Range("A1:C15"), PlotBy:=xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:=ws.Name '"2002"
Next
End Sub
 

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

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
57,633
Office Version
  1. 365
Platform
  1. Windows
You have declared ws incorrectly (it is WORKSHEET not WORKSHEETS).

By the way, when looping through sheets, you either need to activate them or include the ws reference in your range reference to make it work properly. Otherwise, you will never move off the current worksheet, i.e.

Code:
Dim ws As Worksheet

For Each ws In Worksheets
    ws.Activate
...
Next ws
 

micfoste

New Member
Joined
Jul 17, 2008
Messages
19
You have declared ws incorrectly (it is WORKSHEET not WORKSHEETS).

By the way, when looping through sheets, you either need to activate them or include the ws reference in your range reference to make it work properly. Otherwise, you will never move off the current worksheet, i.e.

Code:
Dim ws As Worksheet
 
For Each ws In Worksheets
    ws.Activate
...
Next ws


Tks for your help
 

Watch MrExcel Video

Forum statistics

Threads
1,133,254
Messages
5,657,622
Members
418,404
Latest member
Sneijders

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
Top