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.
Related
I have a table where I want to be able to hide individual rows at a mouse click. The (seemingly) easiest solution I've found is to have a column filled with hyperlinks that call a macro to hide the row that they're in.
There are two ways of calling macros from hyperlinks: using Worksheet_FollowHyperlink with manual hyperlinks, and using =HYPERLINK.
The former works fine, except there's no way (that I've found) to have them generate automatically when new rows are added to the table. I would have to either manually copy them down every time, which is unviable, or add them with VBA, which adds a bunch of complexity to an otherwise simple task.
The latter generates fine, being a formula, but it doesn't actually work. It doesn't trigger Worksheet_FollowHyperlink, and when using =HYPERLINK("#MyFunction()") it just doesn't hide rows (or do much other than editing cells contents).
Function MyFunction()
Set MyFunction = Selection
Selection.EntireRow.Hidden = True
End Function
Is there a good solution to this?
Rather than a Hyperlink, you could handle a Double Click event on the table column
Something like
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim NameOfTableColumn As String
On Error GoTo EH:
NameOfTableColumn = "DblClickToHide" ' update to suit your table
If Not Application.Intersect(Target, Target.ListObject.ListColumns(NameOfTableColumn).DataBodyRange) Is Nothing Then
Target.EntireRow.Hidden = True
Cancel = True
End If
Exit Sub
EH:
End Sub
Please, copy the next code in the sheet code module where the table to be clicked exists. Clicking on each cell in its first column (dynamic to rows adding/insertions/deletions), the clicked row will be hidden:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim tbl As ListObject
If Target.cells.CountLarge > 1 Then Exit Sub
Set tbl = Me.ListObjects(1) 'you may use here the table name
If Not Intersect(Target, tbl.DataBodyRange.Columns(1)) Is Nothing Then
Application.EnableEvents = False
Target.EntireRow.Hidden = True
Application.EnableEvents = True
End If
End Sub
It would be good to think about a way to unhide the hidden row if/when necessary. If only a row should be hidden at a time, it is easy to unhide all the rest of column cells...
I am trying to automate data with a master wookbook. When I open the workbook I want a msg box to appear and clear the contents of specific columns in tables in one of the worksheets. I keep getting the "Application-defined or object-defined error". This is the code in my "This workbook" section:
Option Explicit
Sub Workbook_Open()
Dim answer As Integer
answer = MsgBox("Do you want to clear the totals?", vbYesNo + vbQuestion, "Clear Totals")
If answer = vbYes Then
Call Sheet1.ClearContents_1
End If
End Sub
This is my Sheet1 code:
Sub ClearContents_1()
Call Loop_Clear_C
Call Loop_Clear_M
Call Clear_S
End Sub
Sub Loop_Clear_C()
For i = 1 To Range("UserTable79").Rows.Count
Range("UserTable79[Total]")(i) = 0
Next i
End Sub
Sub Loop_Clear_M()
For i = 1 To Range("ServiceTable79").Rows.Count
Range("ServiceTable79[Total]")(i) = 0
Next i
End Sub
Sub Clear_S()
Range("TotalTable79[Actual Total]").ClearContents
End Sub
They worked separately but not together. The msg box comes up but doesn't run the Sheet1 code. How do I call upon this sheet code?
Edit: The Sheet1 code no longer works either.
Often the "Application-defined or object-defined error" message comes up when the range that you're referring to doesn't exist. If you click on Sheet1 to activate it and then try to use the Immediate panel to perform an operation on the ranges you're referring to (e.g. try entering Range("UserTable79").Select) do you get an error?
Explicitly specifying the worksheet when calling the function from the "ThisWorkbook" section (e.g. if the worksheet is named "SheetName" then specifying Call ThisWorkbook.Sheets("SheetName").ClearContents_1 rather than Call Sheet1.ClearContents_1) can help.
Also - note that you can clear the entire table ranges or entire columns in Loop_Clear_C and Loop_Clear_M with ClearContents or EntireColumn.ClearContents if that's easier.
I have created a macro that allows a user to select an option from a dropdown list which unhides selected rows that corresponds with their selection. The problem is when I protect the sheet I get the following error "Run-time error '1004'" when I select an option for the drop down list. I need this sheet to be protected so the user cannot touch the data sets shown. Here is a sample code (original version is very long):
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Activate
If Not Application.Intersect(Range("C15"), Range(Target.Address)) Is Nothing Then
Select Case Target.Value
Case Is ="Option 1"
Rows("17:75").EntireRow.Hidden = True
Case Is ="Option 2"
Rows("17:28").EntireRow.Hidden = False
End Select
End If
End Sub
I've been reading other threads and I've come across a few options that said I need to Unprotect and Protect my sheet but I'm not to sure how to add this to the code above. And if this is the best option for what I am trying to accomplish
Option 1
Sub UnprotectAll()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
sh.Unprotect Password:=yourPassword
Next sh
End Sub
Sub ProtectAll()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
sh.Protect Password:=yourPassword
Next sh
End Sub
Option 2 - Adding this code somewhere below
UserInterFaceOnly:=True
Any suggestions in how I can accomplish this? And what the full code would look like?
Thanks so much!
#ExcelNoob I’ve made the following assumptions based on your question:
Only the active sheet is relevant
You formatted cell C15 as not Locked (when the sheet is unprotected, right click C15 /format/protection and uncheck ‘locked’ & ‘hidden’
You have indicated the correct rows you want hidden or not (seem a bit odd?)
There are only 2 options
That being the case, the minimum code below will do what you ask. If you want to use a specific password, just put it between the double quotation marks.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("C15"), Target) Is Nothing Then
ActiveSheet.Unprotect Password:=""
If Range("C15").Value = "Option 1" Then
Rows("17:75").Hidden = True
ElseIf Range("C15").Value = "Option 2" Then
Rows("17:28").Hidden = False
End If
ActiveSheet.Protect Password:=""
End If
End Sub
As per above, but if you don't want to protect/unprotect, put your drop down in a form and use :
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Sheet1.Protect "Password", UserInterfaceOnly:=True
End Sub
Where "password" is changed to whatever password you want.
Here is the code I have. I am reading from one sheet and searching for that term on another sheet. If the search finds the match, it will copy that whole row and paste it onto the first sheet. The search, copy, and paste work. I do not recieve an error but it is as if the macro does not stop because Excel won't let me click out of the original cell the macro read. Am I missing a line of code that should end the macro? Ideally, it should just end the macro after the paste.
I went off the code from another thread:
How to Paste copied Row from one Sheet to Another
Sub Ref()
Dim rng1 As Range
Dim rng2 As Range
If Range("A11") = "SELECT" Then
Else
Set rng1 = Worksheets("MAIN").Range("A11")
For Each rng2 In Worksheets("REF").Range("A11:A2000")
If rng1 = rng2(1).Value Then
rng2.EntireRow.Copy
rng1.EntireRow.PasteSpecial (xlPasteFormulas)
Exit For
End If
Next rng2
End If
End Sub
This is where I call the macro
Private Sub Worksheet_Selection_Change(ByVal Target As Range)
Call Ref
End Sub
Thank you in advance
You need to stop the paste from re-triggering the change event:
...
Application.EnableEvents=False
rng1.EntireRow.PasteSpecial (xlPasteFormulas)
Application.EnableEvents=True
...
EDIT: You should probably be using the worksheet_change event, not selection_change. You can handle the event enabling there instead:
Private Sub Worksheet_Change(ByVal Target As Range)
If not application.intersect(me.range("A11"),Target) is nothing then
Application.EnableEvents=False
Call Ref
Application.EnableEvents=True
End if
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