Creating place holder text in Excel - excel

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.

Related

Excel VBA - Copy a formula result to a cell in another sheet + add succesive formula results in consecutive rows each time the formula is recalculated

I am running a partially randomize set of data and trying to find the best solutions depending on certain parameter changes. I need to "record" certain solutions and then compare different results for different parameters each time the randomized variables are recalculated.
I would like to do the to following:
On Sheet1, cell S255, is the result of a formula =SUM(M252:S252)
I need to automatically add that result (Sheet1 S255), to Sheet5, column A, starting at A1.
Then, each time the formula is recalculated and the result changes, I need the new result to be added to the consecutive row to the previous result (so the second result would go to A2, third one to A3, and so on).
Looking for similar cases I have come to be able to do 1. and 2. using this event:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet: Set ws = Sheets("Sheet5") 'declare and set the worksheet the data is to be copied into, amend the sheet name as required
If Target.Address = "$S$255" Then 'if anything changes in C6 or C9 in this sheet
ws.Range("A1").Value = Target.Parent.Range("S255") 'copy the value from cell C10 in this sheet to Sheet2 in cell E5
End If
End Sub
Doing 3. is proving more challenging. What event would be suitable to do so?
Thanks in advance for your time and understanding!
Thats how you are able to solve it with the Worksheet_Calculate event like BigBen suggested. Under "Random numbers" are just a few numbers with the RANDBETWEEN-Function for showing puposes. Just change the code for your case. Everytime you press "Delete" for exaple the new sum will be set under the Results column.
Private Sub Worksheet_Calculate()
Dim lastRow As Long
'EnableEvents must be switched off so that the macro does not
'call itself in an endless loop by cell change
Application.EnableEvents = False
lastRow = WorksheetFunction.CountA(Range("C:C")) + 1
Range("C" & lastRow).Value = Range("A9").Value
Application.EnableEvents = True
End Sub
'example saving sum ONLY IF SUM CHANGES
Private Sub Worksheet_Calculate()
Dim offsetLastSum As Long, curSum As Double
offsetLastSum = Range("NEXT_SUM_OFFSET").Value2
curSum = Range("THE_SUM").Value2
With Range("SUM_HISTORY_HEAD")
If .Offset(offsetLastSum - 1).Value2 <> curSum Then
Application.EnableEvents = False
.Offset(offsetLastSum).Value2 = curSum
Application.EnableEvents = True
End If
End With
End Sub

Add a Value Automatically to a Cell Based on Another 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.

Excel: How to find the value of in the cell which is highlighted without directly referencing

Quick for excel: How can I find the value of in the cell which is highlighted in green without directly referencing? Do I have to use the VBA? Are there any other easier ways?
Wrote some small VB code on the cell selected to find the value based on the color
Option Explicit
Public Sub GoGreen()
Dim rng As Range, cell As Range
Set rng = Range("A4:P19")
For Each cell In rng
If cell.Interior.ColorIndex = 14 Then ActiveCell.Value = cell.Value
Next cell
End Sub

Change Excel Column Background colour with Excel Macro

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.

How to use the Worksheet_Change event for Conditional Formatting?

I have a worksheet which contains a list of tasks, one on each row. Column A is the task name, column B is the date when it must be done and Column C is the person who must do it. Column D is used to indicate when it's been done. If this column contains anything, then the background colour for whole row should be grey, otherwise it should be white.
I'm thinking the worksheet_change event is the best way to handle this. I suppose I could use Conditional Formatting, but that seems to be vulnerable to breaking if cells get dragged around - I need this to be as "bullet-proof" as possible!
In pseudo-code, I'm trying to achieve the following:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target includes a cell in column "D"
If "D" is not empty
Set entire row background to grey
Else
Set entire row background to white
End If
End If
End Sub
Can anybody give me any pointers about the best way to implement this? Am I even on the right lines, or is there a better way?
I think you can use the following condition on every cell:
=INDIRECT("$D" & ROW())>0
I did some copy/pastes and dragged the cells around, and the conditional format did not break.
With Conditional Formatting:
Go to Tools->Options->General and activate the R1C1 reference style
Condition: =ISEMPTY(RC4)
With VBA:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim found As Range
Dim cell As Range
Dim colStart As Integer
Dim colEnd As Integer
Set found = Intersect(Target, Me.Columns(4))
colStart = 1
colEnd = 4
If Not found Is Nothing Then
For Each cell In found
With Me.Range(Me.Cells(cell.Row, colStart), Me.Cells(cell.Row, colEnd)).Interior
If IsEmpty(cell) Then
.ColorIndex = 15
Else
.ColorIndex = xlNone
End If
End With
Next cell
End If
Set found = Nothing
End Sub
I recommend using Conditional Formatting

Resources