Show/hide picture depending of the date

nwpando

New Member
Joined
Oct 29, 2023
Messages
1
Office Version
  1. 2010
Platform
  1. Windows
Hi everyone,

I'm a complet noob with Excel and VBA, i have no idea how it work and i need your help.

I would like to do some kind of advent calendar. I would like to have cells with the different date of the month and when you click a cell a picture is shown on an other cell (depending on which date you choose a different picture is shown)

Like date in the column A,B,C and line 1 to 11 and when you click on the specific date an image appears in cell H10 for example.

Or another idea was to have a cell with the formula "=today()" and depending of the date show an picture would be shown in another cell. And every you open the file, the date change and another picture appears.

I don't know if this is possible or even if this something i can do without any knowledge.

Thanks in advance
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
The following demo is based on your first idea – listing all dates from the 1st to the 31st in a range from A1 to C11 (see below). I have added 3 pictures to the sheet representing the 1st, 2nd and 3rd (obviously I’m not going to create all 31 – you can do that). I have called those pictures “PR1C1” (1st), “PR1C2” (2nd) and “PR1C3” (3rd), where P = picture, R = the row that the ‘date’ is located and C = the column that the ‘date’ is located. Using this method, you would call the picture representing the 15th “PR5C3” because that’s the cell location (row and column) where the 15th is located. I hope you get the drift.
What the code does is, when you select a different cell, the code picks up what row/column you’ve selected, turns that into a string, and shows only that picture in cell H10. I’ve added a demo file to show it in action. You need to put the code in the sheet code area for the sheet where you want this to happen. Right click on the sheet Tab name, select View Code & put the code in the blank window that appears on the right of the screen.

Sheet:
Advent.xlsm
ABC
11st2nd3rd
24th5th6th
37th8th9th
410th11th12th
513th14th15th
616th17th18th
719th20th21st
822nd23rd24th
925th26th27th
1028th29th30th
1131st
Sheet1


Code:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Application.EnableEvents = False
    Dim s As Shape, d As String, c As Range
    Set c = Range("H10")                        '<-- set location for display
    d = "PR" & Target.Row & "C" & Target.Column '<-- this creates a picture 'name'
    For Each s In ActiveSheet.Shapes            '<-- loop through all pictures on the sheet
        If s.Name <> d Then
            s.Visible = False
        Else
            With s
                .Visible = True
                .Top = c.Top
                .Left = c.Left
                .Width = 100        '<-- set the size of the picture here
                .Height = 100
            End With
        End If
    Next s
    Application.EnableEvents = True
End Sub

Link to demo file: Advent.xlsm
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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