Excel Macro to Change Active Cell - vba

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.

Related

Disable a cell in Excel using VBA

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

VBA - How to change colour of no. of columns based on value in cell?

Can anyone please help as I'm new to Excel VBA?
My requirement is to change colour of a range of cell based on value in a particular cell.
e.g. if I've cell B8 with value 2 then change the background colour of B9:B11 to red and C9:C11 to red.
and say if the cell B8 has value 3 then change the background colour of B9:B11 to red, C9:C11 to red and then D9:D11 red.
The range expands to B9:E11 for value 4 and so on. So need a VBA code, Conditional formating for 100 value will not be time effective I guess.
How do I do this?
Thanks
Amrik
Use conditional formatting. Select B9:B11, make a conditional formatting rule based upon formula (=$B$8 >= COLUMN(B$9). Copy B9:B11 to C9:C11, etc and the conditional formatting rule will follow.
If you prefer to be more explicit, you could further avoid vba and just use propel (http://propel.codeplex.com). This example does what I think you described. Just hide rows 1 and 2 when you're done.
Use a worksheet event handler to catch when the value in the cell changes. And using the code below you'll be able to change the color of the range
Private Sub worksheet_Change(ByVal target As Range)
Range(Cells(9, 2), Cells(11, Cells(2, 8))).Interior.Color = vbRed
End Sub

How to highlight a cell when formula result from another sheet changes?

This is one that's been killing me and I've tried almost every solution on the Internet.
Here's background. I have an HR model that has each department broken out on separate tabs. I want to run an extract from our payroll system each payroll run and send highlight any updates individually. If someone's title or salary or status changes, I want to have that called out by highlighting the cell.
Each tab uses an INDEX/MATCH lookup to the extract tab to pull in the current information. What I want is if any value changes or is new(new hire, for example), highlight the cells.
I've played with Worksheet_Calculate and Worksheet_Change to no avail. Worksheet_Change doesn't fire because I'm not making the change directly on the sheet and Worksheet_Calculate doesn't have the Target object for to reference. I've tried the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim updatedCell As Range
Set updatedCell = Range(Target.Dependents.Address)
If Not Intersect(updatedCell, Range("A:A")) Is Nothing Then
updatedCell.Interior.ColorIndex = 3
End If
End Sub
The range I actually need evaluated is A7:R104 but I've been trying to get anything to work when linked to another sheet.
This works fine if formula of target cell is pointing to another cell on same sheet. The moment you point to one on another sheet it doesn't work. I've tried most of the solutions on here with no success. I've even tried putting the Worksheet_Change on the extract sheet and see if I can trigger it that way with no luck.
Is there a recommended solution to triggering a change to a cell for a formula linked to another sheet?
so I just saw this post, I don't know if you've found the solution or are still looking, but:
if you select a cell in sheet 3, you can then go to the home tab, go to "conditional formatting" -highlight cell rules - more rules (at the bottom) - and "use formulas to determine which cells to format" and then put your cursor in the formula box. now, select a cell in sheet 1 (click the sheet1 tab, and click a cell) and you'll notice it should populate the address for sheet1, and the cell u selected. now type <> after that cells address, then select sheet2 and a cell. then click format, and choose a fill color. then ok. if you go to conditional formatting and manage rules it will show there the rule / formula and which cells it applies to.
doing this i was able to select cell D10 in sheet 3, and make it an ugly green if cells in sheet1 and 2 didnt match (I picked which cells) you can also select a range of cells.
thusly, you can apply this rule to whatever dells you want, and if you record a macro of you setting this conditional formatting, you can manitpulate that macro to apply it to a bunch of different cells, and change the ranges. (using loops / variables)

Excel VBA code (assigned to a button) to hide/unhide rows based on cell values across multiple sheets

this is my first post here and additionally I also have completely no knowledge on VBA whatsoever... so please excuse my ignorance ;-)
I'm working on a price list which has a quantity column. The same files has multiple worksheets with multiple currencies. What I need to achieve is to create two buttons on each sheet to hide / unhide all rows where the quantity cell equals zero.
So for example you want to select certain items from the list, so you enter the quantity into appropriate cells (quantity column) and press the button to hide all other rows for which the quantity equals zero.
Now, I found the code for this somewhere already, but it only works on a first sheet and when I copy the sheet (to create another currency) with the buttons and press the button it will still apply the changes (hide / unhide rows) to the first sheet. This code is below:
Public Sub HideRows()
Dim cell As Range
For Each cell In Range("BOQ")
cell.EntireRow.Hidden = (cell.Value = 0 And cell.Value <> "")
Next cell
End Sub
and to unhide:
Public Sub UnhideRows()
Dim cell As Range
For Each cell In Range("BOQ")
If (cell.Value = 0 And cell.Value <> "") Then cell.EntireRow.Hidden = False
Next cell
End Sub
I would be extremely grateful if anyone could propose a proper script to do that separately on multiple sheets. Also to avoid the issue when after a print preview the script runs like a 100 times slower.
Thanks in advance.
Range("BOQ") refers to a range on the first sheet.
So no matter which sheet is selected, the macro will affect that range on sheet 1.
To make the code flexible to the sheet you're on, consider changing it to something like:
Activesheet.Range("A2:A10")

Using Search and Replace with VBA/Excel

Got an annoying Excel/VBA issue that I can't seem to get around - would appreciate any help.
I have a formula in a spreadsheet that says something along the lines of if the cell to its left =1, then the cell itself =on, and if not, ="off".
Is it possible to write some VBA search and replace code that turns "off" to "totally_off" when run?
At the moment it just seems to be editing the formula itself, while I really want it to delete everything in that cell and just replace it with the phrase "totally_off"
Thanks for your help.
The following code should work. Just replace the range B1:B10 with the range that the formulas you want to change are in. The procedure tests the value of the cell immediately to the left of the cells in the formula. If the value is zero, it replaces the formula with the "totally off".
Sub totally_off()
Dim rng As Range
Dim cell As Variant
Set rng = Range("B1:B10")
For Each cell In rng
If cell.Offset(0, -1).Value = 0 Then
cell.Value = "totally off"
End If
Next cell
End Sub
Here's a non-VBA, non-search and replace alternative.
Set up a filter on the formula column with Sort and Filter / Filter on the Home ribbon.
Select "off" from the drop-down menu on the column.
Type "totally off" in the first cell of the filtered column and copy it down to the bottom of the column.
Then remove the filter. The formulas evaluating to "on" will remain intact.
No need for VBA - simply use an AutoFilter:
Select the column with the on/off formula
Apply an AutoFilter (Ctrl-Shift-L
Filter for off
Again, select all values
Simply type in your replacement value totally_offinto the formula bar - but press Ctrl-Enter
Done!