Disable delete/insert option of rows/columns/cells in a workbook - vba

I'm currently trying to figure out how to disable the delete/insert options of cells/rows/columns in a workbook. Currently, I'm working with the following VBA code to disable the delete option for rows and columns. However, this still allows me to delete cells (not clear content, but the "delete" where other cells need to shift). Also, I want to also disable the insert option for rows/columns/cells. Here's the code I'm currently using:
Private Sub Workbook_open()
Dim dis As CommandBarControl
For Each dis In Application.CommandBars.FindControls(ID:=293)
dis.Enabled = False
Next
For Each dis In Application.CommandBars.FindControls(ID:=294)
dis.Enabled = False
Next
End Sub
I was trying to search for the "ID" of the delete cell and insert options so I could just replicate the above code, but I can't seem to find it. Any help will be greatly appreciated, thanks! :)

You can use the sheet protection with VBA code like this :
Private Sub Workbook_Open()
Dim sh As Variant
For Each sh In Sheets
sh.Protect AllowInsertingColumns:=False, AllowInsertingRows:=False, UserInterfaceOnly:=True
Next sh
End Sub

The classical answer for disable delete/insert option of rows/columns/cells in a workbook is simply to protect the worksheet with a password and to define what can be done in the parameters.
This is an example, not allowing inserting columns and rows:
Worksheets(1).Protect AllowInsertingColumns:=True, AllowInsertingRows:=True

Related

how to dynamically update a workbook name in excel using vba?

I’m trying to dynamically update a workbook name in a formula in excel to bring through data from a continually changing source file.
So far I have been getting by with using an indirect formula, but now I have a huge workbook with around 216,000 cells to populate and I don’t think indirect is the most efficient way to do this.
I want to use VBA instead but I have no experience with this. From doing some googling I have found a few things but I’m not sure how to implement my specific needs into the code.
so far 've come up with this:
Sub replace()
Dim cell As Range
cell.Formula = replace(cell.Formula, "OfficeSupplies.csv",
"OfficeSupplies2.csv")
Range("a1:d8").Value
Next
End Sub
However, when I try to execute it, it doesn't work at all.
Edited to insert the handling of a specified range instead of ActiveSheet used range and to handle a sheet different to "Active" one
To answer the question, you could use a code like the following to replace in "Active" sheet used range:
Sub replace()
ActiveSheet.UsedRange.SpecialCells(xlCellTypeFormulas).Replace(What:="OfficeSupplies.csv", Replacement:="OfficeSupplies2.csv", LookAt:=xlPart)
End Sub
or you could explicitly refer to a sheet:
Sub replaceInSpecifiedSheet()
Worksheets("MySheetName").UsedRange.SpecialCells(xlCellTypeFormulas).Replace(What:="OfficeSupplies.csv", Replacement:="OfficeSupplies2.csv", LookAt:=xlPart) ' change "MySheetName" to your actual sheet name
End Sub
or you could want to change formulas in a given range:
Sub replaceInSpecifiedRangeOfSpecifiedSheet()
Worksheets("MySheetName").Range("A5:B8").SpecialCells(xlCellTypeFormulas).Replace(What:="OfficeSupplies.csv", Replacement:="OfficeSupplies2.csv", LookAt:=xlPart) ' change "MySheetName" to your actual sheet name
End Sub
But changing a formula in 216k cells can be quite a time consuming activity
You may consider the opposite: change the name of the”continually changing source file”
You can do that without VBA of course
Should you be “forced” to or prefer use VBA then you could use ‘Name ... As‘ statement
Sub replace2()
Dim FullNameToChange As String
Dim HardCodedFullName As String
FullNameToChange = "C:\ChangingName.xls"
HardCodedFullName = "C:\HardCodedName.xls"
If Dir(FullNameToChange) <> "" And Dir(HardCodedFullName) = "" Then Name FullNameToChange As HardCodedFullName
End Sub
Give this a try
Cells.Replace "OfficeSupplies.csv", "OfficeSupplies2.csv", xlPart, , True

VBA using drop down lists on other worksheets to hide rows automatically

Afternoon all!
I am using the results of various drop down lists to hide the relevant rows on a spreadsheet as the value on the drop down list is changed (so automatically).
With some googling, I have come across the below set up which works nicely, although I have now hit a snag when trying to specify drop down list cell from another worksheet.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim disabled As Boolean
If Range("D2") = "Yes" Then
disabled = True
End If
If disabled Then
Rows("3:8").EntireRow.Hidden = False
Else
Rows("3:8").EntireRow.Hidden = True
End If
End sub
Using the following has not worked, and googling for a solution has lead me up many a dead end:
If Sheets("Topsheet").Range("D27") = "Yes" Then
Am I unable to use values from a neighbouring sheet when declaring a variable due to it being a private sub?
Any help would be much appreciated as I have been stumped on this for a couple of hours!
Your code can be massively simplified. Try using this (You'll have to update the sheet name if it's not the same as yours)
Private Sub Worksheet_Change(ByVal Target As Range)
Sheets("SheetWithRowsToBeHidden").Rows("3:8").EntireRow.Hidden = IIf(Me.Range("D27").Value2 = "Yes", True, False)
End Sub
Also, where have you put this code? Have you put it inside a Module or is it in the Sheet Object? It needs to be in the Sheet Object that has the dropdowns on.

