If statement and variables

Buskopan

Board Regular
Joined
Aug 4, 2014
Messages
54
Hello
Cell N15 may contain the text
N 003.
N 004.
N 022.
N 040.
N 049.
N 071.
N 073.
N 081.
N 087.
N 090.
N 091.
N 093.
N 095.
N 098.
N 099.
N 103.
N 115.
N 119.
N 121.
N 127.
N 133.
N 146.
N 164.
N 166.
N 169.
N 173.
N 179.
N 180.
N 184.
N 186.
N 187.
N 196.
N 198.
N 205.

<tbody>
</tbody>

Depending on text in cell I want macro to cellect the sertain cells. If N15 contains "N 003." then select G2, if text in N15 if "N 004." Then sellect "G3" etc

I can do it this way.

Code:
    If Range("N15").Text = "N 003." Then
    Range("G2").Select
    End If
     
   If Range("N15").Text = "N 004." Then
    Range("G3").Select
    End If
        
   If Range("N15").Text = "N 022." Then
    Range("G4").Select
    End If
        
   If Range("N15").Text = "N 040." Then
    Range("G5").Select
    End If

and so on.

but how I can shorten the code to not type all ranges ?

Thank you
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try like this

Code:
Select Case Range("N15").Text
    Case "N 003.": Range("G2").Select
    Case "N 004.": Range("G3").Select
' and so on
End Select
 
Upvote 0
I don't understand why letter N corresponds to column G. Or what letter M would go to.
But something like this might work.

Code:
Dim strValue As String

strValue = CStr(Range("N15").Value)

Range("G" & (Mid(strValue, 2) - 1)).Select
 
Upvote 0

Forum statistics

Threads
1,213,497
Messages
6,113,999
Members
448,541
Latest member
iparraguirre89

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