Creating A Clickable Image Map
Category: Charts & Graphics / General VBA | [Item URL]
A companion file is available: Click here to download
You might have noticed that some Web sites feature an image map to help navigate the site. An image map is a single image, but it's sensitive to the location at which you click it. This tip demonstrates how to create an image map.
To create an image map, you must be familiar with the MouseOver event. This event is triggered whenever the mouse pointer moves over a particular control. In this case, the control is an Image control. If you're not familiar with the MouseOver event, consult the online help for details. Basically, this event lets you determine the horizontal and vertical coordinates of the mouse pointer, relative to the control. The image I use in the example is shown below.
Setting up the image map simply involves monitoring the mouse position, and taking the appropriate action if the control is clicked. The VBA code is shown below. Every mouse movement over the Image control triggers the MouseMove event and executes the Image1_MouseMove subroutine. The mouse coordinates are passed to the subroutine via the X and Y arguments. A series of Select Case statements determines which flag is under the mouse pointer, and stores the country name in the Country variable. I also display the country in the status bar. Obviously, it will take some work to figure out the coordinates, and you'll probably want to use an image in which the "subimages" are rectangular.
When the image is clicked, the Image1_Click subroutine is executed. This subroutine simply activates a worksheet, the name of which is stored in the Country variable. The worksheets are named for the countries represented by the flags.
VBA Code
In this example, the Image control is placed on a worksheet named Menu. Therefore, this code resides in the code module for the Menu sheet.
Public Country As String
Private Sub Image1_MouseMove(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Select Case Y 'Vertical coordinate
Case Is < 2 'Off the map
Country = ""
Case Is < 47
Select Case X 'Horizontal coordinate
Case Is < 2: Country = ""
Case Is < 78: Country = "United States"
Case Is < 152: Country = "Canada"
Case Else: Country = "" 'Off the map
End Select
Case Is < 87
Select Case X 'Horizontal coordinate
Case Is < 2: Country = ""
Case Is < 40: Country = ""
Case Is < 112: Country = "Cuba"
Case Else: Country = "" 'Off the map
End Select
Case Is < 133
Select Case X 'Horizontal coordinate
Case Is < 2: Country = ""
Case Is < 78: Country = "Mexico"
Case Is < 152: Country = "Puerto Rico"
Case Else: Country = "" 'Off the map
End Select
Case Else 'Off the map
Country = ""
End Select
' The statement below was used while figuring out the coordinates
' Application.StatusBar = Country & " " & X & " " & Y
Application.StatusBar = Country
End Sub
Private Sub Image1_Click()
If Country <> "" Then Sheets(Country).Activate
' Reset the status bar
Application.StatusBar = False
End Sub
NOTE: Another way to create an image map is to insert
transparent objects over the image, and create event-handler subroutines for the
objects.
Excel Tips
Excel has a long history, and it continues to evolve and change. Consequently, the tips provided here do not necessarily apply to all versions of Excel.
In particular, the user interface for Excel 2007 (and later), is vastly different from its predecessors. Therefore, the menu commands listed in older tips, will not correspond to the Excel 2007 (and later) user interface.
All Tips
Browse Tips by Category
Search for Tips
Tip Books
Needs tips? Here are two books, with nothing but tips:
Contains more than 200 useful tips and tricks for Excel | Other Excel 2003 books | Amazon link: John Walkenbach's Favorite Excel Tips & Tricks
Contains more than 200 useful tips and tricks for Excel 2007 | Other Excel 2007 books | Amazon link: John Walkenbach's Favorite Excel 2007 Tips & Tricks
