excel vba 2016 Type mismatch 13

JAWHARRAH

New Member
Joined
Feb 19, 2020
Messages
23
Office Version
  1. 2016
Platform
  1. Windows
Good day

I am a newbie and I am desperate.
I keep getting a error type mismatch 13 at runtime but I cannot understand why.
My code is going through the worksheets and I am referencing the codename and I am running into an error.
PLEASE NOTE IT PROCESSES FINE IF I STEP THROUGH THE CODE.
However at runtime I get the type mismatch error.
I have put where it causes the error in **.

my code is

VBA Code:
    For Each ws In ActiveWorkbook.Sheets
        Application.DisplayAlerts = False
        swsCode = Mid(ws.CodeName, 6, 2)
'        If swsCode = "" Then MsgBox "It's empty"
'        If swsCode = "" Then swsCode = "1"
'        MsgBox swsCode

**        iwsCode = CInt(swsCode)  **
       
        If iwsCode > 5 Then
            ws.Copy
            xfile = FolderPath & "\" & Application.ActiveWorkbook.Sheets(1).Name & ".csv"
            ActiveWorkbook.SaveAs xfile, FileFormat:=xlCSV
            ActiveWorkbook.Close savechanges:=False
        End If
   
    Next

Please please help me.
Let me know if you need any more info

Jo
 
Last edited by a moderator:
I followed all those steps and the 5 csv files got created with no error message.
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi Peter
I still get the same error. the codenames are empty but the sheet names are displayed as they should be.
If I run it to the point of the error and save the file, and resume by reopening everything, it still has the same error.
If I go into debug and end the subroutine and rerun it using F5, it works. I don't understand why I am getting a type mismatch error.
I did a repair thinking it might be my Office, but I had the error at home also.
Do you have any other suggestions?
Is there a way the code could simulate - ending at the error, go into debug, and run - without having to hit debug etc... ???
Regards
Jo
 
Upvote 0
Not being able to reproduce the error means that I cannot really investigate alternatives I'm afraid. Not sure what to suggest.
 
Upvote 0
I have redone the file, hopefully this will produce the error.
Open MATERIALS PLAN, (userform will open)
Open 111222_2020_09_07.
Go to the export to CSV page on MATERIALS PLAN userform.
On MATERIALS PLAN select the export to csv button.
It will ask if you are ready to export.
Once you say ok, it goes through the code you gave me, (showing msgboxes with no sheet name).
It should be exporting 4 csv files.
I have tried this a couple of times. I did not go into debug with this file. I just kept closing and reopening it and it gives the error.
Hopefully you can help.
Thanks
Jo
Excel
 
Upvote 0
OK, I am now able to reproduce that error. (y)
I'm still not sure why, but there seems to be an issue with the particular sheet 'Initial Data' (hidden) though not with the other hidden sheets as far as I can tell so far.
Since you are not processing any of the hidden sheets anyway, try these changes. So far (not greatly tested though) I have not had a recurrence of the error with this change in place.

Rich (BB code):
For Each ws In ActiveWorkbook.Sheets
    If ws.Visible Then
      Application.DisplayAlerts = False
      swsCode = Mid(ws.CodeName, 6, 2)
      iwsCode = CInt(swsCode)

      If iwsCode > 5 Then
          ws.Copy
          xfile = FolderPath & "\" & Application.ActiveWorkbook.Sheets(1).Name & ".csv"
          ActiveWorkbook.SaveAs xfile, FileFormat:=xlCSV
          ActiveWorkbook.Close savechanges:=False
      End If
    End If
Next
 
Upvote 0
Hi Peter
First of all I want to thank you very much for all of your time and effort, you have put into this.
Unfortunately that does not work.
It runs through the program, however no files are generated, then when I try and run it again from that point, it comes up with the type mismatch.

I have even tried to run this without the worksheets hidden, but it still does not work.

I don't know what I can do. I don't know why this went wrong, however I have learned a lot from you.
Much appreciated.
Jo
 
Upvote 0
I can't reproduce that behaviour either & it does not make sense. The only other thing that comes to mind is that there could be some corruption in one or both the workbooks.
It might be worth trying to create a new ones from scratch (and copy your codes over) and see what happens then.
 
Upvote 0
Hi Peter
That was a thought some time ago, so I did create a new workbook. It was definitely corrupted though because there were sheets when there were no sheets. see old version below
Jo
Capture.JPG
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

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