0

I'm using 4 sequential columns from sheet 1 to generate charts in sheet 2. I want to hide those 4 columns in sheet 1.

I've tried the below code to hide the columns:

Set allColumns = dataSheet.Columns("J:M")
allColumns.Hidden = True
1
  • dataSheet needs to be defined as the object (not in the code you've displayed). Beyond that, the code works for me. The error was a run-time 424 when i ran your code as is (2 lines) which states an object is required. Commented Dec 26, 2018 at 15:29

2 Answers 2

1

Constants and References

Sub AllCol()

  Const cVntSheet As Variant = "Sheet1"   ' Worksheet Name/Index
  Const cStrRange As String = "J:M"       ' Range Address

  Dim dataSheet As Worksheet              ' Worksheet

  ' Create a reference to the worksheet.
  Set dataSheet = Worksheets(cVntSheet)

  ' Hide the range.
  dataSheet.Columns(cStrRange).Hidden = True

End Sub
Sign up to request clarification or add additional context in comments.

Comments

1

Swap Sheet1 with your actual sheet name.

Sub HideMe()

    ThisWorkbook.Sheets("Sheet1").Columns("J:M").Hidden = True

End Sub

Notice this can be done in one line. If you want to use variables (workbook or worksheets), they should build the above string when combined

Something like:

Dim wb as Workbook: Set wb = ThisWorkbook
Dim ws as Worksheet: Set ws = wb.Sheets("Sheet1")
Dim hm as String: hm = "J:M"

ws.Columns(hm).Hidden = True

If you substitute your variables into the last line, you will end up with the exact same line of code shown in the first sub.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.