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.
 

Some videos you may like

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Derek

Well-known Member
Joined
Feb 16, 2002
Messages
1,592
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
 

Watch MrExcel Video

Forum statistics

Threads
1,122,509
Messages
5,596,558
Members
414,077
Latest member
ammylar5

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