Excel (VBA): Disabling cell formatting except bold font - excel

I'd like to disable changing the format of the cell except making font bold. Is there any way to achieve that?
Obviously,
.Protect AllowFormattingCells:=True enables all possible format changes.
I've thought that maybe making custom button on Ribbon could serve for this (i.e. unprotecting sheet, making the content bold and protecting again), but I wonder whether there is some more convenient approach to this problem.
I've come across similar issue at http://www.excelforum.com/excel-programming-vba-macros/676299-use-vba-to-lock-all-cell-formatting-except-background-color.html - but it also remains unsolved.

Not a perfect solution, but a possible workaround. In the ThisWorkbook module, paste these events:
Private Sub Workbook_Activate()
Application.OnKey "^b", "MakeBold"
End Sub
Private Sub Workbook_Deactivate()
Application.OnKey "^b"
End Sub
Then, in a regular module:
Sub MakeBold()
ActiveSheet.Unprotect
On Error Resume Next
Selection.Font.Bold = Not (Selection.Font.Bold)
On Error GoTo 0
ActiveSheet.Protect
End Sub
Limitation is that it only works for the keyboard shortcut, and not the ribbon button. I suppose you could create a custom button and "disguise" it as the Bold button, but it's still an imperfect workaround.

Related

Is it possible to force and stay on automatic calculation on Excel with VBA?

I'm sending an Excel workbook to users where I need automatic calculation to be always activated.
The problem is that I can't prevent users to put on manual calculation once they have the file.
The idea I had was to put on automatic calculation each time the user click somewhere in the workbook.
I tried to solve the problem with VBA and a workbook_activate macro :
Private Sub Workbook_Activate()
Application.Calculation = xlAutomatic
End Sub
It worked a time but now it seems that it doesn't work anymore.
Do you know what I can do to solve my problem ?
Foxfire's comments are perfectly valid. This should be approached as a training issue, rather than a technical change. However, there's nothing wrong with trying to do both to increase security. You could even alter the code to detect when a user has switched off autocalc and warn them this is against best practise.
The idea was to put on automatic calculation each time the user click somewhere in the workbook. But I don't know if this is possible...
You could use the Worksheet_SelectionChange event and that would give you exactly that. However, you'd have to put this in each of the worksheets.
A slightly better approach might be to only run when a change is made - after all, only a change should require a recalc. This can be done at a Workbook level like so:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Application.Calculation = xlCalculationAutomatic
End Sub
An alternative version - with a warning:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Application.Calculation <> xlCalculationAutomatic Then
Application.Calculation = xlCalculationAutomatic
MsgBox "Please do not disable automatic calculation blah blah blah.."
End If
End Sub

Run-Time Error '-2147417848 (80010108)' when using ComboBox events [duplicate]

I need a workbook to display the Combobox List Dropdown when it opens.
The combobox in the Workbook is a form control, so a shape.
Cant seem to find the associated property.
If you are using ActiveX Controls then see the below, else if you are using Form Controls then replace them with ActiveX Controls if you want the drop down to happen via code. The below code will work for both ActiveX Controls in a Form as well as Worksheet. If the Control is on a worksheet then change ComboBox1.SetFocus to ComboBox1.Activate
Two ways I can think of...
Using a simple command
Tried And Tested
Private Sub CommandButton1_Click()
ComboBox1.DropDown
End Sub
Using Sendkeys. Sendkeys are unreliable if not used properly.
Tried And Tested
Private Sub CommandButton1_Click()
ComboBox1.SetFocus
SendKeys "%{Down}"
End Sub
SCREENSHOTS
I have had plenty of crashes with .dropdown but find some success with
the SendKeys...
I consider best
UserForm combo box is as Above by Siddharth Rout
ComboBox1.SetFocus
SendKeys "%{Down}"
for some Combo boxes on a worksheets
CB.DropDown Is enough
.. Just as well as they have no setfocus or activate

Excel not handling unload of Userform correctly

