I have to create a cell like and I am looking a way to do it with excel formulas or VBA code.
With two differents formts red to green or just black, but not in all the case depends If.
For example:
Case 1: If cell F1 and G1 has values the format is Red to green
Case 2: If cell F1 has a value but G is empty the final format in image 1 have to be Just black
This VBA code should work. It goes into the worksheet_change event of your sheet.
I just set it for column I to change colors, but you can extend to J and K if you need to.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
If Target.Column = 6 Or Target.Column = 7 Then
Select Case Target.Column
Case Is = 6
If Target <> vbNullString And Target.Offset(, 1) <> vbNullString Then
With Target.Offset(, 2)
.Characters(Start:=1, Length:=5).Font.ColorIndex = 3
.Characters(Start:=6, Length:=3).Font.ColorIndex = 4
End With
Else: Target.Offset(, 2).Font.ColorIndex = 0
End If
Case Is = 7
If Target <> vbNullString And Target.Offset(, -1) <> vbNullString Then
With Target.Offset(, 1)
.Characters(Start:=1, Length:=5).Font.ColorIndex = 3
.Characters(Start:=6, Length:=3).Font.ColorIndex = 4
End With
Else: Target.Offset(, 1).Font.ColorIndex = 0
End If
End Select
End If
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
Related
I have a macro:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim v
If Target.Cells.Count <> 1 Then Exit Sub
If Target.Column <> 1 Then Exit Sub
v = Target.Value
If VarType(v) <> vbDate Then
Application.EnableEvents = False
If v Like "???##" Or v Like "???-##" Then Target.Value = Left(v, Len(v) - 2) & "20" & Right(v, 2)
If VarType(Target.Value) <> vbDate Then Target.Value = Empty
Target.NumberFormat = "m/d/yyyy"
Application.EnableEvents = True
End If
End Sub
When copying (ex: may20, may-20) from another column to column A in Excel itself with this macro, it allows to paste only once - the next cell is no longer pasted, apparently, the clipboard is cleared after the first paste. I have to copy again from another column. How it can be corrected?
See below - if you need to paste the same value again.
The core problem is that the change event always clears the clipboard - there's no (easy) way I'm aware of to prevent that.
Private Sub Worksheet_Change(ByVal Target As Range)
Const MNTH_NM As String = "[A-Z][A-Z][A-Z]" 'a bit better than "???"
Dim v
If Target.Cells.Count <> 1 Then Exit Sub
If Target.Column <> 1 Then Exit Sub
v = Target.Value
If Len(v) > 0 Then
Application.EnableEvents = False
If UCase(v) Like MNTH_NM & "##" Or UCase(v) Like MNTH_NM & "-##" Then
v = Left(v, 3) & "-20" & Right(v, 2)
Target.NumberFormat = "m/d/yyyy"
Target.Value = v
Target.Copy
Else
Target.ClearContents 'if doesn't match the pattern, clear it
End If
Application.EnableEvents = True
End If 'non-zero length
End Sub
An IF function checks whether the cells in any of the rows in the chosen column of the table is filled in, e.g. Column 1, row 1, 2 and 3 - if any of the cells is filled in - additional rows are unhidden.
The function works when the table is getting filled in but only until something gets deleted from it.
e.g. if the table has all the rows 1, 2 and 3 in column 1 filled in - additional rows appear. If I delete one of the cells values e.g. column 1 row 1 - the additional row hides again. The criteria for it to be unhidden is still there, rows 2 and 3 are still filled in.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 2 And Target.Row = 90 Then
If Target.Value = "" Then
Application.Rows("94:101").Select
Application.Selection.EntireRow.Hidden = True
Else: [94:101].EntireRow.Hidden = False
End If
End If
If Target.Column = 2 And Target.Row = 91 Then
If Target.Value = "" Then
Application.Rows("94:101").Select
Application.Selection.EntireRow.Hidden = True
Else: [94:101].EntireRow.Hidden = False
End If
End If
If Target.Column = 2 And Target.Row = 92 Then
If Target.Value = "" Then
Application.Rows("94:101").Select
Application.Selection.EntireRow.Hidden = True
Else: [94:101].EntireRow.Hidden = False
End If
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = 2 And (Target.Column = 90 Or Target.Column = 91 Or Target.Column = 92) Then
If Range("B90") = "" And Range("B91") = "" And Range("B92") = "" Then
Range("A94:A101").EntireRow.Hidden = True
Else
Range("A94:A101").EntireRow.Hidden = False
End If
End If
End Sub
Check #PEH comment first.
Following your philosophy, try this one:
Private Sub Worksheet_Change(ByVal Target As Range)
With WorkSheets(1)
If (.Range("B90").Value2 <> "") Or (.Range("B91").Value2 <> "") Or _
(.Range("B92").Value2 <> "") Then
If Target.Value = "" Then
.Rows("94:101").EntireRow.Hidden = True
Else
.Rows("94:101").EntireRow.Hidden = False
End If
End If
End With
End Sub
Just custom the WorkSheets(1) to your WorkBook.
Having trouble with time formatting.
I have set the cell to custom format 00:00.
Currently in column A a date is inputted, this can be as 0300 which converts to 03:00 which is perfect or you can just enter 03:00.
I now have a problem if a user enters 03;00 as i need this to display 03:00
how can i ensure that all times are in the hh:mm format and not in hh;mm etc.
This needs to auto change on input for anything in column A, except what is the header (A1:A5) although this should not be affected.
Thanks
On your sheets change event you would place the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge = 1 And Target.Column = 1 And Target.Row > 5 Then
Target.Value2 = Replace(Target.Value2, ";", ":")
End If
End Sub
Explaining the code... it first checks to make sure that the change isn't on multiple cells (ie paste) and that the change is on column A below Row 5. If it does pass the conditional it simply replaces ; for :.
This does what i require.
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim xStr As String
Dim xVal As String
Set rng1 = Range("A:A")
Set rng2 = Range("C:C")
Set rng3 = Range("I:I")
On Error GoTo EndMacro
If Application.Intersect(Target, Union(rng1, rng2, rng3)) Is Nothing Then Exit Sub
If Target.Count > 1 Then Exit Sub
If Target.Value = "" Then Exit Sub
If Target.Row < 5 Then Exit Sub
Application.EnableEvents = False
With Target
If Not .HasFormula Then
Target.Value = Replace(Target.Value, ";", ":")
Target.Value = Left(Target.Value, 5)
xVal = .Value
Select Case Len(xVal)
Case 1 ' e.g., 1 = 00:01 AM
xStr = "00:0" & xVal
Case 2 ' e.g., 12 = 00:12 AM
xStr = "00:" & xVal
Case 3 ' e.g., 735 = 07:35 AM
xStr = "0" & Left(xVal, 1) & ":" & Right(xVal, 2)
Case 4 ' e.g., 1234 = 12:34
xStr = Left(xVal, 2) & ":" & Right(xVal, 2)
Case 5 ' e.g., 12:45 = 12:45
xStr = Left(xVal, 2) & Mid(xVal, 2, 1) & Right(xVal, 2)
Case Else
Err.Raise 0
End Select
.Value = Format(TimeValue(xStr), "hh:mm")
End If
End With
Application.EnableEvents = True
Exit Sub
EndMacro:
Application.EnableEvents = True
End Sub
Thanks
I need two actions to take place on one sheet in my workbook. Both are based on a change event, but do not know how to make them both work. Below is the codes that I have:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range
Set r = Target.EntireRow
If Target.row = 1 Then Exit Sub ' Don't change header color
If r.Cells(1, "AD").Value <> "" Then
r.Font.Color = RGB(0, 176, 80)
Else
r.Font.ColorIndex = 1
End If
End Sub
And this one:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range
Set r = Target.EntireRow
If Target.row = 1 Then Exit Sub ' Don't change header color
If r.Cells(1, "E").Value = "6" Then
r.Font.Color = RGB(255, 0, 0)
Else
r.Font.ColorIndex = 1
End If
End Sub
To help determine the best course of action, here is what the end results must be:
For any row that has a date entered into cell AD, the text color for the entire row should change to green. However, if cell E of any row contains a 6 (this is a number formatted as text), then the text in that row should be red.
I am sure that I am over thinking this. All suggestions are appreciated.
Use an And in your first If statement, and add an ElseIf statements.
I am not exactly sure what you want to take precedence if both a date and 6 exist or if there is one without the other, but you can easily adjust the If Then ElseIf block below to sort out your needs.
If r.Cells(1, "AD").Value <> "" And r.cells(1,"E").Value = "6" Then
r.Font.Color = RGB(255, 0, 0)
ElseIf r.Cells(1,"AD").Value <> "" Then
r.Font.Color = RGB(0, 176, 80)
Else
r.Font.ColorIndex = 1
End If
I am new to VBA...looking for code that will only allow me to enter a value in a column if the value in one or more of the three cells immediately to the left "contains" the word "Other". I've successfully written the code so that if the value in one or more of the cells is "Other" I am restricted from entering a value, but have not been successful in using ISERROR and FIND so that the code looks for text that includes "other". Here is what I have right now...
If Target.Column = 15 And Target <> "" Then
If Cells(Target.Row, Target.Column - 1).Value <> "Other" _
Or Cells(Target.Row, Target.Column - 2).Value <> "Other" _
Or Cells(Target.Row, Target.Column - 3).Value <> "Other" _
Then
Target.Value = ""
MsgBox "First Select 'Other' value in one or more of the 'Excluded Employee' Columns to the left"
Exit Sub
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub
Any suggestions would be most appreciated!
If Target.Column = 15 And Target <> "" Then
If InStr(1, Cells(Target.Row, Target.Column - 1).Value, "Other") = 0 _
And InStr(1, Cells(Target.Row, Target.Column - 2).Value, "Other") = 0 _
And InStr(1, Cells(Target.Row, Target.Column - 3).Value, "Other") = 0 _
Then
Target.Value = ""
MsgBox "First Select 'Other' value in one or more of the 'Excluded Employee' Columns to the left"
Exit Sub
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub
You can use COUNTIF with a wildcard to look for at least once cell containing other, i.e.:
If target.Column = 15 And target.Value <> "" Then
If Application.WorksheetFunction.CountIf(target.Offset(0, -3).Resize(1, 3), "*other*") = 0 Then
target.Value = ""
MsgBox "First Select 'Other' value in one or more of the 'Excluded Employee' Columns to the left"
Exit Sub
End If
End If