I am trying to do few calculations and update the result of the same in a cell upon clicking the Calculate button (Active X button).
When the Excel sheet is opened, I want few of the cells to be disabled and greyed out. To implement the same, I have written the below lines of code under the Workbook_Open() Sub. All the conditions written under the sub are working fine, except, the statement that I have written to disable the cell (written the locked function to disable the cell).
After some surfing, I came to understand that sheet needs to be protected if a cell needs to be disabled/locked. So, I added an extra line in the code to protect the sheet. However, this stopped the result to be updated in a cell.
Upon clicking the calculate button, excel states that the "Sheet is protected". So, is there anyway to enable/ disable the cell using VB Script and without protecting the sheet please?
Private Sub Workbook_Open()
Dim b1 As Variant
Set b1 = Sheets("Calculation Tool").CommandButton22
b1.Enabled = False
Range("B4:C4") = ""
Range("E4:O4") = ""
Range("E9:F9") = ""
'This is the code written to disable the cells N4 and O4
Sheets("Calculation Tool").Range("N4:O4").Locked = True
Sheets("Calculation Tool").Protect
End Sub
You cannot "disable" a worksheet cell per-se; it's not like a textbox control.
You can, however:
set the cell's fill color to gray (Ctrl+1 to enter Format Cells > Fill tab)
lock the cell to prevent changes (Ctrl+1 to enter Format Cells > Protection tab)
protect the worksheet (Review tab > Protect Sheet) so the cell locking takes effect.
If you need to change the locked cell you can programmatically unprotect the worksheet, make the change, and then re-protect it. Also note that there are several options available when protecting the worksheet.
Alternatively, you could instead use text boxes, and then disable/lock it like you would other controls.
See Also: Lock or unlock specific areas of a protected worksheet
(Based on your "greyed-out" description, I believe you were thinking of textboxes on Microsoft Access Forms.)
try this code for a range:
Range("A1:B2").Locked = True
Related
I have a problem with excel in full screen mode. When I chose another sheet (via macro) and go back to previous sheet (via macro) I cant write something to cell selected cell. Any advice?
My code for fullscreen:
Private Sub Workbook_Open()
Application.DisplayFullScreen = True
Application.CommandBars("Full Screen").Visible = False
End Sub
My macro code for select sheet:
Private Sub skok()
Sheets("Sheet 2").Select
End Sub
I can testify that this problem is happening to my workbook also!
To improve visibility, I make the workbook go fullscreen with the DisplayFullscreen=True in the same sub.
Later, the user can move to Sheets 2 with macro in a command button. On sheet 2 there is a button "Save & Return" which runs a macro like
Sheets("Sheet1").select
But when we return to Sheet1 the cells seem to freeze. But the command buttons still work.The situation can be 'unlocked' when I randomly select another sheet below on the sheet tab. Then returning to Sheet1 I can then enter in the cells!
One strange thing is that although the sheet seem freeze, I can still copy & paste text in a cell using the Ctrl+C & Ctrl+V on keyboard.
I have the same problem, and it is an Excel bug! We can still copy/paste values in cells, write in cells by VBA code, but we cannot edit cells' value anymore by double-clicking the cell (or F2). The cell edition is frozen! (It is like unchecking the option "Allow editing directly in cells" (Options->Advanced options-> (Sector)"Editing options".)
To create the bug:
Click on an ActiveX control while you are in full screen mode (Application.DisplayFullScreen = True)
...and the bug is there now you cannot edit cells anymore!
The bug stays as long as the formula bar is not physically visible on your screen (fullScreen or not) and will STOP (as long as you do not recreate the bug...) as soon as the formula bar is displayed (staying in fullScreen and executing "Application.DisplayFormulaBar = True" won't work)
The bug will "temporarily" stop (with hidden formula bar) if you activate another sheet but will start again if you click on any other ActiveX control in any sheets (fullScreen or not)...
The answer is a good lesson in form. Try to avoid .Activate and .Select. If you always specify your ranges you can avoid situations like this with multiple workbooks. Use the format:
Workbooks("myBook.xlsx").Sheets("Sheet1").Range("A1").Value = "whatever"
or:
with Workbooks("myBook.xlsx").Sheets("Sheet1")
.Range("A1").Value = "A"
.Range("A2").Value = 2
.Range("A3").Value = "Blah"
End With
I am using Excel 2010 and I want that the Excel sheet will be updated (calculated) automatically when the value of a cell (Range("B7")) is changed (The Range("B7") is a drop-down list). I am trying to use the intersect-target method like:
Private Sub Worksheet_Change(ByVal target As Range)
If Not Intersect(target, Range("B7")) Is Nothing Then
ActiveSheet.Calculate
End If
End Sub
I have two questions about this:
Does it matter if I put this Private Sub under any module? Or am I supposed to write it in the exact sheet under the Microsoft Excel -object menu in VBA?
The second question is how I can make this work? The sheet doesn't update (calculate) automatically when I change the Range("B7"). It only updates when I save the Excel file.
The function should be in the sheet which B7 belongs to, e.g. if the B7 you are updating is in "Sheet1" then the code for the Worksheet_Change event should be under Sheet1 too. You can right click the tab for the sheet, and select show code to get to the right place in VBE.
Excel should be aware that any cell using B7 needs to be recalculated when changed, so that should be handled automatically.
Your code will work if you place it in the sheets event not in Modlue please find the below image for your reference.
Select the respective sheet in VB window and double click on it, it will open the worksheet event window (code window) where you need to copy paste the codes. Now try changing the validation cell it should work :)..
I have created a form in Excel. Based on how the user completes one cell, changes which cells they fill out next (for example: When filling out A10 if they answer "X" they will move to B1, if they answer "Y" they will move to B3.)
In order to guide the user through the form, I created a complex set of conditional formatting rules which will "highlight"(background fill) the next cell they need to fill out. Once they complete the cell the formatting on that cell goes away and switches to the next cell.
I have the conditional formatting working exactly how I want. My question is: Is there a way to have the active cell follow this same path. Either by setting up the same formula rules that guide the conditional formatting or is there a way to have a macro auto set the active cell to the "highlighted" cell from the conditional formatting?
Try this:
Private Sub Worksheet_Change(ByVal Target As Range)
Const NEUTRAL = 16777215
Dim r As Range
For Each r In Cells.SpecialCells(xlCellTypeAllFormatConditions)
If r.DisplayFormat.Interior.Color <> NEUTRAL Then
r.Select
Exit For
End If
Next
End Sub
Note: you can edit NEUTRAL at the top if non-highlighted cells have a different color than pure white.
Note: this assumes that the cell you want the selection to jump to is the only cell on the sheet that is highlighted with conditional formatting.
When I protect a sheet and attempt to edit a cell that has for example an Index it will teleport me to that sheet and highlight the column the index is attempting to return. This is not that nice, as sometimes I am referencing other closed workbooks. How can I disable this feature and is there a way to have it go directly to the result of the index instead of highlighting the entire column?
To reproduce problem,
Create Index Formula, Protect Sheet, Double Click that cell, it pops up you can't edit it and then teleports you to the cells that are contained in the array portion of the index. You can imagine how this can become a problem when you are protecting cells to prevent users from editing things they shouldn't and instead it brings them strait to another place they shouldn't be!
I have tried:
Application.DisplayAlerts = False
but it still pops up the error that the sheet is protected and teleports me. Worksheet_FollowHyperlink(ByVal Target As Hyperlink) doesn't seem to be capturing this event either.
To avoid such, select all the cells you want protected and then on Format Cells go to Protection Tab.
Check the Hidden check box.
Then protect the worksheet and try double clicking the cell again. HTH.
Index(array,,)
Whatever cell references are in that array field even if you place them in an if will be opened up by Excel when the sheet is protected. A VBA solution to this problem is
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Locked Then Cancel = True
End Sub
This solution works because it only creates the secret hyperlink(which is not captured by the hyperlink event) when you double click.
If you want to keep the cell locked, the best solution I found is to put the hyperlink in a text box.
I have data from like A1:Z50 but I want to delete only A5:X50 using VBA
(I think it will be a lot faster than dragging the whole cell or using clickA5+shift+clickX50+delete).
How can I do this ?
And then, how to lock the cell to prevent it from getting fixed or cleared ?
You could define a macro containing the following code:
Sub DeleteA5X50()
Range("A5:X50").Select
Selection.ClearContents
end sub
Running the macro would select the range A5:x50 on the active worksheet and clear all the contents of the cells within that range.
To leave your formulas intact use the following instead:
Sub DeleteA5X50()
Range("A5:X50").Select
Selection.SpecialCells(xlCellTypeConstants, 23).Select
Selection.ClearContents
end sub
This will first select the overall range of cells you are interested in clearing the contents from and will then further limit the selection to only include cells which contain what excel considers to be 'Constants.'
You can do this manually in excel by selecting the range of cells, hitting 'f5' to bring up the 'Go To' dialog box and then clicking on the 'Special' button and choosing the 'Constants' option and clicking 'Ok'.
Try this
Sheets("your sheetname").range("A5:X50").Value = ""
You can also use
ActiveSheet.range
Not sure its faster with VBA - the fastest way to do it in the normal Excel programm would be:
Ctrl-G
A1:X50 Enter
Delete
Unless you have to do this very often, entering and then triggering the VBAcode is more effort.
And in case you only want to delete formulas or values, you can insert Ctrl-G, Alt-S to select Goto Special and here select Formulas or Values.