Why is Excel VBA letting me multiply strings?

GBarnes

New Member
Joined
Sep 30, 2011
Messages
1
I WANT Excel to tell me that I can't multiply a string. What am I missing?

Code:
Option Explicit

Sub why()
Dim myVariable As String
Debug.Print VarType(myVariable), TypeName(myVariable), myVariable
myVariable = 42 'Not "42"
myVariable = myVariable * 3 '126   Why?
Debug.Print VarType(myVariable), TypeName(myVariable), myVariable
End Sub
 
Last edited:

Some videos you may like

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,278
Implicit Coercion

Many programming languages support the conversion of a value into another of a different data type. This kind of type conversions can be implicitly or explicitly made. Implicit conversion, which is also called coercion, is automatically done. Explicit conversion, which is also called casting, is performed by code instructions. This code treats a variable of one data type as if it belongs to a different data type. The languages that support implicit conversion define the rules that will be automatically applied when primitive compatible values are involved.
 

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,446
Office Version
  1. 2013
Platform
  1. Windows
there are several dissertations on this by people like John Walkenbach and Chip Pearson and others where they discuss how a number is a number by any other name and VBA will treat a number as a number, with exceptions. But they are generally referring to math functions when they talk about the numbers being numbers no matter what else you call them. The string data type allows us to use numbers for such things as sheet names or to use leading zeros, etc. But they are still numbers.
 

Watch MrExcel Video

Forum statistics

Threads
1,109,336
Messages
5,528,104
Members
409,802
Latest member
joeino

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top