希望您一切都好。我需要一些帮助,首先,我不是程序员,这是我第一次使用VBA和宏。
我需要执行以下操作的代码。
首先在工作表(如果可能的话)上搜索特定变量,然后在L列上添加“月经费用”或“年费”,并相应地标注颜色。
我尝试了循环,Ifs和Sub过程,最后一种方法是发出租约问题的方法,但仍然没有。老实说,根本无法使添加文本部分正常工作。
我知道这一定是我很想念的事情,但是我迷路了,VBA逻辑正在杀死我。
Sub Set_Variables()
'Defined variables and range.
' Set Variables . There are about 95 in total
Dim Var01 As String: Mensual = "Var01"
Dim Var02 As String: Anual = "Var02"
Dim Var03 As String: PorDemanda = "Var03"
Dim Var04 As String: Cliente = "Var04"
Dim Var05 As String: Internet= "Var05"
Dim Var06 As String: Personal = "Var06"
Dim Var07 As String: Taxis-Glovo = "Var07"
Dim Var08 As String: Taxis-GoPato= "Var08"
Dim Var09 As String: SP = "Var09"
Dim Var10 As String: Taxis-Uber = "Var10"
Dim Var11 As String: Taxis-Uber-Eats= "Var11"
Var01 = "Mensual"
Var02 = "Anual"
Var03 = "PorDemanda"
Var04 = "Cliente"
Var05 = "Internet"
Var06 = "Personal"
Var07 = "Taxis-Glovo"
Var08 = "Taxis-GoPato"
Var09 = "SP"
Var10 = "Taxis-Uber"
Var11 = "Taxis-Uber-Eats"
End Sub
Sub ChangeBackgroundColor()
' I know something is missing here, but no idea what.
Dim cond1 As FormatCondition
Dim cond2 As FormatCondition
Dim cond3 As FormatCondition
' Search Range
Dim rgSearch As Range
Set rgSearch = Range("D2", Range("D2").End(xlDown))
' Delete prevoius conditional formats
rgSearch.FormatConditions.Delete
' Conditions to perform the search
With rgSearch.FormatConditions.Add(xlTextString, TextOperator:=xlContains, String:="Var01:Var03")
With rgSearch.FormatConditions.Add(xlTextString, TextOperator:=xlContains, String:="Var04:Var11")
If cond1 Then
.Interior.Color = vbGreen
.Font.Color = vbBlack
ElseIf cond2 Then
.Interior.Color = vbRed
.Font.Color = vbBlack
ElseIf cond3 Then
.Interior.Color = vbYellow
.Font.Color = vbBlack
End If
End With
End Sub
Sub Add_Text()
' Add text
Dim InsTxtRange As Range
' Insert Text Range
InsTxtRange = Range("L2", Range("L2").End(xlDown)).Value
If InStr(DESCRIPCION, "Va04:Var11") Then
If cond1 Then
' Add text on column L Stating Mensual
ElseIf cond2 Then
' Add text on column L Stating Anual
ElseIf cond3 Then
' Add text on column L Stating On Demand.
End Sub