VBA Macro to move a shape based on the current date

emmsky99

New Member
Joined
Nov 21, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I have a spreadsheet with a shape in the form of vertical dotted line to show the current date, which is currently being positioned manually. I'm trying to run a macro to automatically reposition the line to the current date.

The date format in my sheet is 21-Nov and I have got the macro to work if I physically enter the cell text as the date in the same format ("21-Nov"), but I want to set the macro to always move the line to the current date, without having to change the macro script. I've tried replacing the date for Date(), Current_Date etc. etc. but cannot get it to work.
Can anyone help? This is the macro I'm currently using:

Sub Check()
Dim shp As Shape
Dim r As Range, cel As Range


Set shp = ActiveSheet.Shapes("Straight Connector 2")
Set r = Range("d2:ek2")


For Each cel In r
If cel.Text = "21-Nov" Then
shp.Left = cel.Left - shp.Width
shp.Top = cel.Top - (shp.Height / 2) + (cel.Height / 2)
Exit For
End If
Next cel


End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
I have a spreadsheet with a shape in the form of vertical dotted line to show the current date, which is currently being positioned manually. I'm trying to run a macro to automatically reposition the line to the current date.

The date format in my sheet is 21-Nov and I have got the macro to work if I physically enter the cell text as the date in the same format ("21-Nov"), but I want to set the macro to always move the line to the current date, without having to change the macro script. I've tried replacing the date for Date(), Current_Date etc. etc. but cannot get it to work.
Can anyone help? This is the macro I'm currently using:

Sub Check()
Dim shp As Shape
Dim r As Range, cel As Range


Set shp = ActiveSheet.Shapes("Straight Connector 2")
Set r = Range("d2:ek2")


For Each cel In r
If cel.Text = "21-Nov" Then
shp.Left = cel.Left - shp.Width
shp.Top = cel.Top - (shp.Height / 2) + (cel.Height / 2)
Exit For
End If
Next cel


End Sub
Managed to solve myself. I needed to replace cel.Text with cel.Value
 
Upvote 0

Forum statistics

Threads
1,214,901
Messages
6,122,157
Members
449,068
Latest member
shiz11713

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