View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
FSt1 FSt1 is offline
external usenet poster
 
Posts: 3,942
Default Using a variable in an If statement

hi
guessing here but there might be something wrong with this line.....
strGetCostCenter = """" & "790-30-00" & """" & " OR Temp = " & """" &
ActiveCell.Value & """"""
i suspect that you may have too many double quotes at the end. I pasted the
line in to my vb editor and display the variable in a msgbox. the "extra"
quotes showed up there. i reduced the quottes to 4 and the "extra" quotes
went away.
how are you getting temp??

regards
FSt1
"Rob" wrote:

Matt,

The problem that I have found is that if I type it out like this:

If Temp = "790-30-00" Or Temp = "981107" or Temp = "981022" then
place my statements here
else
place my statements here
endif

it works just fine. However, when I use the process to store "790-30-00"
Or Temp = "981107" or Temp = "981022" to the variable strGetCostCenter it
goes right to the Else. I can't figure out why - I even had a msgbox show
the results of strGetCostCenter to make sure that it is showing properly -
and it is.

" wrote:

On Mar 16, 9:04 pm, Rob wrote:
Hello All,

Is there a way to use a variable in an If statement? I have collected some
information in one part of my subroutine and would like it to be the
conditional of the If statement like this:

Workbooks(orgFile).Worksheets("Cost Centers").Activate
Range("C2").Select
strGetCostCenter = """" & "790-30-00" & """" & " OR Temp = " & """" &
ActiveCell.Value & """"""
ActiveCell.Offset(1, 0).Range("A1").Select

Do Until ActiveCell = ""
strGetCostCenter = strGetCostCenter & " OR Temp = " & """" &
ActiveCell.Value & """"
ActiveCell.Offset(1, 0).Range("A1").Select
Loop

If Temp = strGetCostCenter Then

endif

If have looked at the results of the strGetCostCenter variable and it
matches exactely what I would use manually in the If statement.

Any suggestions would be greatly appreciated.

Regards,

Rob


Rob,

I'm not quite sure what you are asking for, but I think you have all
of your pieces in your code already. I've created a simple example
below.

Best,

Matt Herbert

If Temp = strGetCostCenter Then

endif


If Temp = strGetCostCenter Then
'insert your code here
End If

'-----------------------

Sub TestVarInIf()
Dim strName As String
Dim strMyName As String

strName = Range("a1").Value
strMyName = "Rob"

If strName < strMyName Then
MsgBox "The name does not match." & vbLf & vbLf & "The name is: "
& strMyName
End If

End Sub