VBA IF Statement based on multiple cell values and return text based on a value range/beginning with

peng_p

New Member
Joined
Mar 11, 2020
Messages
10
Platform
  1. Windows
Hi, I'm trying to learn how to code a macro for a spreadsheet I am working on and I am stuck at this part of the process:

1583936688779.png


What I am looking for:
If column O value is greater than zero then
If column Q value is between 0400 to 0499, then print "ON" in column P else
If column Q value is between 0300 to 0399, then print "MB" in column P else
So on and so forth...
It is a running list, so there could be 10 rows of data or 1000.

Alternatively, instead of a between check for column Q, would it be easier to have it check the first two digits instead? i.e. 04, 03, etc.

Sorry if this is very simplistic, I am still trying to learn and my online resources haven't helped me. Thank you for helping.
 
...I've only just seen that I didn't include a test for col "O" values being >0.
Here's the amended code:
VBA Code:
Private Sub CommandButton1_Click()
Dim pcodes As String
Dim cl As Range
Dim rng As Range
Dim lstrw As Integer

lstrw = Me.Range("Q10000").End(xlUp).Row
Set rng = Me.Range("Q2:Q" & lstrw)
pcodes = "ONMBHHYTURFDNMTTBNGHYTRED"
For Each cl In rng
    If cl.Offset(0, -2).Value > 0 Then  cl.Offset(0, -1).Value = Mid(pcodes, (Left(cl.Value, 2) * 2) - 1, 2)
Next
End Sub
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
04 - ON
03 - QB
05 - MB
08 - BC
etc.

I also want to make sure that it is only checking this column after checking that column O is greater value than zero, else it should leave it blank.
 
Upvote 0
Saying etc. does not help. I need to know all the possible codes in order to come up with a solution.
 
Upvote 0
For one last time, can you please supply all the codes/abbreviations.
 
Upvote 0
In that case try
VBA Code:
Public Sub peng()
   Dim Cl As Range
   Dim Ary As Variant
  
   Ary = Array("QB", "ON", "MB", "", "", "BC")
   For Each Cl In Range("Q3", Range("Q" & Rows.Count).End(xlUp))
      If Cl.Offset(, -2) > 0 Then
         Cl.Offset(, -1) = Ary(Left(Cl, 2) - 3)
      End If
   Next Cl
End Sub
 
Upvote 0
Amazing, thank you so much! It works well!

I think I understand the coding and your reasoning to know the constant value/abbreviation now when I review it.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,885
Messages
6,122,090
Members
449,065
Latest member
Danger_SF

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