if statements with strings

chammy88

Board Regular
Joined
Jun 24, 2011
Messages
56
Hi all,

I'm trying to do something like this:
If RealExp(i) = "any string" then
RealExp(i) = ""
End if

RealExp is an array

my problem is that I don't know what to put for "any string" I thought VB would recognize "string" as any letters in there but it didn't work.

Is there a way I can say if RealExp has words in any of it's rows then delete them?

Thanks
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
If you wanted to erase the string regardless of contents then something like
Code:
 If Len(RealExp(i)) > 0 Then RealExp(i) = vbNullString
But if you want to distinguish between say numbers and alphabetic characters then you need to test the string before deciding to empty it

If it is the later please provide a sample

Regards

Dave
 
Upvote 0
I see no reason to test, unless RealExp is an array with elements that may or may not be strings.

Code:
Redim RealExp(LBound(RealExp) to UBound(RealExp))
'or
For i = LBound(RealExp) To UBound(RealExp)
    RealExp(i) = vbNullString
Next i
'or
For i = LBound(RealExp) To UBound(RealExp)
    If TypeName(RealExp(i)) = "String" Then
        RealExp(i) = vbNullString
    End If
Next i

But, "Is there a way I can say if RealExp has words in any of it's rows then delete them" suggests that RealExp is a Range object. If that is the case

Code:
On Error Resume Next
RealExp.SpecialCells(xlCellTypeConstants, xlTextValues).ClearContents
On Error Goto 0
 
Upvote 0
Hey guys,

thanks for all your suggestions, I have overcome that hurdle now.

I have one other questions that you guys may be able to help me with.

I've got two columns and I want a code to go through the first column and then I want to put some if statements in.
For example:
1. if any of the cells say "Stacking.STK02" I want that cell to display the 5 letters on the right (ie. "STK02)
2. If the text is > 41 characters, only show the last 7 characters.

I have tried numerous methods that I think should work but they are coming up with a "Subscipt out of range error".

If anyone wants me to I can put the code up that I wrote but I think I'd rather see other peoples fresh ideas first.

cheers
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,726
Members
452,939
Latest member
WCrawford

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