I'm trying to create an IF statement that does the following:
highlights (with red color) anything with a value greater than 1 and less than 26, and then continue with the rest of the macro and do other things (which I've successfully done).
if there's a value over 25, then highlight with red, produce a messagebox, and exitsub (which I've successfully done).
if ALL rows are = 1, then do nothing and exit sub (which i'm struggling with).
For Each C In Range("B2:B25000").Cells
If C.Value > 1 And C.Value < 26 Then
firstValue = C.Value
firstAddress = C.Address
Exit For
If Not (C.Value > 1 And C.Value < 26) Then Exit Sub 'No
ElseIf C.Value > 25 Then
C.Interior.Color = VBA.ColorConstants.vbRed
MsgBox "Too big!"
Exit Sub
End If
Next
C.Interior.Color = VBA.ColorConstants.vbRed 'if greater than 1 & less than 26 then Color = red
'remaining of the macro goes here
End Sub
Use the if statements to set logic flags and then decide whether to exit sub or continue.
Option Explicit
Sub test()
Dim ws As Worksheet, c As Range, lastrow As Long
Dim bAllOnes As Boolean, bTooBig As Boolean
Set ws = Sheet1
bAllOnes = True
bTooBig = False
lastrow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
For Each c In ws.Range("B2:B" & lastrow).Cells
If Val(c.Value) > 1 Then
bAllOnes = False
c.Interior.Color = VBA.ColorConstants.vbRed
If c.Value > 25 Then
bTooBig = True
End If
ElseIf Val(c.Value) < 1 Then
bAllOnes = False
End If
Next
If bTooBig Then
MsgBox "Too big!", vbCritical
Exit Sub
ElseIf bAllOnes Then
MsgBox "All 1's!", vbCritical
Exit Sub
Else
MsgBox "Continueing"
End If
End Sub
I replaced this
If Not (C.Value > 1 And C.Value < 26) Then Exit Sub
with this ElseIf Application.WorksheetFunction.max(Range("b:b")) = 1 Then Exit Sub
and it worked perfect
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
How can my code be made shorter?
If a user fills the cell with color yellow then if its value is 0 then it will turn to red and it will popup a message box, then if its value is > 0 it will back again to yellow, then if the user enters value of > 0 in the "no fill up cell" it will turn grey and back to no fill up if I input 0 this code is for column L only I need to make this for column M, N and O also.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet
Set ws = ActiveSheet
On Error GoTo ExitSub
'WEEK 0
'For Task Not done
With ws.Cells(15, 12)
If Not (Application.Intersect(Range("L15"), Target) Is Nothing) Then
If .Interior.ColorIndex = 6 And .Value < 1 Then
MsgBox "Project Delay!"
Range("L15").Interior.ColorIndex = 3
Else
If Range("L15").Interior.ColorIndex = 3 And .Value > 0 Then
Range("L15").Interior.ColorIndex = 6
End If
End If
'For overlapped Task
If .Interior.ColorIndex = -4142 And .Value > 0 Then
MsgBox "Overlap!"
Range("L15").Interior.ColorIndex = 16
Else
If Range("L15").Interior.ColorIndex = 16 And .Value < 1 Then
Range("L15").Interior.ColorIndex = -4142
End If
End If
End If
End With
On Error GoTo ExitSub
'For Task Not done
With ws.Cells(17, 12)
If Not (Application.Intersect(Range("L17"), Target) Is Nothing) Then
If .Interior.ColorIndex = 6 And .Value < 1 Then
MsgBox "Project Delay!"
Range("L17").Interior.ColorIndex = 3
Else
If Range("L17").Interior.ColorIndex = 3 And .Value > 0 Then
Range("L17").Interior.ColorIndex = 6
End If
End If
'For overlapped Task
If .Interior.ColorIndex = -4142 And .Value > 0 Then
MsgBox "Overlap!"
Range("L17").Interior.ColorIndex = 16
Else
If Range("L17").Interior.ColorIndex = 16 And .Value < 1 Then
Range("L17").Interior.ColorIndex = -4142
End If
End If
End If
End With
End Sub
Please try this code. As far as I understood your intentions it should do what you want.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Tmp As Long
With Target
If .Cells.CountLarge > 1 Then Exit Sub
If (.Column >= Columns("L").Column) And (.Column <= .Columns("O").Column) Then
Tmp = Val(.Value)
Select Case .Row
Case 15
.Interior.ColorIndex = IIf(Tmp, 6, 3)
If Tmp = 0 Then
MsgBox "Project Delay!", _
vbCritical, "Attention required!"
End If
Case 17
.Interior.ColorIndex = IIf(Tmp, 16, -4142)
If Tmp Then
MsgBox "Enter a value of zero.", _
vbExclamation, "Overlap!"
End If
End Select
End If
End With
End Sub
I have kept the syntax simple so that you ought to be able to tweak it where it needs tweaking. Good luck!
I am struggling to get a Worksheet_Change event to work with the goal of checking if there is a % difference greater than 10% between range G12:42 and range J12:42. I have a calculation in range G12:42, which seems to be causing me some of the headache.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim diffPercent
'Check that the data is changed between row 12 and 42 and it is even row. eg 12,14,16...42.
If (Target.Row > 12 And Target.Row < 42) And ((Target.Row Mod 2) = 0) Then 'And _
'(Target.Column = 7 Or Target.Column = 10) Then
'Get the values in J ang G columns of that particular row.
number1 = Range("G" & Target.Row).Value
number2 = Range("J" & Target.Row).Value
'Check for presence of both the inputs to calculate difference in percentage.
If Not chkInputs(number1, number2) Then
Exit Sub
End If
'Calculate the percentage difference.
diff = number2 - number1
diffPercent = (diff / number2) * 100
'Give alert if difference more than 10 percent
If diffPercent > 10 Then
MsgBox "Oppps. Your system is not working!"
End If
End If
End Sub
Function chkInputs(number1, number2)
chkInputs = False
If IsNumeric(number1) And IsNumeric(number2) Then
chkInputs = True
End If
End Function
The expected result is the triggering of a MsgBox providing a message.
No need to have a separate funciton. You can include it in the main code. Also use Intersect to work with the relevant range else the code will trigger if there is a change anywhere in that row range. One more thing. Check if the cell in column J is not 0 else you will get an Overflow error.
You may also want to see Working with Worksheet_Change
Is this what you are trying (Untested)?
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngToCheck As Range
Dim NumA As Variant, NumB As Variant
Dim i As Long
On Error GoTo Whoa
'~~> Set the relevant range
Set rngToCheck = Union(Range("G12:G42"), Range("J12:J42"))
Application.EnableEvents = False
If Not Intersect(Target, rngToCheck) Is Nothing Then
For i = 12 To 42 Step 2 '<~~ Loop through only even rows
NumA = Range("G" & i).Value
NumB = Range("J" & i).Value
If IsNumeric(NumA) And IsNumeric(NumB) And NumB <> 0 Then
If ((NumA - NumB) / NumB) * 100 > 10 Then
MsgBox "Please check the value of Col G and J Cells in row " & i
Exit For
End If
End If
Next i
End If
Letscontinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub
I am trying to put the value of the cell that is after the one with the string "Senast", into cell Q2. The code below does not work. Any ideas why?
Sub Find_Data()
Dim item_in_review As Variant
Dim row_number As Long
For row_number = 1 To 1000 Step 1
item_in_review = Sheets("Investor_Importerad data").Range("A" & row_number)
If InStr(item_in_review, "Senast") Then
row_number = row_number + 1
Worksheets("Översikt innehavCells").Cells(2, "Q").Value = Worksheets("Investor_Importerad data").Cells(row_number, "A").Value
Exit For
End If
If item_in_review = 300 Then
MsgBox "300"
Exit For
End If
Next row_number
End Sub
It seems that you are looking to either a wildcard match to Senast or the number 300 as a true number. Whichever comes first, you exit your For ... Next loop.
The following is filled with numerous error trapping and criteria conditions that should isolate your problem.
Sub Find_Data()
Dim rSenast As Variant, r300 As Variant, wsn As String
wsn = "Översikt innehavCells"
Dim row_number As Long
With Worksheets("Investor_Importerad data")
rSenast = Application.Match("*senast*", .Range("a:a"), 0)
r300 = Application.Match(300, .Range("a:a"), 0)
If IsError(r300) Then _
r300 = Application.Match(Format(300, "0"), .Range("a:a"), 0)
Select Case True
Case CBool(IsNumeric(rSenast) And IsNumeric(r300))
If r300 < rSenast Then
If MsgBox("300 found before 'senast" & vbLf & "Continue with value transfer?", vbYesNo, "Continue?") <> vbYes Then _
Exit Sub
End If
Case CBool(IsError(rSenast) And IsError(r300))
MsgBox "Neither 'senast' nor 300 can be found"
Exit Sub
Case CBool(IsError(rSenast) And IsNumeric(r300))
MsgBox "300 can be found at row " & r300
Exit Sub
End Select
On Error GoTo No_Such_Worksheet
If .Cells(rSenast + 1, "A").Value <> vbNullString Then
Worksheets(wsn).Cells(2, "Q") = .Cells(rSenast + 1, "A").Value
Else
MsgBox .Cells(rSenast + 1, "A").Address(0, 0) & " appears to be empty"
End If
On Error GoTo 0
End With
Exit Sub
No_Such_Worksheet:
With Worksheets.Add(after:=Worksheets("Investor_Importerad data"))
.Name = wsn
End With
Resume
End Sub
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