using =right() and =left() in VBA

lhernandez

Active Member
Joined
May 22, 2006
Messages
282
I know this is very simple question and I have done this before I can just not remeber.
I have a cell the reads either ##C or ##TR.
I am trying to write an if statment stating
if the right value of sheet1 cells(x,y) = "C" then
etc.
else
if the right of sheet1.cells(x,y) = "TR" then
etcs...
end if
Can someone please help me! :)
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,304
Office Version
  1. 365
Platform
  1. Windows
Well I don't think either Left or Right are what you are looking for if there is always 2 characters/digits before what you want to test.
Code:
Select Case Mid(Cells(x,y),3)
       Case "C"
            ' do something
        Case "TR"
            ' do something else
        Case Else
            ' do something completely different
End Select
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
Hi,

surely when you type "left" or "right" in the VBA-helpfiles, you would get your answer
try this
Code:
If Right(Cells(1, 1), 1) = "C" Then MsgBox "hello"
kind regards,
Erik

EDIT: adjust last argument when you need two characters
Code:
If Right(Cells(1, 1), 2) = "CC" Then MsgBox "hello"
 

Forum statistics

Threads
1,141,681
Messages
5,707,795
Members
421,528
Latest member
datdude151

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
Top