Apr 23 2024 10:26 AM - edited Apr 23 2024 11:18 AM
Hello Everyone,
I am a basic Excel user attempting my first VBA macro and being reminded that simple <> easy (at least not for me).
Desired VBA outcome - When a user clicks on the "Data" worksheet tab, I want the macro to unhide any hidden columns, display columns A - CZ, and only display rows 1-18. I used ChatGPT to start. Countless iterations later, this ChatGPT code displays the rows correctly but does not unhide the hidden columns and show all columns.
=========================ChatGPT=========================
Private Sub Worksheet_Activate()
Dim ws As Worksheet
Dim col As Range
' Set the worksheet object to the "Data" worksheet
Set ws = ThisWorkbook.Sheets("Data")
' Hide all rows and columns
ws.Rows.Hidden = True
ws.Columns.Hidden = True
' Set the range for rows 1 to 18 and columns A to CZ
Set rowRange = ws.Rows("1:18")
Set colRange = ws.Columns("A:CZ")
' Unhide rows and columns
rowRange.Hidden = False
colRange.Hidden = False
End Sub
=========================================================
Any suggestions to fix this are greatly appreciated!
Thom
Apr 23 2024 01:44 PM
SolutionTry this version. And never rely on ChatGPT, please.
Private Sub Worksheet_Activate()
Dim rowRange As Range
Dim colRange As Range
' Hide all rows and columns
Rows.Hidden = True
Columns.Hidden = True
' Set the range for rows 1 to 18 and columns A to CZ
Set rowRange = Rows("1:18")
Set colRange = Columns("A:CZ")
' Unhide rows and columns
rowRange.EntireRow.Hidden = False
colRange.EntireColumn.Hidden = False
Application.Goto Range("A1")
End Sub
Apr 23 2024 04:06 PM
Apr 23 2024 01:44 PM
SolutionTry this version. And never rely on ChatGPT, please.
Private Sub Worksheet_Activate()
Dim rowRange As Range
Dim colRange As Range
' Hide all rows and columns
Rows.Hidden = True
Columns.Hidden = True
' Set the range for rows 1 to 18 and columns A to CZ
Set rowRange = Rows("1:18")
Set colRange = Columns("A:CZ")
' Unhide rows and columns
rowRange.EntireRow.Hidden = False
colRange.EntireColumn.Hidden = False
Application.Goto Range("A1")
End Sub