How to Fix ByRef Argument Type Mismatch for Sine and Cosine in Excel VBA Classes
Question details
The user encounters a ByRef argument type mismatch error when attempting to use built-in Sine and Cosine functions inside a VBA class module.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- Writing or executing mathematical VBA code using trigonometric functions (Sin, Cos) within a custom class module environment.
- Observed behavior
- A ByRef argument type mismatch error is thrown for Sin and Cos calls, while the Tangent function executes correctly. This is caused by accidentally overloading or redefining 'Sin' and 'Cos' as custom properties or methods within the class.
Ensure you have access to the VBA Editor (ALT + F11) and have a basic understanding of your custom class module's properties, methods, and variables.
Rename Custom Procedures and Explicitly Qualify Built-in Functions
Resolve naming conflicts by ensuring no custom variables or methods share the names of native VBA trigonometric functions, and explicitly reference the VBA math library.
This error is not an inherent Excel VBA bug. It occurs because a variable, function, or property within your class module has been named 'Sin' or 'Cos', which shadows the built-in VBA trigonometric functions. When VBA expects a double numeric value for the native math function but encounters a custom object or mismatched variable type instead, it triggers a ByRef argument type mismatch.
Press ALT + F11 to launch the Visual Basic for Applications Editor in your spreadsheet application.
In the Project Explorer pane on the left, double-click your specific Class Module to view its source code.
Use the search function (CTRL + F) to look for any custom procedures, properties, or Dim statements named 'Sin' or 'Cos'. Rename these to something descriptive, such as 'CustomSin' or 'ClassCos'.
Update your code to explicitly call the built-in functions by adding the library prefix. Change your math calculations to use 'VBA.Math.Sin()' or 'VBA.Math.Cos()' and ensure you pass a valid Double numeric argument.

Write and Execute VBA Code Smoothly in WPS Spreadsheet
WPS Office provides robust, integrated support for VBA and macros. You can seamlessly write, edit, debug, and run your class modules with full syntax compatibility.
- 1. Launch WPS Spreadsheet: Open WPS Office and create a new Spreadsheet or open your existing macro-enabled workbook.
- 2. Access the VBA Editor: Navigate to the 'Tools' tab on the ribbon and click on 'VBA Editor', or simply press ALT + F11 to open the integrated development environment.
- 3. Insert Your Code: Right-click in the Project window, select 'Insert', and choose 'Class Module' to paste or write your VBA code.
- 4. Run and Debug: Use the built-in Run (F5) and Step Into (F8) tools to execute your trigonometric calculations smoothly.

Frequently Asked Questions
Why does Tangent work while Sine and Cosine fail in my VBA class?
Tangent (Tan) usually works without issues because developers are less likely to accidentally name custom variables or properties 'Tan'. 'Sin' and 'Cos' are much more commonly used as custom names in mathematical or physics-related class modules, leading to shadowing conflicts.
What does 'ByRef argument type mismatch' mean in VBA?
This error occurs when you pass a variable of one data type to a procedure that explicitly expects a reference (ByRef) to a different data type. For native math functions, VBA expects a specific numeric type (like Double), and shadowing the function name can confuse the compiler about what parameters are required.
How do I pass numeric arguments with the correct data type in VBA?
You can explicitly declare your variables using the 'As' keyword (e.g., 'Dim myValue As Double') before passing them into the mathematical function. Alternatively, you can use conversion functions like CDbl() to ensure the value is treated as a Double.




