VBA & Macro Problems

How to Fix ByRef Argument Type Mismatch for Sine and Cosine in Excel VBA Classes

Elise WilliamsElise Williams Oct 16, 2026 871 views

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.

Fixing ByRef Argument Type Mismatch for Sine and Cosine in Excel VBA Classes
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press ALT + F11 to launch the Visual Basic for Applications Editor in your spreadsheet application.

2
Locate the Class Module

In the Project Explorer pane on the left, double-click your specific Class Module to view its source code.

3
Find and Rename Overloaded Functions

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'.

4
Qualify Built-in Functions

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.

Rename Custom Procedures and Explicitly Qualify Built-in Functions
Best Practice: Always prefix built-in functions with 'VBA.' or 'VBA.Math.' when working inside complex class modules to guarantee you are referencing the native library and avoid namespace conflicts.
Seamless VBA Support

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. 1. Launch WPS Spreadsheet: Open WPS Office and create a new Spreadsheet or open your existing macro-enabled workbook.
  2. 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. 3. Insert Your Code: Right-click in the Project window, select 'Insert', and choose 'Class Module' to paste or write your VBA code.
  4. 4. Run and Debug: Use the built-in Run (F5) and Step Into (F8) tools to execute your trigonometric calculations smoothly.
Full compatibility with Microsoft Excel macro-enabled formats (.xlsm and .xltm)Advanced VBA Editor with built-in debugging and stepping toolsFree and lightweight office suite for fast code executionSeamless migration of complex class modules and user forms
microsoft office alternative - wps office

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.