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
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 am currently linking lots of subs together to format headings in excel for upload to other dashboard software. The headings need to be in a certain order. I am running several subs triggered by a command button to search for the heading text in a sepcified range of cells. having trouble with if the heading isn't found which sometimes happens as the data is downloaded from brandwatch and if nothing returns for that particular subject in Brandwatch then the heading isn't in the downloaded data file. I want the macro to stop when the required text isn't found in the range.
I am only just learning VB and have no idea how to stop the next sub from running if the specific text isnt found. As it stands at the minute it is putting in another heading as the next subroutine is a copy active column and then goto another specific column and paste.
The message 'The Value you are searching for is not available' comes up but then the next sub pastes the wrong heading in when I just want all the macros to stop at that point.
Any help would be greatly appreciated.
Thanks Sylly
the offending Subroutine is below
Sub FIND_POTENTIAL_IRRELEVANT_MENTIONS()
'============================
'FIND POTENTIAL_IRRELEVANT_MENTIONS
'============================
Dim MyValue As Variant
On Error Resume Next
Range("CT9:HD9").Find(What:="Market Insight Report").Select
On Error GoTo 0
MyValue = ActiveCell.Value
If MyValue = "" Then
MsgBox "The Value you are searching for is not available"
Else
MsgBox MyValue & " found in " & ActiveCell.Address
End If
End Sub
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
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
I learned that I need to write a macro if I want users to delete rows on a protected sheet.
This is the code I got by googling around:
Sub delete_row()
ActiveSheet.Unprotect Password:="justme"
ActiveCell.EntireRow.Delete
ActiveSheet.Protect Password:="justme"
End Sub
Where exactly should I place this code? Will it work if multiple rows are deleted etc.?
MrExcel is down today, so limited options.
Paste this in a module
Option Explicit
Sub DeleteMe()
Dim Ret As Range, Cl As Range
On Error Resume Next
Set Ret = Application.InputBox("Please select the Cells", "Delete Rows", Type:=8)
On Error GoTo 0
ActiveSheet.Unprotect Password:="justme"
If Not Ret Is Nothing Then Ret.EntireRow.Delete
ActiveSheet.Protect Password:="justme"
End Sub
When you run the above macro it will ask you to select the cell(s). Whatever cells you select, the entire row will get deleted.