VBA Syntax: Writing as Text, not Number

OldDogNewTricks

New Member
Joined
Jun 27, 2014
Messages
21
How do I have VBA understand that my content is text, not numbers? I keep getting an error when I try to run my code because my words have dashes in them.

Here is the part of my code that is giving me issues:
Word = "I-BUTANE"
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
What is the error? This works for me

Code:
Sub test()
Dim word As String
word = "I-BUTANE"
MsgBox word
End Sub
 
Upvote 0
You are going to have to tells us more about where the word is coming from and exactly what you are attempting to do with it. Also, if you show us your non-working code, that maybe also give us a better understanding of the description I just asked you to provide.
 
Upvote 0
Here is my code:

Dim X As Long, Index As Long, Word As String
Word = "I-BUTANE"
If Len(Word) Then
For X = 1 To Cells(Rows.Count, "A").End(xlUp).Row
If UCase(Trim(Cells(X, "A").Value)) = UCase(Word) Then
Index = Index + 1
Rows(X).Name = Word & Index
End If
Next
End If




I have a column filled with various chemicals. The order changes, but each chemical will repeat itself ten times. Whenever a row contains "I-BUTANE" in the first column, the entire row will be labeled "I-BUTANE#," where # changes with each appearance (first time you see I-BUTANE, the row is labeled I-BUTANE1).

I keep getting an error when I run my code for all the chemicals that contain a "-" in it. I am assuming Excel does not consider this to be a string.

Does this help?
 
Upvote 0
I believe a hyphen is an invalid character for Range Names.

I would suggest replacing the - with _

Rows(X).Name = Replace(Word, "-", "_") & Index
 
Upvote 0

Forum statistics

Threads
1,215,412
Messages
6,124,761
Members
449,187
Latest member
hermansoa

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top