conditional assignment

exceltadpole

Board Regular
Joined
Jan 14, 2004
Messages
108
Consider this code snippet:


Dim selectedCell As Range

' ......
' several lines of code that handle the selectedCell variable go here
' ......

Sheets("items").Range("j34") = selectedCell.Offset(0, 5).Value



The above works correctly.
Here is a sample of values that may be found in selectedCell.Offset(0, 5)
AAA987
NMEK9745
PJ36ED9W
AAMPUERF9
DGK73

How can I include the following to the above code:
Before the assignment of "Sheets("items").Range("j34") = selectedCell.Offset(0, 5).Value" happens, the "selectedCell.Offset(0, 5).Value" must be checked.
If the first 2 characters contain "AA" then continue with the above assignment.
If the first 2 characters do not contain "AA" then just do a MsgBox "AA not found".
Given the above sample, only 2 values "AAA987" and "AAMPUERF9" would be assigned.

Not being a VBA expert, I am having difficulty with this conditional assignment.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi there

You could try code something like this

Dim selectedCell As Range
Set selectedCell = Range(ActiveCell.Address)

If Left(selectedCell.Offset(0, 5).Value, 2) = "AA" Then
Msgbox "AA found"
Else
MsgBox "AA not found"
End If
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,246
Members
449,075
Latest member
staticfluids

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