I have a range of data AO4:AO30. It is a list of dates, some cells are blank but most are populated. I want to make a macro button called “show data”. I want to button to clear the data when clicked and then repopulate the range with the same data once clicked again. The range is conditionally formatted to colour a Gantt chart style calendar so it matters if the data is actually there or not. I have no experience with VBA and have been having a hard time trying to google my way through it. Any advice helps, thanks!
I’ve tried getting chatgpt to make a code for me and that didn’t work. I’ve tried several websites but I don’t fully know what I’m looking for so that has been difficult.
Without more details or some code you've tried it's hard to say that this is the best answer, but here's an idea:
Here's the data I'm using to test
Using a global variable to store and retrieve dates
Option Explicit
' This is my global variable to store the dates
Dim storageArray() As Variant
Private Sub ShowData()
' The range the dates are stored in (A1 to A4 on sheet1 in this example)
Dim dateRange As Range
Set dateRange = ThisWorkbook.Sheets("Sheet1").Range("A1:A4")
' Check if the range is empty
If WorksheetFunction.CountA(dateRange) = 0 Then
' The range is empty
' Add the dates back to the range
dateRange.Value = storageArray
Else
' The range is not empty
' Store the dates in the global variable and clear the range
storageArray = dateRange.Value
dateRange.Clear
End If
End Sub
Using another location to store and retrieve dates
Option Explicit
Private Sub StoreData()
' The range the dates are stored in (A1 to A4 on sheet1 in this example)
Dim dateRange As Range
Set dateRange = ThisWorkbook.Sheets("Sheet1").Range("A1:A4")
' The other range to temporarily store the dates
' Does not have to be on the same sheet
Dim storageRange As Range
Set storageRange = ThisWorkbook.Sheets("Sheet1").Range("C1:C4")
' Check if the range is empty
If WorksheetFunction.CountA(dateRange) = 0 Then
' The range is empty
' Add the dates back to the range
dateRange.Value = storageRange.Value
storageRange.Clear
Else
' The range is not empty
' Store the dates in the other location and clear the range
storageRange.Value = dateRange.Value
dateRange.Clear
End If
End Sub