I've got worksheets that users will be hiding/unhiding columns on for printing purposes. Within those columns are certain cells which need to be copied to a visible area when those columns are hidden and the process reversed when they are not. The function works fine to detect the columns' visible status when the worksheet is activated. However it does not update at the time the column statuses are changed. I understand that the worksheet_calculate sub will detect changes in row visibility, but not columns, nor does it work in worksheet_change. Worksheet_SelectionChange seems buggy (selects more than selected even without code). Suggestions?
Public Function Copy_Hidden_ChargeCode_Descriptions(ByRef sht As String)
On Error GoTo ErrorHandler
Application.EnableEvents = False
If Worksheets(sht).Columns("C:G").Hidden = False Then
If Not IsEmpty(Worksheets(sht).Range("H4")) Then
Worksheets(sht).Range("H1").Cut Worksheets(sht).Range("C1")
Worksheets(sht).Range("H4:N9").Cut Worksheets(sht).Range("C4:H9")
End If
Else
If IsEmpty(Worksheets(sht).Range("H4")) Then
Worksheets(sht).Range("C1").Cut Worksheets(sht).Range("H1")
Worksheets(sht).Range("C4:H9").Cut Worksheets(sht).Range("H4:N9")
End If
End If
Application.EnableEvents = True
Exit Function
ErrorHandler:
Debug.Print Err.Number, Err.Description
End Function
Related
I've created a drop down menu through data validation for workbook navigation. The following is a snippet of code I have for the drop down box to change worksheets in the workbook:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not (Application.Intersect(Range("J4"), Target) Is Nothing) Then _
ThisWorkbook.Sheets("Home").Visible = False
ThisWorkbook.Sheets(Target.Value).Activate
ThisWorkbook.Sheets(Target.Value).Visible = True
ThisWorkbook.Sheets("Imported Data").Visible = False
End Sub
The code is meant to hide all other worksheets that are accessible by the drop down list besides the one selected. I have about 10 tabs and this code has worked perfectly to achieve the basic goal of navigation. However, some pages have formulas and when you update data in the cells meant for calculations the workbook jumps to a random worksheet in the workbook that is not at all referenced in this sub.
Is there some way to have my worksheets not try to do anything with this sub unless the dropdown menu itself is changed?
Bonus (less important) Question: is there a way to make the drop box default to (blank) unless the menu itself is accessed?
Then _
The space followed by an underscore _ means that the current statement isn't finished yet but continues on the next line. Right now the last 3 lines will run whenever there is a change in the worksheet. Put the entire code in If-Endif.
Also avoid unnecessary use of On Error Resume Next. Use proper error handling.
You need to make the sheet visible before you activate it and not vice versa.
Try this
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
If Not (Application.Intersect(Range("J4"), Target) Is Nothing) Then
ThisWorkbook.Sheets("Home").Visible = False
ThisWorkbook.Sheets(Target.Value).Visible = True
ThisWorkbook.Sheets(Target.Value).Activate
ThisWorkbook.Sheets("Imported Data").Visible = False
End If
Letscontinue:
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub
is there a way to make the drop box default to (blank) unless the menu itself is accessed?
If you have created it with Data Valdation then insert a blank value in the list.
I'm currently developing an add-in for Excel which will automatically format a table. The users has to follow the specific format when preparing the table, or else the common error of "Merging cells only keeps the upper-left cell value, and discards the other values." is bound to appear.
I would like to mute this alert from Excel, but would still like to catch this error and pass a different message to the users to terminate this sub. I've tried this:
Sub FormatTable()
On Error Goto ErrHandler
Application.DisplayAlerts = False
'Codes for formatting the table
Exit Sub
ErrHandler:
MsgBox "Incorrect formatting. Terminating process to conserve data."
End Sub
However, I do realise that using "Application.DisplayAlerts = False" will cause Excel to choose the default action and proceed to merge the cells which causes a big mess. It will not go the ErrHandler. Is there some way for making this happen? Thank you.
You could test for merged cells in the selected range prior to running your code:
Public Function HasMergedCells(oRng As Range)
Dim oCell As Range
Dim oArea As Range
For Each oArea In oRng.Areas
For Each oCell In oArea.Cells
If oCell.MergeArea.MergeCells Then
HasMergedCells = True
Exit Function
End If
Next
Next
End Function
Using Excel 2010 I am editing an existing unprotected workbook and have created EntireColumn.Hidden and EntireRow.Hidden in commands in the Worksheet_Change() event to fire when a Data Validation cell is changed, but they don't work.
Private Sub Worksheet_Change(ByVal Target As Range)
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
If Not Intersect(Target, Range("$C$2")) Is Nothing Then
Select Case Target.Value
Case "NO"
MsgBox "You just changed to HIDE" '<= Proves it fires
Range("$C$3").Value = "Invisible" '<= Does change cell
Columns("N:O").EntireColumn.Hidden = True '<= Doesn't hide
Case "YES"
MsgBox "You just changed to UNHIDE" '<= Proves it fires
Range("$C$3").Value = "Visible" '<= Does change cell
Columns("N:O").EntireColumn.Hidden = False '<= Doesn't unhide
End Select
End If
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
The event is firing as I have MsgBoxes to prove it. I can change cell values etc., but not the hiding/unhiding of the column/row.
I've copied my code to a new book and it works. So I copied it back into the original book but as a fresh, blank sheet and it works. It still doesn't work in the original, sizable sheet.
When I copied this into a simple macro it does work as required, hiding the correct columns, but at the push of a button:
Sub HideThem()
Columns("N:O").EntireColumn.Hidden = True '<= DOES work
End Sub
I need this to update automatically based on the value of a single cell. I've even tried to call this mini Sub from within the Worksheet_Change() event but that didn't work either.
Are there any known conflicts with other commands/events, on-sheet buttons, images, merged cells etc. that could be preventing the columns/rows from hiding?
I tried to use a CheckBox instead of a YES/NO Data Validation cell to fire the code (as that could be acceptable) but when I try to insert an ActiveX CheckBox it says Cannot insert object, even in a brand new blank book. Could this be a related problem?
I suppose you have a drop-down list in cell C3 with two items, viz "Visible" and "Invisible".
The following code will hide Columns N and O when you change the value of Range C3 from blank / "Visible" to "Invisible". Prior to this action, you will have to read the message and click OK. Changing from "Invisible" to "Visible" will present you with a message box. Click OK and see the hidden columns reveal themselves.
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("C3") = "Invisible" Then
MsgBox ("You just changed to HIDE")
Columns(14).Hidden = True
Columns(15).Hidden = True
Else
If Range("C3") = "Visible" Then
MsgBox ("You just changed to UNHIDE")
Columns(14).Hidden = False
Columns(15).Hidden = False
End If
End If
End Sub
I have the following senario: whenever the user selects a certain cell, I'm copying a table from a hidden sheet to the active sheet. Then, when the user changes his selection, I need to clear the contents of the copied table and copy another table from the hidden sheet.
To copy the tables from the hidden sheet I'm using:
source.Cells(leftRow, leftCol).CurrentRegion.Copy target.Range("A1")
The problem is that this action seems to cause selectionChanged to get fired again, which triggers my ClearContents command.
Is there a way to use this command without getting selectionChanged fired?
Thanks,
Li
Use Application.EnableEvents = False.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo Whoa
Application.EnableEvents = False
'
'~~> YOUR CODE
'
Letscontinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub
i have an excel with 75 columns and some thousands of rows of data. Out of 75 columns I am using 5 columns for my vba coding purpose. These 5 columns hold flags (either 0 or 1) based on which I am locking the cells in the corresponding row (Flags are coming from Database). As user doesn't want these flag columns I just hid those columns but when ever user tries to copy data from my workbook to another workbook user is able to copy the hidden columns which client doesn't want.
So is there anyway to restrict them not to copy the hidden columns through VBA or with any setting? Actually for this issue what I thought is like on key press of Ctrl + C, I tried to change the Selection.Copy as Selection.Range.SpecialCells(xlCellTypeVisible). But I am getting some error like wrong number of arguments or invalid property assignment.
The lines of code is
Private Sub Workbook_Open()
Application.OnKey "^c", "Copy"
End Sub
Sub Copy()
If Selection Is Nothing Then
Else
Selection.Copy = Selection.Range.SpecialCells(xlCellTypeVisible)
End If
End Sub
Any ideas to restrict users not to copy the hidden columns. Any help would be appreciated greatly.
Try this
Sub Copy()
Dim rng As Range
On Error GoTo Whoa
If Not Selection Is Nothing Then
Set rng = Selection.Cells.SpecialCells(xlCellTypeVisible)
rng.Copy
End If
LetsContinue:
Exit Sub
Whoa:
MsgBox Err.Description, vbCritical, "Error Number : " & Err.Number
Resume LetsContinue
End Sub
Note: I have used Error Handling which is a must because the user might select non contiguous ranges and the code will break if the error handling is not done :) See Screenshot below