I need to play around with weekend dates identification and color the value in col M :
I need to check for "weekend dates" in Col K of sheet "Latency" (starting from row 2)
If a weekend date is found then check in Col O for the text "fail". If that is found,
Check for either of these three text "Moved to SA (Compatibility Reduction)" or "Text 2" or "Text 3" in Col P.
If either of these text is found and if the number in Col M is >1 then color it in red.
I have the below code that checks only for Sunday. But I want this to run for weekend and add additional keywords to check.
Sub SundayCheck()
Dim r, LastRow, RemainingDay As Double
LastRow = Range("M:O").Cells(Rows.count, "A").End(xlUp).Row
Application.ScreenUpdating = False
For r = 2 To LastRow
RemainingDay = 0
If Weekday(Range("K" & r).Value, vbSunday) = 1 Then
RemainingDay = Round((24 - Format(TimeValue(Range("K" & r)), "h")) / 24, 1)
If InStr(1, Range("O" & r).Text, "Fail", vbTextCompare) > 0 Then
If Range("M" & r) - RemainingDay >= 1 Then
Range("M" & r).Cells.Font.ColorIndex = 3
Else
Range("M" & r).Cells.Font.ColorIndex = 0
End If
End If
End If
Next r
End Sub
with the minimum editing of your code
Option Explicit
Sub SundayCheck()
Dim r As Long, LastRow As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).row
Application.ScreenUpdating = False
For r = 2 To LastRow
If Weekday(Range("K" & r).Value, vbSunday) = 1 Or Weekday(Range("K" & r).Value, vbSunday) = 7 Then
If UCase(Range("O" & r).Text) = "FAIL" Then
Select Case True
Case InStr(Range("P" & r).Text, "Moved to SA (Compatibility Reduction)") > 0, _
InStr(Range("P" & r).Text, "Text2") > 0, _
InStr(Range("P" & r).Text, "Text3") > 0
If Range("M" & r) > 1 Then
Range("M" & r).Cells.Font.ColorIndex = 3
Else
Range("M" & r).Cells.Font.ColorIndex = 0
End If
End Select
End If
End If
Next r
End Sub
Where I took your condition 3 as a full match instead of a partial one
Related
Column E Column F
Start_Time End_Time
12:16:56 12:16:57
12:16:57 12:16:59
12:18:50 12:19:04
12:20:13 12:20:13
12:20:32 12:20:33
12:20:42 12:20:49
12:31:16 12:31:17
12:37:32 12:37:47
12:45:41 12:45:43
12:48:36 12:48:36
12:48:44 12:48:46
I want to do if the end time is equal to the start time, use the previous end time as the start time. For example, in the picture if E5=F5, then E5=F4. I used this common,it works but only in the specific cell. How can I apply it to the whole column? I tried If...then...statement as following, not works.Thanks.
For i = 2 To lastrow - 1
If Range("E3").Value = Range("F3").Value Then
Range("E3").Value = Range("F2").Value
End If
Next i
Expected output:
Column E Column F
Start_Time End_Time
12:16:56 12:16:57
12:16:57 12:16:59
12:18:50 12:19:04
12:19:04 12:20:13
12:20:32 12:20:33
12:20:42 12:20:49
12:31:16 12:31:17
12:37:32 12:37:47
12:45:41 12:45:43
12:45:43 12:48:36
12:48:44 12:48:46
For i = 2 To lastrow - 1
If Range("E" & i+1).Value = Range("F" & i+1).Value Then
Range("E" & i+1).Value = Range("F" & i).Value
End If
Next i
No Loop needed:
With Worksheets("Sheet1") ' Change to your sheet name
.Range("E3:E" & lastrow - 1).Value = _
.Evaluate("IF(E3:E" & lastrow - 1 & " = F3:F" & lastrow - 1 & ",F2:F" & _
lastrow - 2 & ",E3:E" & lastrow - 1 & ")")
End With
I have a code which performs different checks for 3 different columns. It works absolutely fine, but I want some alteration. Let’s see the code first.
Sub test()
On Error Resume Next
Dim cel As Range
Dim colCStr As String, colDStr As String, colEStr As String
Set ws = Sheets("Sheet1")
With ws
LastRow = .Cells(.Rows.count, "C").End(xlUp).row
For Each cel In .Range("C2:C" & LastRow)
'condition for Column C (cell not empty & characters in cell are alphabet)
For i = 1 To Len(cel)
If Not (Not IsEmpty(cel) And Asc(UCase(cel)) > 64 And Asc(UCase(cel)) < 91) Then
colCStr = colCStr & "," & cel.row
Exit For
End If
Next i
'condition for Column D (cell is numeric & length of cell value is 2 or 3)
If Not (IsNumeric(cel.Offset(0, 1)) And (Len(cel.Offset(0, 1)) = 2 Or Len(cel.Offset(0, 1)) = 3)) Then
colDStr = colDStr & "," & cel.Offset(0, 1).row
End If
'condition for Column E (cell is numeric & length of cell value is 7 or 8 or cell value is 0)
If Not (IsNumeric(cel.Offset(0, 2)) And (Len(cel.Offset(0, 2)) = 7 Or Len(cel.Offset(0, 2)) = 8) Or cel.Offset(0, 2) = 0) Then
colEStr = colEStr & "," & cel.Offset(0, 2).row
End If
Next cel
End With
'disply message box only if there's error
If Len(colCStr) > 0 Then
Sheets("Error_sheet").Range("A2" & row).Value = "Errors in Column C" & " : " & Mid(colCStr, 2, Len(colAStr))
If Len(colDStr) > 0 Then
Sheets("Error_sheet").Range("B2" & row).Value = "Errors in Column D" & " : " & Mid(colDStr, 2, Len(colDStr))
If Len(colEStr) > 0 Then
Sheets("Error_sheet").Range("C2" & row).Value = "Errors in Column E" & " : " & Mid(colEStr, 2, Len(colEStr))
Else
End If
End If
End Sub
The code performs following checks:
Column C: Cell not empty & characters in cell are alphabet (Actually I don’t want to perform any checks over here in Column C, but if I delete the lines of code which validate Column C the rest of code stops getting executed too).
Column D: Cell is numeric & length of cell value is 2 or 3 (I want the absolutely same checks).
Column E: Cell is numeric & length of cell value is 7 or 8 or cell value is 0 (I want the absolutely same checks).
I appreciate your time and efforts.
This version doesn't use Offset so it should be easier to update (and more efficient)
Option Explicit
Public Sub CheckColDandE()
Dim ws As Worksheet, lr As Long, arr As Variant, r As Long
Dim dOk As Boolean, eOk As Boolean, dErr As String, eErr As String
Set ws = ThisWorkbook.Worksheets("Sheet1")
lr = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
arr = ws.Range("D2:E" & lr)
For r = 1 To lr - 1
dOk = IsNumeric(arr(r, 1)) And arr(r, 1) > 9 And arr(r, 1) < 1000
eOk = IsNumeric(arr(r, 2))
eOk = eOk And (arr(r, 2) > 999999 And arr(r, 2) < 100000000 Or arr(r, 2) = 0)
If Not dOk Then dErr = dErr & r + 1 & ", "
If Not eOk Then eErr = eErr & r + 1 & ", "
Next
With ws.Range("D" & lr + 1 & ":E" & lr + 1)
.Value2 = vbNullString
If Len(dErr) > 0 Then .Cells(1) = "Rows with Errors: " & Left(dErr, Len(dErr) - 2)
If Len(eErr) > 0 Then .Cells(2) = "Rows with Errors: " & Left(eErr, Len(eErr) - 2)
End With
End Sub
Delete the following lines (and update your comments! The column names in comments and code ae not the same):
'condition for Column A (cell not empty & characters in cell are alphabet)
For i = 1 To Len(cel)
If Not (Not IsEmpty(cel) And Asc(UCase(cel)) > 64 And Asc(UCase(cel)) < 91) Then
colCStr = colCStr & "," & cel.row
Exit For
End If
Next i
I have dates along with time under Col K and certain values (numbers) corresponding to these days under Col M.
I have a code that changes the color of these values if they are greater than 1 and if they have a text "waiting" in col P.
What I don't know to do is, add the below condition into this code:
1.I want to identify if these days belongs to a Sunday.
2.If Yes, then I want to check if the Sunday hours (lets say the date/time format is "15/1/2016 17:00" so the remaining time left for Sunday to get over is 0.3 day) subtracted from the number in Col M and if still the number is >1, then it should be highlighted in "Red".
3.The subtraction should not affect or appear in the current sheet.
I tried the below code but I'm not sure where I'm making the mistake as there are no result.
Sub Datefilter()
Dim r As Long
Dim m As Long
On Error GoTo ExitHere:
m = Range("M:P").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Application.ScreenUpdating = False
For r = 1 To m
remainingDay = 0
If Weekday(Range("K" & r)) = 1 Then
remainingDay = Round((24 - Format(TimeValue(Range("K" & r)), "h")) / 24, 1)
End If
If Range("P" & r) = "*waiting*" Then
If Range("M" & r) - remainingDay >= 1 Then
Range("M" & r).Cells.Font.ColorIndex = 3
Else
Range("M" & r).Cells.Font.ColorIndex = 0
End If
End If
Next r
ExitHere:
Application.ScreenUpdating = True
End Sub
I feel this would be much easier with Excel's built-in functions and some helper columns.
(1) Use the WEEKDAY() function to get the day of the week. Then use a simple comparison to check if it is Sunday.
(2) Dates are stored as the amount of time expired since 0th January 1900, with partial dates as fractions. Therefore, to return the time, simply take the rounded bit of the date from the date: =A1-ROUNDDOWN(A1,0)
(3) Use conditional formatting to check if the cell is < 1 and then turn it red.
Let me know if you would like a screenshot of an example.
Try this:
Sub Datefilter()
Dim r, lastrow, remainingDay As Long
'On Error GoTo ExitHere: ' I recommend to delete this
lastrow = Range("M:P").Cells(Rows.Count, "A").End(xlUp).Row
Application.ScreenUpdating = False
For r = 1 To lastrow
remainingDay = 0
If Weekday(Range("K" & r).Value, vbSunday) = 1 Then
remainingDay = Round((24 - Format(TimeValue(Range("K" & r)), "h")) / 24, 1)
If InStr(1, Range("P" & r).Text, "waiting", vbTextCompare) > 0 Then
If Range("M" & r) - remainingDay >= 1 Then
Range("M" & r).Cells.Font.ColorIndex = 3
Else
Range("M" & r).Cells.Font.ColorIndex = 0
End If
End If
End If
Next r
'ExitHere: ' I recommend to delete this
Application.ScreenUpdating = True
End Sub
I am looking to search for different values (Commodity Group in range "FY", Sub Group in range "FZ", Product in Range "GA") on the same Worksheet based on 3 comboboxes - one for each of the items - and copy it to another Worksheet.
Note: It is not necessary to select all 3 comboboxes because Combobox2 is populated based on combobox1 and Combobox3 based on Combobox2. Moreover, the user Needs to be able to create a Portfolio based on Inputs from only 1 or 2 comboboxes. Also, if that makes a difference, the items in all 3 ranges on the Database-Worksheet may contain ( ) / , -
I cannot seem to get it working beyond the point that it looks for the item in the first Combobox.
Two pictures for Illustration-purposes:
http://imgur.com/a/FxeNh
http://imgur.com/a/KtqdU
Here my take on it - thank you all in advance:
Dim wb As Workbook
Set wb = ActiveWorkbook
Dim ws1, ws2, ws3 As Worksheet
Set ws1 = wb.Worksheets("Meta DB")
'ws2 not here
Set ws3 = wb.Worksheets("Supplier Criteria TreeView")
'1. - - get all Suppliers for the selected Input
Dim strFind As String
Dim strRange As String
Dim i, j, k As Long
'1.1. - - Get value to search for and range to go through (depending on combobox selections)
If Me.comboProduct.ListIndex = -1 And Me.comboSubGroup.ListIndex = -1 And Me.comboCG.ListIndex <> -1 Then
strRange = "FY"
strFind = Me.comboCG.value
ElseIf Me.comboProduct.ListIndex = -1 And Me.comboSubGroup.ListIndex <> -1 And Me.comboCG.ListIndex <> -1 Then
strRange = "FZ"
strFind = Me.comboSubGroup.value
ElseIf Me.comboProduct.ListIndex <> -1 And Me.comboSubGroup.ListIndex <> -1 And Me.comboCG.ListIndex <> -1 Then
strRange = "GA"
strFind = Me.comboProduct.value
End If
'Paste starting at row 2 or 30 in ws3, respectively (Active / Inactive)
j = 2
k = 30
'Start searching from row 4 of Database, continue to end of worksheet
For i = 4 To ws1.UsedRange.Rows.Count
If ws1.Range(strRange & i) = strFind Then
'Check for active Supplier
If ws1.Range("E" & i) = "Yes" Then
'Copy row i of Database to row j of ws3 then increment j
ws1.Range("B" & i & ":" & "E" & i).Copy Destination:=ws3.Range("B" & j & ":" & "E" & j) 'Copy Name, Potential Supplier, ID, Active
j = j + 1
Else
'If inactive Supplier, post further down from 30 onwards. Second listbox populates from there
If ws1.Range("E" & i) = "No" Then
ws1.Range("B" & i & ":" & "E" & i).Copy Destination:=ws3.Range("B" & k & ":" & "E" & k) 'Copy Name, Potential Supplier, ID, Active
k = k + 1
Else
Exit Sub
End If
End If
End If
Next i
Private Sub cmdPortfolio_Click()
Dim product As String, col As Variant
Dim rw As Long, x As Long
Dim c As Range, Target As Range
'1.0. - - Clear previously used range
Worksheets("Supplier Criteria TreeView").Range("A2:L28,A30:L100").Clear
'1.1. - - Get value to search for and range to go through (depending on combobox selections)
If Me.comboProduct.ListIndex = -1 And Me.comboSubGroup.ListIndex = -1 And Me.comboCG.ListIndex <> -1 Then
col = "FY"
product = Me.comboCG.Value
ElseIf Me.comboProduct.ListIndex = -1 And Me.comboSubGroup.ListIndex <> -1 And Me.comboCG.ListIndex <> -1 Then
col = "FZ"
product = Me.comboSubGroup.Value
ElseIf Me.comboProduct.ListIndex <> -1 And Me.comboSubGroup.ListIndex <> -1 And Me.comboCG.ListIndex <> -1 Then
col = "GA"
product = Me.comboProduct.Value
End If
With Worksheets("Meta DB")
For x = 4 To .Cells(Rows.Count, col).End(xlUp).row
If .Cells(x, col) = product Then
rw = IIf(.Range("E" & x) = "Yes", 29, Rows.Count)
Set Target = Worksheets("Supplier Criteria TreeView").Cells(rw, "B").End(xlUp).Offset(1)
.Range("B" & x & ":" & "E" & x).Copy Destination:=Target
With Target.EntireRow
Set c = Worksheets("Criteria").Range("K3", Worksheets("Criteria").Range("K" & Rows.Count).End(xlUp)).Find(.Cells(1, "D"))
If Not c Is Nothing Then
.Cells(1, "A") = Round(c.EntireRow.Cells(1, "L"))
.Cells(1, "F") = Round(c.EntireRow.Cells(1, "Q"))
.Cells(1, "G") = Round(c.EntireRow.Cells(1, "AG"))
End If
End With
End If
Next
End With
End Sub
How do i add dashes(-) til my cell value = 5, If my length character is not equal to five and i have a 4 character, for ex A B... what i want it to do if i have cell value less then 5 then i want it to replace with dashes(-) till my cell length value reach to 5 character. Here is my Code and image... IMAGE will make more sense.. let me know if there is any confusion.
Sub xn()
Dim x As Integer, lastrow As Long, a As Long, i As Long
Dim xcell As String
a = 1
lastrow = Worksheets("Sheet2").UsedRange.Rows.Count + 1
For i = a To lastrow
xcell = Worksheets("Sheet2").Range("A" & i).Value
Do Until Len(xcell) = 5
If Len(xcell) <> 5 Then
Worksheets("Sheet2").Range("C" & i) = Replace(xcell, " ", "_")
Else
Exit Do
End If
Loop
Next i
End Sub
try this
Sub test()
Dim lastrow&, i&, xcell$, z%
lastrow = Sheet2.Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To lastrow
xcell = Replace(Sheet2.Range("A" & i).Value, " ", "")
If Len(xcell) < 5 And xcell <> "" Then
z = 5 - Len(xcell)
Sheet2.Cells(i, "C").Value = Left(xcell, Len(xcell) - 1) & _
WorksheetFunction.Rept("-", z) & Right(xcell, 1)
Else
Sheet2.Cells(i, "C").Value = xcell
End If
Next i
End Sub
output
This line isn't going to do anything unless there's already spaces padding the end of the string:
Worksheets("Sheet2").Range("C" & i) = Replace(xcell, " ", "_")
You need to check the length, if it's less than five, add 5 - length characters to the end of it:
Sub xn()
Dim lastrow As Long
Dim i As Long
Dim xcell As String
lastrow = Worksheets("Sheet2").UsedRange.Rows.Count + 1
For i = 1 To lastrow
xcell = Worksheets("Sheet2").Range("A" & i).Value
If Len(xcell) < 5 Then
Worksheets("Sheet2").Range("C" & i) = xcell & String$(Len(xcell) - 5, "_")
End If
Next i
End Sub
You can also leave out the variable 'a' - it's basically a constant in the code you posted.