Carla Mozer
Carla Mozer

Reputation: 11

Click shape to move forward and back in cells of the same row

I have a table with data in date format (day/month/year) distributed in row 6 and in several columns that go from E6 to NE6.

That is, E6 = 01/01/2025 and NE6 = 12/31/2025.

To advance between these dates, I created two images (arrows), to which I want to assign a macro when I click:

With what VBA code can I advance or go back through the cells in the same row using buttons?

I tried this code:

'MOVE CELL RIGHT ARROW

Sub Move_right_forward()

Dim ActiveLine As Integer, ActiveCollection As Integer
ActiveLine = ActiveCell.Row
ActiveCollection = ActiveCell.Column

If ActiveLine < 4 Then
MsgBox "You must select a cell that contains data!"
ExitSub
End If

If ActiveLine > LastLine Then
MsgBox "You must select a cell that contains data!"
ExitSub
End If

Cells(ActiveLine, ActiveCollection + 1).Select

End Sub

Upvotes: 1

Views: 84

Answers (2)

Black cat
Black cat

Reputation: 6271

Put the below code into a standard module.
Add to the sheet two shapes (a left arrow and a right arrow).
Right click each and then click Assign Macro.
In the popup window select the corresponding macro. Then click OK.

Sub leftarrow()
Dim ws As Worksheet, cell As Range
Set ws = ActiveSheet
lastRow = ws.Cells(ws.Rows.count, "A").End(xlUp).Row
Set cell = ActiveCell
If cell.Row < 4 Or cell.Row > lastRow Then MsgBox ("Selected cell is out of range!"): Exit Sub
mo = Month(ws.Cells(6, cell.Column))
If mo = 1 Then
  mocol = ws.Rows(6).Find(DateSerial(2025, 1, 1)).Column
  ws.Cells(cell.Row, mocol).Select
Else
  mocol = ws.Rows(6).Find(DateSerial(2025, mo - 1, 1)).Column
  ws.Cells(cell.Row, mocol).Select
End If
End Sub

Sub rightarrow()
Dim ws As Worksheet, cell As Range
Set ws = ActiveSheet
lastRow = ws.Cells(ws.Rows.count, "A").End(xlUp).Row
Set cell = ActiveCell
If cell.Row < 4 Or cell.Row > lastRow Then MsgBox ("Selected cell is out of range!"): Exit Sub
mo = Month(ws.Cells(6, cell.Column))
If mo = 12 Then
  mocol = ws.Rows(6).Find(DateSerial(2025, 12, 1)).Column
  ws.Cells(cell.Row, mocol).Select
Else
  mocol = ws.Rows(6).Find(DateSerial(2025, mo + 1, 1)).Column
  ws.Cells(cell.Row, mocol).Select
End If
End Sub

If you are in the first or last month of the year the activecell will move to the actual month's (Jan or Dec) first day.

Upvotes: 0

taller
taller

Reputation: 18923

Assuming row 6 is formatted with the desired date format, the script will determine the first day of the next/previous month.

If the script doesn't resolve the issue, please share a sample of your worksheet data and its layout.


Sub GetNextMth() ' for Right Arrow button
    Const RNG_REF = "E6:NE6"
    Dim c As Range
    For Each c In Range(RNG_REF).Cells
        If IsDate(c) Then
            c.Value = DateSerial(Year(c), Month(c) + 1, 1)
        End If
    Next
End Sub

Sub GetPreMth() ' for Left Arrow button
    Const RNG_REF = "E6:NE6"
    Dim c As Range
    For Each c In Range(RNG_REF).Cells
        If IsDate(c) Then
            c.Value = DateSerial(Year(c), Month(c) - 1, 1)
        End If
    Next
End Sub

enter image description here

Upvotes: 0

Related Questions