问题
I would like to hide certain columns and rows based on inputs into certain cells, to make the code easily editable.
Furthermore, if the reference cell/range is empty, then I would like to continue the code without hiding any rows or columns.
Columns to Hide: based on cells C8:D8
Rows to Hide: based on cells C9:D9
This is where the logical fest should take place
reportColumnsAddr = settingsSheet.Range("C8").Value & ":" & settingsSheet.Range("D8").Value
reportRowsAddr = settingsSheet.Range("C9").Value & ":" & settingsSheet.Range("D9").Value
Current Code
Option Explicit
Private Sub CommandButton1_Click()
Dim MyFolder As String, MyFile As String
Dim StartTime As Double
Dim MinutesElapsed As String
Dim Filename As String
Dim Cell As String
Dim Counter As Long
If ThisWorkbook.Sheets("Sheet1").Range("C7").Value = vbNullString Then
MsgBox "Enter Tab Name"
Exit Sub
End If
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Title = "Select a Folder"
If .Show = True Then
MyFolder = .SelectedItems(1)
End If
If .SelectedItems.Count = 0 Then Exit Sub
Err.Clear
End With
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
MyFile = Dir(MyFolder & "\", vbReadOnly)
StartTime = Timer
Do While MyFile <> ""
DoEvents
On Error GoTo 0
Workbooks.Open Filename:=MyFolder & "\" & MyFile, UpdateLinks:=False
Dim settingsSheet As Worksheet 'Source
Dim reportSheet As Worksheet 'To convert to PDF
Dim targetColumnsRange As Range 'feeds from source
Dim targetRowsRange As Range
Dim reportSheetName As String 'source sheet with the target's sheet name
Dim reportColumnsAddr As String
Dim reportRowsAddr As String
' Set a reference to the settings sheet
Set settingsSheet = ThisWorkbook.Worksheets("Sheet1") ' source
' Gather the report sheet's name
reportSheetName = settingsSheet.Range("C7").Value ' good
reportColumnsAddr = settingsSheet.Range("C8").Value & ":" & settingsSheet.Range("D8").Value
reportRowsAddr = settingsSheet.Range("C9").Value & ":" & settingsSheet.Range("D9").Value
Set reportSheet = Sheets(reportSheetName)
Set targetColumnsRange = reportSheet.Range(reportColumnsAddr)
Set targetRowsRange = reportSheet.Range(reportRowsAddr)
targetColumnsRange.EntireColumn.Hidden = True
targetRowsRange.EntireRow.Hidden = True
With reportSheet.PageSetup
.Zoom = False
.FitToPagesWide = 1 '.FitToPagesTall = 1
End With
Filename = ActiveWorkbook.Name
Cell = Replace(Filename, ".xlsx", ".PDF")
reportSheet.Select
reportSheet.PageSetup.Orientation = xlLandscape
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & Cell, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=True, OpenAfterPublish:=False
Counter = Counter + 1
0
Workbooks(MyFile).Close SaveChanges:=False
MyFile = Dir
Loop
'turns settings back on
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
Application.Calculation = xlCalculationManual
MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")
MsgBox "Successfully Converted " & Counter & " Files in " & MinutesElapsed & " minutes", vbInformation
End Sub
回答1:
A couple of thoughts about your code:
Dim MySheet As String
As
mySheet
is referring to the sheet name, be clear about it.Rename the variable as
Dim mySheetName as String
Set ReportSheet = Sheets(MySheet)
When setting the reference to the report sheet use full quilifying to the object.
Add
Set reportSheet = ThisWorkbook.Worksheets(mySheetName)
I tried using the following logic, but it did not work, since I'm not able to insert " as part of the naming to properly create my variable.
To insert a double quote character, you can use:
char(34)
e.g.
TEST1 = "(" & Chr(34) & ThisWorkbook.Sheets("Sheet1").Range("C8").Value & ThisWorkbook.Sheets("Sheet1").Range("D8").Value & Chr(34) & ")"
Now about the request:
You could do this in a shorter way, but I chose this longer path, to illustrate the idea.
- Gather the settings' information
- Set a reference to both columns and rows
- Find the intersection between them
- Look for non empty cells in that range and hide their columns and rows
Code:
Public Sub DynamicallyHideCells()
Dim settingsSheet As Worksheet
Dim reportSheet As Worksheet
Dim targetColumnsRange As Range
Dim targetRowsRange As Range
Dim targetRange As Range
Dim targetCell As Range
Dim reportSheetName As String
Dim reportColumnsAddr As String
Dim reportRowsAddr As String
' Set a reference to the settings sheet
Set settingsSheet = ThisWorkbook.Worksheets("Sheet1")
' Gather the report sheet's name
reportSheetName = settingsSheet.Range("C7").Value
' Check the : between the two cells reference
reportColumnsAddr = settingsSheet.Range("C8").Value & ":" & settingsSheet.Range("D8").Value
' Check the : between the two cells reference
reportRowsAddr = settingsSheet.Range("C9").Value & ":" & settingsSheet.Range("D9").Value
' Set a reference to the report's sheet
Set reportSheet = ThisWorkbook.Worksheets(reportSheetName)
' Set a reference to the report's columns
Set targetColumnsRange = reportSheet.Range(reportColumnsAddr)
' Set a reference to the report's rows
Set targetRowsRange = reportSheet.Range(reportRowsAddr)
' Find the range of cells to be evaluated
Set targetRange = Intersect(targetColumnsRange, targetRowsRange)
' Loop through each cell and hide if not empty
For Each targetCell In targetRange.Cells
If targetCell.Value <> vbNullString Then
targetCell.EntireColumn.Hidden = True
targetCell.EntireRow.Hidden = True
End If
Next targetCell
End Sub
EDIT:
If you only need to hide the columns. Use the following code:
EDIT 2:
Added line to check if input cells are empty (' Check if either cell are empty and exit sub).
Public Sub HideColumns()
Dim settingsSheet As Worksheet
Dim reportSheet As Worksheet
Dim targetColumnsRange As Range
Dim reportSheetName As String
Dim reportColumnsAddr As String
' Set a reference to the settings sheet
Set settingsSheet = ThisWorkbook.Worksheets("Sheet1")
' Gather the report sheet's name
reportSheetName = settingsSheet.Range("C7").Value
' Check the : between the two cells reference
reportColumnsAddr = settingsSheet.Range("C8").Value & ":" & settingsSheet.Range("D8").Value
' Check if either cell are empty and exit sub
If settingsSheet.Range("C8").Value = vbNullString Or settingsSheet.Range("D8").Value = vbNullString Then
' Set a reference to the report's sheet
Set reportSheet = ThisWorkbook.Worksheets(reportSheetName)
' Set a reference to the report's columns
Set targetColumnsRange = reportSheet.Range(reportColumnsAddr)
' Hide the columns in range
targetColumnsRange.EntireColumn.Hidden = True
Else
' Do something here
End If
End Sub
Hope this is what you're looking for.
Let me know if it works.
回答2:
Replace "" with """" :
TEST1 = "(" & """" & ThisWorkbook.Sheets("Sheet1").Range("C8").Value & ThisWorkbook.Sheets("Sheet1")
.Range("D8").Value & """" & ")"
来源:https://stackoverflow.com/questions/60312781/vba-hiding-columns-and-rows-using-cell-reference