Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Excel Assigning Worksheets to Variables

Tags:

excel

vba

I am having a "Run-time Error '13' Type Mismatch" when attempting to assign a worksheet to a variable. It works for all of the other worksheets but is seems to only happen to this worksheet.

Here is my code:

Sub TS()
Dim RD, Dep, QC, MM, Pro As Workbook

With ThisWorkbook
Set RD = .Sheets("RawData")
Set Dep = .Sheets("Departments")
Set QC = .Sheets("QC")
Set MM = .Sheets("MM")
Set Pro = .Sheets("Production") 'I have a problem with this line
End With

End Sub

My worksheet labels are below:

enter image description here

like image 342
Spectator Avatar asked Jan 29 '26 05:01

Spectator


1 Answers

The error is pertinent to your erroneous declaration:

Pro As Workbook

You should declare it as Worksheet, or just variant without specifying the type, like:

Dim RD, Dep, QC, MM, Pro

A better way would be to use the strong typed declaration:

Dim RD As Worksheet, Dep As Worksheet, QC As Worksheet, MM As Worksheet, Pro As Worksheet

Hope this may help.

like image 119
Alexander Bell Avatar answered Jan 30 '26 21:01

Alexander Bell