I am currently working on a multiple if, I would like to avoid writing 100 "OR" in my code and therefore I have a list of criterias (AO450 to AO515) and I would like to have something like this : If the cells(i, 33) like one of the criterias, then "comment", your help would be very welcomed :)
Sub MultipleIf()
For i = 3 To 467
If Cells(i, 19).Value > 5000 Then
If Cells(i, 33).Value Like "*" & "Cells(AO450:AO515).Value" & "*" Then
Cells(i, 35).Value = "commentA"
ElseIf Cells(i, 19).Value > 0 And Cells(i, 33).Value Like "*" & "ABC" & "*" Then
Cells(i, 35).Value = "commentB"
ElseIf Cells(i, 19).Value > 0 And Cells(i, 33).Value Like "*" & "DEF" & "*" Then
Cells(i, 35).Value = "commentC"
Else
Cells(i, 35).Value = "commentD"
End If
ElseIf Cells(i, 19).Value < -5000 Then
If Cells(i, 33).Value Like "*" & "Cells(AO450:AO515).Value" & "*" Then
Cells(i, 35).Value = "commentAA"
ElseIf Cells(i, 19).Value < 0 And Cells(i, 33).Value Like "*" & "ABC" & "*" Then
Cells(i, 35).Value = "commentBB"
ElseIf Cells(i, 19).Value < 0 And Cells(i, 33).Value Like "*" & "DEF" & "*" Then
Cells(i, 35).Value = "commentC"
Else
Cells(i, 35).Value = "commentD"
End If
Else:
If Cells(i, 19).Value < 0 And Cells(i, 33).Value Like "*" & "GHI" & "*" Then
Cells(i, 35).Value = "commentE"
Cells(i, 36).Value = "commentF"
Else
Cells(i, 35).Value = "commentD"
End If
End If
Next
End Sub
EDIT - updated to add full (reworked) code
If you want to test whether a value is found in a single-column range then you can do something like this:
Sub MultipleIf()
'come up with some better variable names than `v19` and `v33`...
Dim ws As Worksheet, i As Long, rw As Range, v19, v33 As String, rngList As Range, txt
Set ws = ActiveSheet
Set rngList = ws.Range("AO450:AO515")
For i = 3 To 467
Set rw = ws.Rows(i)
v19 = rw.Cells(19).Value
v33 = rw.Cells(33).Value
txt = ""
If v19 > 5000 Then
Select Case True 'https://stackoverflow.com/questions/794036/select-case-true
Case ColumnContains(rngList, v33): txt = "commentA"
Case v33 Like "*ABC*": txt = "commentB"
Case v33 Like "*DEF*": txt = "commentC"
Case Else: txt = "commentD"
End Select
ElseIf v19 < -5000 Then
Select Case True
Case ColumnContains(rngList, v33): txt = "commentAA"
Case v33 Like "*ABC*": txt = "commentBB"
Case v33 Like "*DEF*": txt = "commentC"
Case Else: txt = "commentD"
End Select
Else
If v19 < 0 And v33 Like "*GHI*" Then
txt = "commentE"
rw.Cells(36).Value = "commentF"
Else
txt = "commentD"
End If
End If
If Len(txt) > 0 Then rw.Cells(35).Value = txt
Next
End Sub
'does a (single-column) range `rng` contain the string `txt`?
Function ColumnContains(rng As Range, txt As String) As Boolean
ColumnContains = Not IsError(Application.Match("*" & txt & "*", rng, 0))
End Function
Related
I have a program that opens an Excel spreadsheet and makes changes to it. I am always editing the first sheet but if it is a certain type of report I want to edit the second sheet as well. This all works fine for me on the first sheet and all but centering the text in the cell on the second sheet. I get an Object Required error only when I try to do this. I center the text in the cells on the first sheet no problem. The error only comes after I pass the object to the new procedure. Here is some of my code:
First Procedure
Private Sub OSummary1(strfile As String, strTableResults As String, dtUnivDt As Date)
Dim xlApp As Object
Dim objWorkbook As Object
Dim objSheet As Object
Set xlApp = CreateObject("Excel.Application")
Set objWorkbook = xlApp.Workbooks.Open(strfile)
Set objSheet = objWorkbook.Sheets(1)
Later in the code
ElseIf Mid(strTableResults, 11, 1) = 1 Then
Max = 11
Do Until i > Max
If .Cells(i, 4).Value = "0" And .Cells(i, 2).Value = "0" And .Cells(i, 3).Value = "0" Then
.Cells(i, 4).Value = "NA"
.Cells(i, 4).Interior.ColorIndex = 15
.Cells(i, 3).Value = "-"
.Cells(i, 2).Value = "-"
ElseIf .Cells(i, 2).Value = "0" Then
.Cells(i, 4).Value = "0.0"
.Cells(i, 4).Interior.ColorIndex = 22
ElseIf .Cells(i, 4).Value >= "95.00" Or .Cells(i, 4).Value = "100" Then
.Cells(i, 4).Interior.ColorIndex = 43
ElseIf .Cells(i, 4).Value >= "90.00" And .Cells(i, 4).Value < "95.00" Then
.Cells(i, 4).Interior.ColorIndex = 36
Else
.Cells(i, 4).Interior.ColorIndex = 22
End If
If .Cells(i, 4).Value = 0 Then
.Cells(i, 4).NumberFormat = "0.00%"
ElseIf Not .Cells(i, 4).Value Like "*.*" Then
.Cells(i, 4).NumberFormat = "#.00""%"""
ElseIf .Cells(i, 4).Value Like "*.#" Then
.Cells(i, 4).NumberFormat = "#.#0""%"""
Else
.Cells(i, 4).NumberFormat = "#.##""%"""
End If
If .Cells(i, 1).Value = "AppealNotificationTimeliness" Then
.Cells(i, 1).Font.Bold = True
.Cells(i, 2).Font.Bold = True
.Cells(i, 3).Font.Bold = True
.Cells(i, 4).Font.Bold = True
.Cells(i, 2).HorizontalAlignment = xlCenter
.Cells(i, 3).HorizontalAlignment = xlCenter
.Cells(i, 4).HorizontalAlignment = xlCenter
iB = Len(.Cells(i, 2).Value)
iC = Len(.Cells(i, 3).Value)
iD = .Cells(i, 4).Value
Else
'Indent header
.Cells(i, 1).IndentLevel = 3
'Indent sub-headers
If iB < 3 Then
.Cells(i, 2).IndentLevel = 5
ElseIf iB > 2 And iB < 5 Then
.Cells(i, 2).IndentLevel = 4
ElseIf iB > 4 And iB < 7 Then
.Cells(i, 2).IndentLevel = 3
Else
.Cells(i, 2).IndentLevel = 2
End If
If iC < 3 Then
.Cells(i, 3).IndentLevel = 4
ElseIf iC > 2 And iC < 5 Then
.Cells(i, 3).IndentLevel = 3
ElseIf iC > 4 And iC < 7 Then
.Cells(i, 3).IndentLevel = 2
Else
.Cells(i, 3).IndentLevel = 1
End If
If iD = "NA" Then
.Cells(i, 4).IndentLevel = 5
ElseIf iD = "100" Then
.Cells(i, 4).IndentLevel = 3
Else
.Cells(i, 4).IndentLevel = 4
End If
End If
i = i + 1
Loop
If Right(strTableResults, 3) = "FDR" Then
Call FDRTable1(objWorkbook)
End If
This all works fine for sheet 1
Second Procedure from Call above
Private Sub FDRTable1(ByRef objWorkbook As Object)
Dim objSheet As Object
Dim RowCnt As Integer
Dim CurrentRow As Integer
Dim CurrentRowVal As String
Dim iRange As Range
Dim iCells As Range
Dim i As Integer
Dim Max As Integer
Set objSheet = objWorkbook.Sheets(2)
i = 2
With objSheet
'Header
.Cells(1, 1).Font.Size = 12
.Range("A1:G1").Font.Bold = True
.Cells.EntireColumn.AutoFit
.Range("A2:G6").BorderAround LineStyle:=xlContinuous, Weight:=xlMedium
.Range("A7:G9").BorderAround LineStyle:=xlContinuous, Weight:=xlMedium
.Range("A10:G14").BorderAround LineStyle:=xlContinuous, Weight:=xlMedium
.Range("A15:G17").BorderAround LineStyle:=xlContinuous, Weight:=xlMedium
Max = 17
Do Until i > Max
If .Cells(i, 7).Value = "0" And .Cells(i, 4).Value = "0" And .Cells(i, 6).Value = "0" Then
.Cells(i, 7).Value = "NA"
.Cells(i, 7).Interior.ColorIndex = 15
.Cells(i, 4).Value = "-"
.Cells(i, 5).Value = "-"
.Cells(i, 6).Value = "-"
ElseIf .Cells(i, 4).Value = "0" Then
.Cells(i, 7).Value = "0.0"
.Cells(i, 7).Interior.ColorIndex = 22
ElseIf .Cells(i, 7).Value >= "95.00" Or .Cells(i, 7).Value = "100" Then
.Cells(i, 7).Interior.ColorIndex = 43
ElseIf .Cells(i, 7).Value >= "90.00" And .Cells(i, 7).Value < "95.00" Then
.Cells(i, 7).Interior.ColorIndex = 36
Else
.Cells(i, 7).Interior.ColorIndex = 22
End If
If .Cells(i, 7).Value = 0 Then
.Cells(i, 7).NumberFormat = "0.00%"
.Cells(i, 4).HorizontalAlignment.xlCenter
.Cells(i, 5).HorizontalAlignment.xlCenter
.Cells(i, 6).HorizontalAlignment.xlCenter
.Cells(i, 7).HorizontalAlignment.xlCenter
ElseIf Not .Cells(i, 7).Value Like "*.*" Then
.Cells(i, 7).NumberFormat = "#.00""%"""
.Cells(i, 4).HorizontalAlignment.xlCenter
.Cells(i, 5).HorizontalAlignment.xlCenter
.Cells(i, 6).HorizontalAlignment.xlCenter
.Cells(i, 7).HorizontalAlignment.xlCenter
ElseIf .Cells(i, 7).Value Like "*.#" Then
.Cells(i, 7).NumberFormat = "#.#0""%"""
.Cells(i, 4).HorizontalAlignment.xlCenter
.Cells(i, 5).HorizontalAlignment.xlCenter
.Cells(i, 6).HorizontalAlignment.xlCenter
.Cells(i, 7).HorizontalAlignment.xlCenter
Else
.Cells(i, 7).NumberFormat = "#.##""%"""
.Cells(i, 4).HorizontalAlignment.xlCenter
.Cells(i, 5).HorizontalAlignment.xlCenter
.Cells(i, 6).HorizontalAlignment.xlCenter
.Cells(i, 7).HorizontalAlignment.xlCenter
End If
i = i + 1
Loop
End With
End Sub
This all works too except I get the error when I try to center the text (.HorizontalAlignment.xlCenter). If I remove those lines, it works fine.
I have code which is used for concatenation. The cells are dynamic; whenever a change in cells in a range the concatenation function will automatically execute and gives the value. Currently I asked the concatenation function which has to run for the complete range even though the modification is in a single row. Which is causing a lot of time during the execution.
Is there is any way to define to update only a single row that is modified? I know the technique if the range is single column, for multiple columns I didn't have any idea.
My Code
ColumnLetter3 = Split(Cells(1, c1_column).Address, "$")(1)
ColumnLetter4 = Split(Cells(1, c6_column).Address, "$")(1)
Range3 = ColumnLetter3 & st_workrow2 + 1 & ":" & ColumnLetter4 & last_cell1
Set xrng3 = Range(Range3)
If Not Application.Intersect(xrng3, Range(Target.Address)) _
Is Nothing Then
For i = c_row + 1 To last_cell1
If Cells(i, c1_column) = "" And Cells(i, c2_column) = "" And Cells(i, c3_column) = "" And Cells(i, c4_column) = "" And Cells(i, c5_column) = "" And Cells(i, c6_column) = "" Then
Cells(i, c_column) = ""
Else
Cells(i, c_column) = Cells(i, c1_column) & "-" & Cells(i, c2_column) & "-" & Cells(i, c3_column) & "-" & Cells(i, c4_column) & "-" & Cells(i, c5_column) & "-" & Cells(i, c6_column)
Cells(i, c_column).Replace what:="+", Replacement:=""
Cells(i, c_column).Replace what:="-----", Replacement:="-"
Cells(i, c_column).Replace what:="----", Replacement:="-"
Cells(i, c_column).Replace what:="---", Replacement:="-"
Cells(i, c_column).Replace what:="--", Replacement:="-"
If Right(Cells(i, c_column), 1) = "-" Then
l = Len(Cells(i, c_column))
Cells(i, c_column) = Left(Cells(i, c_column), l - 1)
End If
If Left(Cells(i, c_column), 1) = "-" Then
l = Len(Cells(i, c_column))
Cells(i, c_column) = Right(Cells(i, c_column), l - 1)
End If
End If
Next I
Endif
It's hard to tell exactly what you're doing here (perhaps strip down your question?), however looks like you want to get a list of the rows in your target? In that case you can isolate it using Columns(1). See below...
If Not Application.Intersect(xrng3, Range(Target.Address)) _
Is Nothing Then
Dim aCell As Range
For Each aCell In Target.Columns(1).Cells
i = aCell.Row
If Cells(i, c1_column) = "" And Cells(i, c2_column) = "" And Cells(i, c3_column) = "" And Cells(i, c4_column) = "" And Cells(i, c5_column) = "" And Cells(i, c6_column) = "" Then
'skipped....
End If
Next aCell
End If
I have the following code
Dim i As Integer
For i = 3 To 10
If Range("H3").Value = Cells(i, 2).Value And Range("I3").Value < Cells(i, 4).Value And _
Range("I3").Value >= Cells(i, 3).Value Then
Range("J3").Value = Cells(i, 5).Value
End If
Next i
I want the value of J3 to represent the sum of all the iterations and not just the last iteration if i. Can it be done?
While there are certainly better methods of adding up cells, for your particular method this should work.
Dim i As long, lTotal as long
For i = 3 To 10
If Range("H3").Value = Cells(i, 2).Value And Range("I3").Value < Cells(i, 4).Value And _
Range("I3").Value >= Cells(i, 3).Value Then
lTotal = Cells(i, 5).Value + lTotal
End If
Next i
Range("J3").Value = lTotal
Keep a running total of of your loop, then use the running total as your cell's value after you've finished the loop
Change this line
Range("J3").Value = Cells(i, 5).Value
To:
Range("J3").Value = Range("J3").Value + Cells(i, 5).Value
I have a workbook with monthly worksheets. One for Emails and one for Calls and I have created two userForms for data entry, one for Emails and one for Calls.
The forms do the job and they enter date in the right place but if I have selected the "August 18 Email" sheet and use the Email form, once the form is submitted it jumps to display the "August 18 Calls" sheet.
I just want it to stay in the selected worksheet, in this case "August 18 Email".
The code for the Emails form is the one below and the one for the Calls is nearly the same but only changing this line : Set ws = Sheets(Format(Date, "mmmm yy") & " calls")
Private Sub CommandButton2_Click()
Dim lRow As Long
Dim ws As Worksheet
Set ws = ActiveSheet
Set ws = Sheets(Format(Date, "mmmm yy") & " emails")
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
If Me.txtDateBox.Value = "" Then
.Cells(lRow, 1).Value = Format(Date, "dd/mmm/yy")
Else
.Cells(lRow, 1).Value = Me.txtDateBox.Value
End If
myVar = ""
For x = 0 To Me.ListBox2.ListCount - 1
If Me.ListBox2.Selected(x) Then
If myVar = "" Then
myVar = Me.ListBox2.List(x, 0)
Else
myVar = myVar & "," & Me.ListBox2.List(x, 0)
End If
End If
Next x
.Cells(lRow, 11).Value = myVar
myVarSign = ""
For x = 0 To Me.ListBox3.ListCount - 1
If Me.ListBox3.Selected(x) Then
If myVarSign = "" Then
myVarSign = Me.ListBox3.List(x, 0)
Else
myVarSign = myVarSign & "," & Me.ListBox3.List(x, 0)
End If
End If
Next x
.Cells(lRow, 12).Value = myVarSign
myVarTheme = ""
For x = 0 To Me.ListBox1.ListCount - 1
If Me.ListBox1.Selected(x) Then
If myVarTheme = "" Then
myVarTheme = Me.ListBox1.List(x, 0)
Else
myVarTheme = myVarTheme & "," & Me.ListBox1.List(x, 0)
End If
End If
Next x
.Cells(lRow, 14).Value = myVarTheme
.Cells(lRow, 2).Value = Me.Time.Value
.Cells(lRow, 3).Value = Me.ComboBox1.Value
.Cells(lRow, 4).Value = Me.ComboBox2.Value
.Cells(lRow, 5).Value = Me.ComboBox3.Value
.Cells(lRow, 6).Value = Me.ComboBox4.Value
.Cells(lRow, 7).Value = Me.ComboBox5.Value
.Cells(lRow, 8).Value = Me.ComboBox15.Value
.Cells(lRow, 9).Value = Me.ComboBox6.Value
.Cells(lRow, 10).Value = Me.ComboBox7.Value
.Cells(lRow, 13).Value = Me.ComboBox11.Value
.Cells(lRow, 15).Value = Me.ComboBox16.Value
.Cells(lRow, 16).Value = Me.TextBox2.Value
End With
Me.txtDateBox.Value = ""
Me.Time.Value = ""
Me.ComboBox1.Value = ""
Me.ComboBox2.Value = ""
Me.ComboBox3.Value = ""
Me.ComboBox4.Value = ""
Me.ComboBox5.Value = ""
Me.ComboBox6.Value = ""
Me.ComboBox7.Value = ""
Me.ComboBox11.Value = ""
Me.ComboBox16.Value = ""
Me.ComboBox15.Value = ""
Me.TextBox2.Value = ""
Dim iCount As Integer
For iCount = 0 To Me!ListBox1.ListCount
Me!ListBox1.Selected(iCount) = False
Next iCount
For iCount = 0 To Me!ListBox2.ListCount
Me!ListBox2.Selected(iCount) = False
Next iCount
For iCount = 0 To Me!ListBox3.ListCount
Me!ListBox3.Selected(iCount) = False
Next iCount
End Sub
It could be improved a lot but I am happy if after submission the worksheet in view stays instead to jumping to another one.
As you can see I am only beginning (I have managed to create this with help of others).
If you remove any instances of .Select or .Activate on worksheet, range, or cell objects, your sheet shouldn't change.
If that is not an option, another solution would be to note what sheet you are on when the code is called and then Activate that sheet before ending your sub. Since we do not see all of the userform code, you will have to strategically decide where this goes (as mentioned by #K.Davis, nothing shown switches the sheet so it must be happening in some other code).
When the macro/userform is launched:
Dim StartSheet as Worksheet
Set StartSheet = ActiveSheet
Then, before exiting macro/userform:
StartSheet.Activate
You may have to pass this along as a parameter depending on how your code is structured.
i have VBA code that check entered dates with the current date and fill the cell in the appropriate color and check if the colomn "F" is not empty it will color the D,E,F columns.
the problem is that i have until now 21 records but the system just color 19 record even so the 2 rows are not empty in the F column.
code:
Private Sub CommandButton1_Click()
Dim i As Long
For i = Range("C5000").End(xlUp).Row To 2 Step -1 'Range upto 5000, chnge this as per your requirment'
If IsEmpty(Cells(i, 3)) Then
Cells(i, 3).Interior.Color = xlNone
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) < 0 Then
Cells(i, 3).Interior.Color = vbGreen
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) = 0 Then
Cells(i, 3).Interior.Color = vbYellow
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 1 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 4 Then
Cells(i, 3).Interior.Color = vbRed
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 5 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 10 Then
Cells(i, 3).Interior.Color = vbCyan
Else
Cells(i, 3).Interior.ColorIndex = xlNone
End If
' your 2nd criteria to color the entire row if "F" is not empty
If Trim(Range("F" & i).Value) <> "" Then Range("D" & i & ":F" & i).Interior.ColorIndex = 15
Next
End Sub
The ElseIf statements will throw Runtime Error 13 if the cells have a non-date value in them. This is caused by trying to convert a non-date value into a date VBA.CDate(Cells(i, 3))
Private Sub CommandButton1_Click()
Dim i As Long
With Worksheets("Sheet1")
For i = Range("C" & .Rows.Count).End(xlUp).Row To 2 Step -1 'Range upto 5000, chnge this as per your requirment'
If IsDate(Cells(i, 3)) Then
Select Case VBA.CDate(.Cells(i, 3)) - VBA.Date()
Case Is < 0
.Cells(i, 3).Interior.Color = vbGreen
Case Is = 0
.Cells(i, 3).Interior.Color = vbYellow
Case Is <= 4
.Cells(i, 3).Interior.Color = vbRed
Case Is <= 10
.Cells(i, 3).Interior.Color = vbCyan
Case Else
.Cells(i, 3).Interior.ColorIndex = xlNone
End Select
Else
.Cells(i, 3).Interior.ColorIndex = xlNone
End If
' your 2nd criteria to color the entire row if "F" is not empty
If Trim(.Range("F" & i).Value) <> "" Then .Range("D" & i & ":F" & i).Interior.ColorIndex = 15
Next
End With
End Sub
Might be something with your data, it runs properly to me. What kind of data do you have in the F column?