VBA code

The Idea Dude

Well-known Member
Joined
Aug 15, 2002
Messages
591
Office Version
  1. 2016
Platform
  1. Windows
I am trying to create a macro that does the follwing (I just cant get the syntax correct):

If active cell background colourindex = 1 then msgbox ""

NOTE: whenever a cell is selected, this macro needs to run
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If ActiveCell.Interior.ColorIndex = 1 Then MsgBox ""

End Sub
 
Upvote 0
Using 'target' rather than 'activecell' may be a better idea:<pre>Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Interior.ColorIndex = 1 Then MsgBox "Bingo!"
End Sub</pre>
Edit:
If you want to ignore multi-cell selections then use target.interior; if you want to trap ANY selection having the active cell in the upper left, then use activecell.interior.
This message was edited by Swamp Thing on 2002-09-15 20:45
 
Upvote 0
On 2002-09-15 20:23, The Idea Dude wrote:
I am trying to create a macro that does the follwing (I just cant get the syntax correct):

If active cell background colourindex = 1 then msgbox ""

NOTE: whenever a cell is selected, this macro needs to run

<pre/>
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Interior.ColorIndex = 1 Then
MsgBox "Here"
End If
End Sub
</pre>

<META content=Excel.Sheet name=ProgId><META content="Microsoft Excel 9" name=Generator><LINK href="./sht_files/filelist.xml" rel=File-List><STYLE id=AAA_Instructions_VBAEnter_5820_Styles></STYLE>  <DIV id=AAA_Instructions_VBAEnter_5820 align=center x:publishsource="Excel"><TABLE style="TABLE-LAYOUT: fixed; WIDTH: 424pt; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 width=564 border=0 x:str><COLGROUP><COL style="WIDTH: 23pt; mso-width-source: userset; mso-width-alt: 1097" width=30><COL style="WIDTH: 39pt; mso-width-source: userset; mso-width-alt: 1901" width=52><COL style="WIDTH: 320pt; mso-width-source: userset; mso-width-alt: 15616" width=427><COL style="WIDTH: 23pt; mso-width-source: userset; mso-width-alt: 1097" width=30><COL style="WIDTH: 19pt; mso-width-source: userset; mso-width-alt: 914" width=25><TBODY><TR style="HEIGHT: 12.75pt" height=17><TD class=xl285820 style="WIDTH: 23pt; HEIGHT: 12.75pt" width=30 height=17> </TD><TD class=xl285820 style="WIDTH: 39pt" width=52> </TD><TD class=xl285820 style="WIDTH: 320pt" width=427> </TD><TD class=xl285820 style="WIDTH: 23pt" width=30> </TD><TD class=xl285820 style="WIDTH: 19pt" width=25> </TD></TR><TR style="HEIGHT: 15pt" height=20><TD class=xl285820 style="HEIGHT: 15pt" height=20> </TD><TD class=xl315820>STEP</TD><TD class=xl315820>HOW</TD><TD class=xl245820> </TD><TD class=xl285820> </TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD class=xl285820 style="HEIGHT: 12.75pt" height=17> </TD><TD class=xl335820 align=right x:num>1</TD><TD class=xl295820 style="WIDTH: 320pt" width=427><SPAN style="mso-spacerun: yes"> </SPAN>Press Alt-F11 to display the Visual Basic editor (VBE).</TD><TD class=xl325820> </TD><TD class=xl285820> </TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD class=xl285820 style="HEIGHT: 12.75pt" height=17> </TD><TD class=xl305820><U style="VISIBILITY: hidden; mso-ignore: visibility"> </U></TD><TD class=xl255820 style="WIDTH: 320pt" width=427> </TD><TD class=xl225820> </TD><TD class=xl285820> </TD></TR><TR style="HEIGHT: 51pt" height=68><TD class=xl285820 style="HEIGHT: 51pt" height=68> </TD><TD class=xl335820 align=right x:num>2</TD><TD class=xl295820 style="WIDTH: 320pt" width=427><SPAN style="mso-spacerun: yes"> </SPAN>In the Project Explorer window, find an entry for the sheet you're working with. (For example, look for Sheet1 if the cells you're formatting are on that sheet.) If you don't see the Project Explorer window, press Ctrl-R.</TD><TD class=xl235820> </TD><TD class=xl285820> </TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD class=xl285820 style="HEIGHT: 12.75pt" height=17> </TD><TD class=xl305820><U style="VISIBILITY: hidden; mso-ignore: visibility"> </U></TD><TD class=xl255820 style="WIDTH: 320pt" width=427> </TD><TD class=xl225820> </TD><TD class=xl285820> </TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD class=xl285820 style="HEIGHT: 12.75pt" height=17> </TD><TD class=xl335820 align=right x:num>3</TD><TD class=xl295820 style="WIDTH: 320pt" width=427><SPAN style="mso-spacerun: yes"> </SPAN>Double-click the sheet entry in the Project Explorer window.</TD><TD class=xl325820> </TD><TD class=xl285820> </TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD class=xl285820 style="HEIGHT: 12.75pt" height=17> </TD><TD class=xl305820><U style="VISIBILITY: hidden; mso-ignore: visibility"> </U></TD><TD class=xl255820 style="WIDTH: 320pt" width=427> </TD><TD class=xl225820> </TD><TD class=xl285820> </TD></TR><TR style="HEIGHT: 38.25pt" height=51><TD class=xl285820 style="HEIGHT: 38.25pt" height=51> </TD><TD class=xl335820 align=right x:num>4</TD><TD class=xl295820 style="WIDTH: 320pt" width=427><SPAN style="mso-spacerun: yes"> </SPAN>Above the code window, you'll find two drop-down lists. In the list on the left, choose Worksheet. In the list on the right, choose SelectionChange.</TD><TD class=xl235820> </TD><TD class=xl285820> </TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD class=xl285820 style="HEIGHT: 12.75pt" height=17> </TD><TD class=xl305820><U style="VISIBILITY: hidden; mso-ignore: visibility"> </U></TD><TD class=xl255820 style="WIDTH: 320pt" width=427> </TD><TD class=xl225820> </TD><TD class=xl285820> </TD></TR><TR style="HEIGHT: 38.25pt" height=51><TD class=xl285820 style="HEIGHT: 38.25pt" height=51> </TD><TD class=xl335820 align=right x:num>5</TD><TD class=xl295820 style="WIDTH: 320pt" width=427><SPAN style="mso-spacerun: yes"> </SPAN>A procedure template for Worksheet_SelectionChange now appears in the code window. Between the Private Sub line and the End Sub line, type the name of your conditional formatting procedure.</TD><TD class=xl325820> </TD><TD class=xl285820> </TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD class=xl285820 style="HEIGHT: 12.75pt" height=17> </TD><TD class=xl305820><U style="VISIBILITY: hidden; mso-ignore: visibility"> </U></TD><TD class=xl255820 style="WIDTH: 320pt" width=427> </TD><TD class=xl225820> </TD><TD class=xl285820> </TD></TR><TR style="HEIGHT: 25.5pt" height=34><TD class=xl285820 style="HEIGHT: 25.5pt" height=34> </TD><TD class=xl345820> </TD><TD class=xl255820 style="WIDTH: 320pt" width=427>For example your Worksheet_SelectionChange procedure would look like this:</TD><TD class=xl325820> </TD><TD class=xl285820> </TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD class=xl285820 style="HEIGHT: 12.75pt" height=17> </TD><TD class=xl275820> </TD><TD class=xl255820 style="WIDTH: 320pt" width=427> </TD><TD class=xl225820> </TD><TD class=xl285820> </TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD class=xl285820 style="HEIGHT: 12.75pt" height=17> </TD><TD class=xl345820> </TD><TD class=xl255820 style="WIDTH: 320pt" width=427>Private Sub Worksheet_SelectionChange(ByVal Target As Range)</TD><TD class=xl325820> </TD><TD class=xl285820> </TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD class=xl285820 style="HEIGHT: 12.75pt" height=17> </TD><TD class=xl275820> </TD><TD class=xl255820 style="WIDTH: 320pt" width=427><SPAN style="mso-spacerun: yes">   </SPAN>If Target.Interior.ColorIndex = 1 Then</TD><TD class=xl225820> </TD><TD class=xl285820> </TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD class=xl285820 style="HEIGHT: 12.75pt" height=17> </TD><TD class=xl345820> </TD><TD class=xl255820 style="WIDTH: 320pt" width=427><SPAN style="mso-spacerun: yes">      </SPAN>MsgBox "Here"</TD><TD class=xl325820> </TD><TD class=xl285820> </TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD class=xl285820 style="HEIGHT: 12.75pt" height=17> </TD><TD class=xl275820> </TD><TD class=xl255820 style="WIDTH: 320pt" width=427><SPAN style="mso-spacerun: yes">   </SPAN>End If</TD><TD class=xl225820> </TD><TD class=xl285820> </TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD class=xl285820 style="HEIGHT: 12.75pt" height=17> </TD><TD class=xl345820> </TD><TD class=xl265820 style="WIDTH: 320pt" width=427>End Sub</TD><TD class=xl325820> </TD><TD class=xl285820> </TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD class=xl285820 style="HEIGHT: 12.75pt" height=17> </TD><TD class=xl285820> </TD><TD class=xl285820> </TD><TD class=xl285820> </TD><TD class=xl285820> </TD></TR></TBODY></TABLE></DIV>
 
Upvote 0
If you want to ignore multi-cell selections then use target.interior; if you want to trap ANY selection having the active cell in the upper left, then use activecell.interior.
This message was edited by Swamp Thing on 2002-09-15 20:45

Thanks for your advise
 
Upvote 0

Forum statistics

Threads
1,214,808
Messages
6,121,684
Members
449,048
Latest member
81jamesacct

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