For a Excel document that is becoming larger then the internet I am trying to get rid of automatic lay outs cause they are seriously slowing our excel to an extend where its becomes non usable.
I attempted to create a macro that colours the background of a cell based on the active cell value.
Sub find()
Dim CurValue As String
Dim ColorIndex As String
Dim Findr As Range
Dim MyRange As Range
Set MyRange = ActiveCell
CurValue = ActiveCell.Value
With ActiveCell
Set Findr = Range("A1:A10").find(CurValue)
If Not Findr Is Nothing Then
ColorIndex = Findr.Offset(ColumnOffset:=1).Value
MyRange.Interior.ColorIndex = ColorIndex
' rngStart.Select
End If
End With
End Sub
This sub works perfectly.
However for the problem:
Now i want to call it whenever a cell changes but if I call the macro whenever a cell changes in my Sheet.I tried using the sheet sourcecode for every change.
But then it uses the cell the user jumps to after the change rather then the previous edited cell.
How do i get this Macro to call for every changed cell rather then the new select cell?
Putting the following in the appropriate Worksheet object should work:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim CurValue As String
Dim ColorIndex As String
Dim Findr As Range
For Each AC In Target.Cells
CurValue = AC.Value
Set Findr = Range("A1:A10").find(CurValue)
If Not Findr Is Nothing Then
ColorIndex = Findr.Offset(ColumnOffset:=1).Value
AC.Interior.ColorIndex = ColorIndex
End If
Next
End Sub
Note - one of the A1:A10 needs to be blank, but the value in B next to the blank must have a ColorIndex value and cannot be blank. I'd suggest a zero to empty all colour out of the cell but I can't see what your sheet's 'empty' cells look like..
The For..Each loop is to handle where more than one cell is changed at once, it performs the colour-change on each changed cell.
Also, cells that 'change' due to the result of a formula changing, rather than an edit, will not change using this method.
Related
Please help me, as I am not very experienced with VBA.
Let's say I have the following cells with specific background colour as defined by conditional formatting. (Cells A3:A6)
Now I would like cells (C3:C6) to copy the colour from A3:A6 and apply it to C3:C6.
Please help me get a code that will recognize the colour set from Conditional formatting and change C3:C6 respectively. Thank you very much.
EDIT:I messed up and didn't enter any values for cells so it may be confusing how is there conditional formatting when nothing is in the cell. Assume cells with colours are 1-5.
Current code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xSRg, xDRg, xISRg, xIDRg As Range
Dim xFNum As Long
On Error Resume Next
Set xSRg = Sheet1.Range("A3:A5")
Set xDRg = Sheet1.Range("C3:C6")
For xFNum = 1 To xSRg.Count
Set xISRg = xSRg.Item(xFNum)
Set xIDRg = xDRg.Item(xFNum)
xIDRg.DisplayFormat.Interior.Color = xISRg.DisplayFormat.Interior.Color
Next xFNum
End Sub
To be honest - your question/your requirement doesn't make any sense in my eyes (esp. within the selection change event and w/o using target) - but here is the code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With Sheet1
Dim rgFormatCondition As Range
Set rgFormatCondition = Me.Range("A3:A6")
Dim rgTarget As Range
Set rgTarget = Me.Range("C3:C6")
Dim i As Long
For i = 1 To rgFormatCondition.Rows.Count
rgTarget(i).Interior.color = rgFormatCondition(i).DisplayFormat.Interior.color
Next
End With
End Sub
You have to apply the displayformat-color of the cells with format condition to the interior color of the target cell.
I want to add a value to a cell based on another with VBA but I'm not sure how. I already searched on internet about it but can't find anything.
I have a table, and on the Column C, if any cell contains the text "MAM" (because it might have MAM-565), then change the value from Cell A to "Wrong", but if it contains "NAC", then change value to "Correct". It should be in the same row as the text found.
Also, I want to add the date automatically to cell E every time Cell in D is filled.
This the code I have already:
Private Sub Worksheet_Change(ByVal Target As Range)
'Add Issue Type'
Dim Code As Range
Set Code = Range("C2:C100000")
For Each Cell In Code
If InStr(1, Cell, "NAC") Then
Range("A2:A10000").Value = "Correct"
ElseIf InStr(1, Cell, "MAM") Then
Range("A2:A10000").Value = "Wrong"
End If
Next
End Sub
This how my table looks like:
Table
Thanks in advance guys :)
To automatically add the datestamp:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng as Range
Set rng = Intersect(Target, Me.Range("D:D"))
If rng Is Nothing Then Exit Sub
On Error GoTo SafeExit
Application.EnableEvents = False
Dim cell as Range
For Each cell in rng
If Not IsEmpty(cell) Then ' don't do anything if cell was cleared
cell.Offset(,1).Value = Date
End If
Next
SafeExit:
Application.EnableEvents = True
End Sub
As far as the Correct/Wrong, this can easily be done with a formula (ISNUMBER(SEARCH(...)). I don't see the need for VBA here.
Even better, create a table using Ctrl+T. Excel will automatically add the formula in column A in new rows.
I want to exclude the topmost selection from a list of selection(CTRL + Click) in a column. For example, if I selected Cell V12 + V10 + V14 + V9. The topmost selection is V9. I have this code that loops through all the selected cells but I need to exclude the topmost selection (i.e V9).
Here's the working code:
Dim rngPart as Range
For Each rngPart in Application.Selection.Areas
MsgBox rngPart.Address
Next
I need help with excluding the topmost selection
I would break this up into a few functions to compose it together.
First I would want a function to find the top range based on its row. To do this we just need a simple loop and assign the range if it is less than the previous cell.
Private Function getTopRange(ByRef source As Range) As Range
Dim cell As Range
For Each cell In source
If getTopRange Is Nothing Then
Set getTopRange = cell
End If
If cell.Row < getTopRange.Row Then
Set getTopRange = cell
End If
Next cell
End Function
Then I would create the function that would return the range excluding the top one.
Again to do that, we just need a simple loop. If it isn't the top cell, then union it to our return range value.
Private Function excludeTopRange(ByRef source As Range) As Range
Dim topRange As Range
Set topRange = getTopRange(source)
Dim cell As Range
For Each cell In source
' Only add if not the top cell
If cell.Address <> topRange.Address Then
If excludeTopRange Is Nothing Then
Set excludeTopRange = cell
Else
Set excludeTopRange = Union(excludeTopRange, cell)
End If
End If
Next cell
End Function
Putting it all together you just call our new function!
Private Sub test()
Dim source As Range
Set source = Application.Selection
Dim excluded As Range
Set excluded = excludeTopRange(source)
MsgBox excluded.Address
End Sub
It's a good design to try and keep your functions small and reusable like this. It's easier to read, debug, test, and reuse!
I'll start by making my objective clear, and then explaining it fully.
My goal is to check for non-blank values in a range, but only in the hidden cells of that range, and then use conditional formatting in a different cell, depending on whether the cells in the range are empty or not.
I have a named range called Location_Address_RangeCheck that covers the cells directly to the right of the location numbers, like this (location numbers are not part of the range).
When the Number of Locations is changed, the rows that go beyond that number (up to 25) are automatically hidden on worksheet_change to reduce clutter and reduce scrolling to see the stuff below it. That code works fine, so I'm not posting it here so as to not confuse anyone with what I'm trying to accomplish.
I want to provide a safeguard to ensure that there aren't values in the hidden rows that could affect outputs (i.e., if someone selects "3" for Number of Locations, but there is data in cells that might be on the row of the 8th location).
My goal is to check for non-blank values in the range, but only in the hidden cells, and then use conditional formatting in the cell next to the number of locations chosen, depending on whether the cells in the range are empty or not.
So if there is data in the hidden cells, then it would cause the sheet to look like this
.
I've tried so many different things so far, but I'm not making any progress. I've scoured the internet trying to find a solution, but everything I've found is about finding things in visible cells, which is the opposite of what I'm trying to achieve.
Here is the code I have written so far, which I know does not achieve my objective:
Sub testhiddencells()
Dim myRange As Range
Set myRange = Range("Location_Address_RangeCheck")
NumRows = Application.WorksheetFunction.CountA(myRange)
If Range("Location_Address_RangeCheck").Hidden = True Then
If Application.WorksheetFunction.CountA(Range("Location_Address_RangeCheck")) <> 0 Then
MsgBox "There's something there"
End If
End If
End Sub
Here is a minimal example to check with a cell is both hidden and is non-empty:
Option Explicit
Sub Test()
Dim ws As Worksheet
Dim rngToCheck As Range
'test range - all cells populated with 'a' and 3 are hidden
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set rngToCheck = ws.Range("A1:A7")
If TestForNonBlankCellsInHiddenRange(rngToCheck) Then
'do you conditional format stuff here
End If
End Sub
Function TestForNonBlankCellsInHiddenRange(rngToCheck As Range) As Boolean
Dim rngCell As Range
Dim blnCheck As Boolean
'assume that hidden cells are blank
blnCheck = False
'iterate range
For Each rngCell In rngToCheck
If rngCell.EntireRow.Hidden And Not IsEmpty(rngCell.Value) Then
'found a hidden and non-empty cell
blnCheck = True
'debug address of this cell
Debug.Print rngCell.Address
End If
Next rngCell
'return check
TestForNonBlankCellsInHiddenRange = blnCheck
End Function
Looking at the code you used already, you should be able to adapt this to the particular use case of your worksheet.
I'm still fairly new to excel but I can just about work out some simple formulas. I'm looking to create a placeholder text effect. The way I want to achieve this is like so;
Cell A1 : has the place holder text and is hidden.
Below this, cell B1 containing the formula.
This formula checks to see if Cell C1 is blank if C1 is blank it returns a value to C1. That value being the value of A1.
This is what I have in my head but I'm unsure on how to code this. I have bounced around the web for a while but I can't find a specific answer. The closest I have gotten is;
=IF(C6<>"","",C4)
Just to clarify Im looking to oput the result of formula B1 into C1.
OK, so put this in the worksheet's code module. Make sure you put it in the module for the specific sheet you're monitoring.
First, fill your range C5 through C99 with the formula like =$C$1. You should only need to do this one time, the macro will take care of it later.
Private Sub Worksheet_Change(ByVal Target As Range)
'the formula reference
Dim defaultFormula As String
defaultFormula = "=$C$1"
'The default text cell:
Dim defaultText As Range
Set defaultText = Range("C1")
'The cells you want to monitor:
Dim rng As Range
Set rng = Range("C5:C999") '## Modify as needed
'Cell iterator
Dim cl As Range
If Intersect(Target, rng) Is Nothing Then Exit Sub
'Avoid infinite looping
Application.EnableEvents = False
'If the user has deleted the value in the cell, then replace it with the formula:
For Each cl In Intersect(Target, rng)
If Trim(cl.Value) = vbNullString Then
cl.Formula = defaultFormula
End If
Next
'Turn on Events:
Application.EnableEvents = True
End Sub
What this does is (hopefully) pretty self-explanatory from the comments in the above code The Change event is raised any time a cell on the worksheet is changed.