Hi, Can you help to issue "Type Mismatch" encountered when running macro of this file.2A.xlsm
Microsoft OfficeVBAMicrosoft Excel
Last Comment
byundt
8/22/2022 - Mon
zorvek (Kevin Jones)
The macro doesn't do much. No type error when I ran it.
Post a workbook that generates the error to get help.
Kevin
byundt
I am not reproducing the problem when I run the macro. Does the sample workbook need data for the error to appear?
Also, you probably want to move the macro from ThisWorkbook to a regular module sheet as in the attached version of your workbook. ThisWorkbook and the worksheet code panes are class modules, and should be used by the event subs they contain rather than general purpose macros.
I recommend you declare your integer variables as Long rather than Integer. Long is a "long integer" while Integer is a "short integer" though both variable types occupy the same amount of memory. An Integer variable will overflow if you try to give it a value more than 32,767. This causes a runtime error if you are looping though the rows on a large worksheet. Rather than think about whether an overflow might occur, I just declare all integer variables as Long--and recommend you do so as well.
To add to what Brad stated, we recommend putting general routines in general code modules - right-click anywhere in the project tree and select Insert->Code Module.
ThisWorkbook and worksheet modules are class modules but they are persistent classes which means an instance of that class object is instantiated at all times and any properties, methods (Subs), and functions are readily accessible by prefacing the routine name with "ThisWorkbook." or "Sheet1." Once running, the code will run exactly as it would in a general code module.
Note that any routines defined in the ThisWorkbook or a sheet module will not be exposed as a macro when using the Macros dialog. But you can refer to it in a command button's (or any shape's) OnAction property by prefacing the routine name with "ThisWorkbook."
Kevin
zorvek (Kevin Jones)
Peter,
Posting a picture of the error does not help us. We need a workbook that we can use to reproduce the error. Or show us the line of code that is encountering the error.
Peter Chan, I'm running the Beta Channel version of Microsoft 365. This is the latest version of Excel that has been released outside Microsoft. I do not get the error you show, so you must be running a different version of Excel.
I did encounter a Type 6 overflow error when I put 40,000 rows of data in your workbook. The fix for that is to change the Integer declarations to Long.
Brad
byundt
Workbook that I was testing. Variables were declared as Long.
I just tested the workbook in Excel 2013, and could not reproduce the "Type Mismatch" error there, either.
Please post a workbook that reproduces the error. Tell us exactly which version of Excel you are using. Ideally, click the Debug choice when you get the error and tell us which statement is having problems and what the input values are for it.
For debugging purposes, I added a statement On Error GoTo Errhandler. And at the bottom of the sub, Errhandler said Resume--and I put a watch on it so the code would stop there while I investigated what was happening. The Type Mismatch error was occurring on column C 1800-22 and an overflow on 750-2058247. The Type Mismatch occurred when you tried to return two characters from beyond the right end of "1800-22." The overflow occurred because 2058247 is too big to fit in an integer variable
byundt
This version of the file removes my debugging statements, but includes the Long and CLng fixes.
Post a workbook that generates the error to get help.
Kevin