A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data
Dear @Les M,
Thank you for reaching out to the Microsoft 365 Q&A forum community support team. I completely understand the frustration when a script works perfectly for some inputs but throws an error for others. Troubleshooting logical errors like "Type Mismatch" can be incredibly time-consuming, especially when the cause isn't immediately visible in the cell formatting.
The reason you are seeing this error specifically for December and February is likely due to how Application.VLookup behaves:
- Missing Data or Range: Your lookup range is set to BL4:BM8, which only covers 5 rows. If December or February are not within those 5 rows, or if there is a tiny typo (like a hidden space) in those cells, VLookup returns an error value (like #N/A).
- Variable Type Conflict: You have yr defined as an Integer. An Integer can only hold numbers. When VLookup fails and returns an error code, VBA tries to "force" that error into your Integer variable, resulting in the Type Mismatch.
To fix this, change your variable type to Variant. This allows the variable to hold either the year or the error code without crashing, letting us handle the problem gracefully.
For more details, please refer to Microsoft’s official documentation:
VLOOKUP function - Microsoft Support
Type mismatch (Error 13) | Microsoft Learn
We truly appreciate you're understanding that our Q&A forum has limited resources for deep code troubleshooting. Complex projects often require detailed code examples and extensive testing that go beyond the scope of this forum.
My goal is to guide you in the right direction for immediate fixes, but for advanced scripting support, the best place is Stack Overflow. The community there is very active and can provide precise, specialized guidance for Newest 'vba' Questions - Stack Overflow
When posting there, it is helpful to include:
- A clear description of the problem.
- Any specific error messages (like "Error 13").
- The relevant parts of your script and a sample of your data.
I hope the above information is clear. Thank you for your understanding and cooperation. Please let me know how it goes after trying the steps above. I’m here to help further if needed.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.