I unfortunately can't share the workbook that this is happening in due to security reasons. But I was wondering if anyone had experienced the following and if so how they had fixed it.
I'm having an issue where I unload one of the userforms in my workbook using
Unload UserForm
Whilst this userform is open it also changes the Activesheet. After the code has finished running it seems that the Activesheet although visually has changed for all other purposes it is still set to the previous one.
I am then unable to scroll (unless I double click into a cell first) and the whole sheet tells me it is locked (until I somehow trigger the Change procedure or manually toggle between another sheet and back).
I've also tried to Unprotect the sheet but in doing so the Ribbon continues on saying Unprotect sheet but oddly the sheet which was active when I loaded the userform becomes unprotected instead.
The Userform is loaded by using UserForm.Show. I haven't experienced this behaviour in any other workbooks that I have.
Have tried:
Toggling EnablingEvents
Toggling ScreenUpdating
Resetting the freeze panes area
Changing where the destination sheet is activated
All to no avail. Has anyone come across similar or know of a reason this behaviour could be experienced?
Edit:
Load procedure:
Sub SearchRev_Button()
If ConnectToDB() = True Then
Review_Search.Show
Call CloseConnections
End If
End Sub
Unload procedure:
Private Sub LoadReviewButton_Click()
'Loads the selected review to be edited
Application.Calculation = xlCalculationManual
If Me.ResultListBox.ListIndex = -1 Then Exit Sub
Select Case rs.Fields("ReviewMethod").Value
Case "Review"
If rs.Fields("ReviewMethod").Value = "ChangeReview" Then Review_ChangeReviewType.Show
FetchReviewDetail ReviewID
Unload Me ' <-- This is the line that unloads the userform to return to sheet
Case "ISP"
Unload Me
ISP_Dash.Show
Case Else
MsgBox prompt:="You cannot make changes to this review", Title:=TitleString
End Select
Application.Calculation = xlCalculationAutomatic
End Sub

Sheet select from a Userform button causes data entered to go on previous sheet

I call a macro called SelectSheet1, from a button on a userform, to select Sheet1.
When data is entered afterwards it is put on the previous sheet.
This is happening on Excel 2013. I confirmed it is not a problem in Excel 2007.
Also it is not a problem if the macro is run directly from the developer tab, keyboard shortcut, quick access toolbar or ribbon customization.
The userform command button code:
Private Sub CommandButton1_Click()
Call SelectSheet1
Unload UserForm1
End Sub
The SelectSheet1 macro selects the sheet:
Sub SelectSheet1()
Sheets("Sheet1").Activate
End Sub
Link to xlsm file in dropbox
Link to youtube video if you want to see with your own eyes
It is a strange error and wondering if it a problem with Excel 2013, something changed in the way they do things and possibly there is a workaround.
Make sure there is only a single sheet Select'ed before you Activate a sheet:
Sub SelectSheet1()
Sheets("Sheet1").Select
Sheets("Sheet1").Activate
End Sub
Remember: "Although only a single sheet may be Active, many may be Selected."
I was able to figure out how to get it to work, but not sure why this solves the issue.
Im unloading the Userform before call macro and I tried using select instead of Activate, those did not help. But after I switched so that the UserForm loads with vbModeless then my problem went away, not sure why this fixed it.
For me the key to fixing this issue was vbModeless, but would love if somebody who understood more could explain why.
Private Sub CommandButton1_Click()
Unload UserForm1
Call SelectSheet1
End Sub
Sub ShowMainMenu()
UserForm1.Show vbModeless
End Sub
Sub SelectSheet1()
Sheets("Sheet1").Select
End Sub

Trap delete key

My problem is fairly trivial: I need to apply logic to the delete button in Excel. In a related question I asked for means to clear cells in a pivot table, and realising now that this might not be the right approach, this is yet another alternative I'm considering. Unfortunately, I admittedly have little experience with Visual Basic, and this task is proving such a challenge (quite surprisingly).
Is it possible to trap (as in listen to) the delete-key in Excel and trigger a subroutine in VBA when pressed?
I'd appreciate all kind of input! Thanks!
Yes.
You case use Application.Onkey to assign code to keys
If you
(1) add this code to a sheet module that contains your pivottable (right-click your sheet tab, View Code and past the code below)
This code activates and then de-activates the intercept when the user enters then leaves this particular sheet:
Private Sub Worksheet_Activate()
Application.OnKey "{DELETE}", "Intercept"
End Sub
Private Sub Worksheet_Deactivate()
Application.OnKey "{DELETE}"
End Sub
(2) add this code to a regular module
Sub Intercept()
MsgBox "user just pressed delete"
End Sub
then delete on that sheet will be trapped and the user given a message

Resources