VBA seems to disappear after I protect a range of cells - vba

Hi I can't seem to get my head around this problem:
After I lock a range of cells A1:U1 (and all other cells are unlocked) and I then protect the worksheet - the VBA in the worksheet doesn't appear to work anymore - is there any way around this??
Thanks,
Kieran

If the VBA changes cells that you have locked then you need to update the VBA to unlock the worksheet before running the rest of the code. Then complete the Sub by locking the worksheet.
'Add to beginning of the Sub
Sheets("worksheet_name").Unprotect Password:="password"
'Add to end of the Sub
Sheets("worksheet_name").Protect Password:="password"

Related

Locking Worksheet

I am using Excel 2013, and have a macro that copies a "master" worksheet to the end of the workbook, once filled in by the user. The worksheet that's copied, is renamed, according to the "master" report number. Is there a way that once the sheet is copied to the end, it can be locked so the user can not make any changes to it. I just want them to be able to view it. I've researched some sample code online, but nothing seems to do what I am trying to do. Anyone have any ideas or can help? Much thanks
You can use the .Protect and .Unprotect properties. Below is an example sub that protects all worksheets, charts in a workbook and then protects the workbook itself. Here's a site with more detailed info, and it would also be good to review how items are protected in Excel generally.
Sub protectWk(wk As Workbook)
Dim sh as Worksheet, ch as Chart
For Each sh In wk.Worksheets
sh.Protect ("password")
Next
For Each ch In wk.Charts
ch.Protect ("password")
Next
wk.Protect ("password")
wk.Close SaveChanges:=True
End Sub

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.

Excel VBA - Loop not adding autofilters to my worksheets

I have some code that (should) loop through all my worksheets and add an autofilter, however for some reason they're not showing up. When I turn on events, I can see that it is quickly being added then removed almost instantly. I'm assuming that this is because of something written earlier in my code, but I have too much code before this to evaluate what is causing the issue... Is there something I can add to guarantee the filters are added? Code:
Dim wsfixer As Worksheet
For Each wsfixer In ActiveWorkbook.Worksheets
With ActiveSheet
.AutoFilterMode = False
.Range("A:S").AutoFilter
End With
On Error Resume Next
Next
If you want to process the code on each worksheet, change
With ActiveSheet
to
With wsfixer
By using With ActiveSheet the code within the With block is being "shortcutted" to using the active sheet (e.g. .AutoFilterMode is treated as ActiveSheet.AutoFilterMode). So you are executing the same code over and over to the one active sheet.

Excel to replace formulas with values

I have an Excel workbook (1) with around 9 sheets that is pulling in and manipulating data from a second workbook (2).
After pulling in the data from workbook (2) I need to be able to replace the formulas in workbook (1) to the resulting values that the formulas have produced, from here I will then save the workbook (1) with the results.
Is there a macro that can do this for me?
On your new workbook some basic code such as:
Sub Value()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
ws.UsedRange.Value = ws.UsedRange.Value
Next
End Sub
While the OP is dated, I want to make note of a non-loop method that is useful. In certain scenarios, loops can really slow down the code execution. To replace formulas in a cell --without a loop-- try:
With Sheets("example").Range("A1:C1000")
.value = .value
End With
You can revise the reference as necessary, but the execution is seamless, fast, and as a bonus - prevents range highlighting that cannot be cleared if you pursued the .copy + .pastespecial xlPasteValues approach.
What seems to work for me is to use concatenate()
So, for example, the formula I have referencing a cell from another sheet is:
=arrayformula(iferror(index('To Be Processed'!X:X,small(if($A$1='To Be
Processed'!$Y2,row('To Be Processed'!X:X)),row((2:2))),"")))
and if I change to the formula to:
=concatenate(arrayformula(iferror(index('To Be
Processed'!X:X,small(if($A$1='To Be Processed'!$Y2,row('To Be
Processed'!X:X)),row((2:2))),""))))
and it puts in the text value from the reference cell into my second sheet.
Which may or may not be helpful depending on how you populate your sheets--I'm not very good with VBA, though, which means I do more things manually :)

how to prevent re sizing of buttons after release of auto filter in excel 2010 using vba

I have an excel sheet with some buttons (each doing different functions) i have created an auto filter macro as shown below but the problem is when i release the filter my all buttons get very small in their sizes (means they change their original size) although i selected the radio button (Do not move or size with cell) from each button's property.
Sub AutoFilter()
Range("A1:I1628").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$I$1631").AutoFilter Field:=8, Criteria1:="="
Selection.Copy
Sheets("Blank Names").Select
Range("A1").Select
ActiveSheet.Paste
End Sub
I am badly in need of help, please assist me with this issue.plzzzzzz
My guess is that you need to set the object positioning of the button to "Don't move or size with cells" (as seen here, unfortunately I don't have enough reputation to post images yet).
If you have many different buttons in many different spreadsheets, this code should set the property for all of them (provided none of the worksheets are protected, etc.)
Sub test()
Dim ws As Worksheet, sh As Shape
For Each ws In Worksheets
For Each sh In ws.Shapes
If sh.Type = msoFormControl Then
sh.Placement = xlFreeFloating
End If
Next
Next
End Sub
I faced the same issue and couldn't find a solution anywhere in the internet. Then on manual testing, I found that the issue is with the format.
Please clear the format from the range of cells where you're trying to put autofilter
For example, I copied data from source file and pasted the as values and then I put the autofilter.