Maro to always copy and paste as formulas by default in Excel

I need help to check this macro that intends to copy and paste without formatting. It doesn't work fine.
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
On Error Resume Next
Target.PasteSpecial xlPasteFormulas
Application.CutCopyMode = True
End Sub
How do I make Ctrl-V to paste without any format? I want to keep the excel clean and tidy, so users can copy and paste values and formulas without formatting.
Thanks!
If you want to make your own, custom Ctrl-V, well, you can achieve it this way:
' Code Module ThisWorkbook
Private Sub Workbook_Open()
Application.OnKey "^v", "ThisWorkbook.PasteWithoutFormat"
End Sub
Private Sub PasteWithoutFormat()
If Application.CutCopyMode Then Application.Selection.PasteSpecial xlPasteFormulas
End Sub
Try this: Assigning Macros to Short-Cut Keys in Excel
You'll need to work out how users have access to the macro - there are a few ways to do this and I'm not so sure what will work in your environment, nor your access level to make these options work.
I suppose I'd build a very simple ADD-IN in which I'd embed the macro and there also, assign the macro shortcut to override CTRL + V. Then deploy the ADD-IN according to your company policies.
However, we need more details from you to fully implement the solution.
I went for the easiest way. I set up Ctr-v (in macro's options) to run the following code:
Sub Pstfrmul()
Selection.PasteSpecial xlPasteFormulas
End Sub
Many many thanks for all your answers and comments. Best,
Gerónimo

How do I lock cells containing formulas but still allow macros to work?

I have a worksheet that I make my employees fill out, and I have calculated cells that I want to lock so they cannot change them. I have selected the cells and selected properties and ensured that the "lock" checkbox is checked. When I protected the worksheet/workbook the "export to csv" macro button stopped working. In order to enable the macro to be completed I inserted this VB code into the Workbook:
Private Sub Workbook_Open()
Dim wSheet As Worksheet
For Each wSheet In Worksheets
wSheet.Protect Password:="password", _
UserInterFaceOnly:=True
Next wSheet
End Sub
This worked but had the unintended side effect of allowing my locked formulas to be able to be edited even though they were locked. Only cells containing non-formula values remained locked. What is the proper way to allow macros but still lock formula cells?
My solution was to lock the entire workbook and worksheets, then code into the VB button the disabling of the lock then the re-enabling the macro. Like so:
Sub MyMacro()
Sheet1.Unprotect Password:="password"
'insert code here
Sheet1.Protect Password:="password"
End Sub
I then deleted my Workbook_Open code.

Making excel graphs appear/disappear

I want a graph only to appear when a condition is fulfilled. To be more precise: I have a drop down menu that changes the content of a graph. If the menu point "Total revenue" is clicked, I want a second graph to appear. I am new to VBA and this is what I came up with so far:
Sub Iffuntion()
Dim SelectedChart As Range
Dim notVisible As Boolean
If Range("D100").Value = Range("E100").Value Then
ActiveSheet.ChartObjects("Testchart").Visible = True
Else
ActiveSheet.ChartObjects("Testchart").Visible = notVisible
End If
End Sub
It works, but I have to execute the VBA to make the graph appear/disappear and I would like that to happen automatically. Also the condition should eventually be in another worksheet to keep the sheet with the graphs nice and tidy. I read that to achieve this I have toI have to activate the other worksheet. Would you recommend this way or is there a better solution?
Thanks for the help and best regards!
Pete
EDIT: Here is the link to a sample file with the proposed solution of Cor_Blimey, that I couldn't get to work properly. The interconnections in the excel are more complicated than they would have to be, but I wanted to be as accurate ad possible in displaying what is actually happening in my excel. Thanks for taking a look!
https://dl.dropboxusercontent.com/u/18406645/sample.xlsm
Assuming you mean that they change, from a data validation drop down list, the contents of a cell then you can put your code into the Worksheet's Worksheet_Change event. This event is fired when the user changes the content of a cell (or by an external link).
Then test for the cell being the right cell then run your code.
Like:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Intersect(Target, Me.Range("D100"))
If Not rng Is Nothing Then
If rng.Value = Me.Range("E100").Value Then
Me.ChartObjects("Testchart").Visible = True
Else
Me.ChartObjects("Testchart").Visible = False
End If
End If
End Sub