excel 通过将定义范围的名称拆分为常量和动态部分,连续检索定义范围

bprjcwpo  于 2023-02-05  发布在  其他
关注(0)|答案(2)|浏览(116)

下面的代码应该是停车场管理工具的一部分。它基本上只不过是一个条件格式的停车位的形式定义的范围在表“GF”基于相关的状态定义为字符串在表“GF列表”的列表对象。
与下面的示例不同,该代码稍后将应用于数百个停车位,其中有八种可能的格式,因此我希望使用VBA过程而不是标准的条件格式来解决整个问题。
代码失败,因为我无法动态检索“CurrentLot”作为Range,然后在IfThenElse过程中对其进行格式化。
希望你们能帮我。谢谢。

Sub No_01_to_05a()

Dim gfList As Worksheet
Dim gfPlan As Worksheet
Dim status As String
Dim CurrentLot As Range
Dim i As Integer
Dim No As String

Set gfList = ThisWorkbook.Worksheets("GF List")
Set gfPlan = ThisWorkbook.Worksheets("GF")

'Parking lots that are defined manually here
Dim LotNo1 As Range
Set LotNo1 = gfPlan.Range("B2", "C2")

Dim LotNo2 As Range
Set LotNo2 = gfPlan.Range("D2", "E2")

Dim LotNo3 As Range
Set LotNo3 = gfPlan.Range("F2", "G2")

Dim LotNo4 As Range
Set LotNo4 = gfPlan.Range("H2", "I2")

Dim LotNo5 As Range
Set LotNo5 = gfPlan.Range("J2", "K2")

Dim LotNo5a As Range
Set LotNo5a = gfPlan.Range("M2", "M3")

'ForNext procedure
For i = 4 To gfList.Range("E" & Application.Rows.Count).End(xlUp).Row
    
    status = gfList.Range("E" & i).Value
    No = gfList.Range("B" & i).Value
    CurrentLot = "LotNo" & No 'Line that does not seem to work
    
        If status = "Vacant" Then
                CurrentLot.Interior.Color = RGB(255, 255, 0)
                CurrentLot.Font.Color = RGB(0, 0, 0)
    
        ElseIf status = "Let" Then
                CurrentLot.Interior.Color = RGB(146, 208, 80)
                CurrentLot.Font.Color = RGB(0, 0, 0)

        ElseIf status = "Reserved" Then
                CurrentLot.Interior.Color = RGB(0, 176, 240)
                CurrentLot.Font.Color = RGB(0, 0, 0)
                
        Else
                CurrentLot.Interior.Color = RGB(255, 255, 255)
                CurrentLot.Font.Color = RGB(0, 0, 0)     
        End If
                  
Next i

End Sub
qhhrdooz

qhhrdooz1#

你不能仅仅通过给一个变量赋值一个字符串来把一个变量resp. object赋值给另一个变量/对象,这个字符串和变量的名字一样,这就是你想用CurrentLot = "LotNo" & No做的
您必须使用Select Case语句来实现这一点

Option Explicit

Sub No_01_to_05a()

    Dim gfList As Worksheet
    Set gfList = ThisWorkbook.Worksheets("GF List")
    Dim status As String
    Dim CurrentLot As Range
    Dim i As Integer
    Dim No As String

    'ForNext procedure
    For i = 4 To gfList.Range("E" & Application.Rows.Count).End(xlUp).Row
    
        status = gfList.Range("E" & i).Value
        No = gfList.Range("B" & i).Value
        'CurrentLot = "LotNo" & No   <= You cannot assign a string to a range
        Set CurrentLot = getLot(No)
    
        If status = "Vacant" Then
            CurrentLot.Interior.Color = RGB(255, 255, 0)
            CurrentLot.Font.Color = RGB(0, 0, 0)
    
        ElseIf status = "Let" Then
            CurrentLot.Interior.Color = RGB(146, 208, 80)
            CurrentLot.Font.Color = RGB(0, 0, 0)

        ElseIf status = "Reserved" Then
            CurrentLot.Interior.Color = RGB(0, 176, 240)
            CurrentLot.Font.Color = RGB(0, 0, 0)
                
        Else
            CurrentLot.Interior.Color = RGB(255, 255, 255)
            CurrentLot.Font.Color = RGB(0, 0, 0)
        End If
                  
    Next i

End Sub

Function getLot(ByVal No As Long) As Range
    
    Dim gfPlan As Worksheet
    Set gfPlan = ThisWorkbook.Worksheets("GF")
    
    'Parking lots that are defined manually here
    Dim LotNo1 As Range
    Set LotNo1 = gfPlan.Range("B2", "C2")

    Dim LotNo2 As Range
    Set LotNo2 = gfPlan.Range("D2", "E2")

    Dim LotNo3 As Range
    Set LotNo3 = gfPlan.Range("F2", "G2")

    Dim LotNo4 As Range
    Set LotNo4 = gfPlan.Range("H2", "I2")

    Dim LotNo5 As Range
    Set LotNo5 = gfPlan.Range("J2", "K2")

    Dim LotNo5a As Range
    Set LotNo5a = gfPlan.Range("M2", "M3")
    Select Case No
        Case 1
            Set getLot = LotNo1
        Case 2
            Set getLot = LotNo2
        Case 3
            Set getLot = LotNo3
        Case 4
            Set getLot = LotNo4
        Case 5
            Set getLot = LotNo5
        Case Else
            '
    End Select
End Function

Select Case语句上的其他reading

x4shl7ld

x4shl7ld2#

VBA不具备在语言内部计算表达式的功能,因此由于批次No并不总是数字,因此可以使用Dictionary保存批次,并将No作为键

Sub No_01_to_05a()

    Dim gfList As Worksheet
    Dim gfPlan As Worksheet
    Dim status As String
    Dim CurrentLot As Range
    Dim i As Integer
    Dim No As String
    Dim dictLot As Object
    
    Set dictLot = CreateObject("Scripting.Dictionary")
    
    Set gfList = ThisWorkbook.Worksheets("GF List")
    Set gfPlan = ThisWorkbook.Worksheets("GF")
    
    dictLot "1", gfPlan.Range("B2", "C2")
    dictLot "2", gfPlan.Range("D2", "E2")
    dictLot "3", gfPlan.Range("F2", "G2")
    dictLot "4", gfPlan.Range("H2", "I2")
    dictLot "5", gfPlan.Range("J2", "K2")
    dictLot "5a", gfPlan.Range("M2", "M3")
    
    'ForNext procedure
    For i = 4 To gfList.Range("E" & Application.Rows.Count).End(xlUp).row
        
        status = gfList.Range("E" & i).Value
        No = gfList.Range("B" & i).Value

        If Not dictLot.Exists(No) Then
            MsgBox "Lot: " & No & " does not exists.", vbInformation ' Show a message when the lot does not exists
            Exit For
        End If
        
        Set CurrentLot = dictLot(No) ' Retrieve the lot using the No as key in the dictionary
        
        If status = "Vacant" Then
            CurrentLot.Interior.Color = RGB(255, 255, 0)
            CurrentLot.Font.Color = RGB(0, 0, 0)
        ElseIf status = "Let" Then
            CurrentLot.Interior.Color = RGB(146, 208, 80)
            CurrentLot.Font.Color = RGB(0, 0, 0)
        ElseIf status = "Reserved" Then
            CurrentLot.Interior.Color = RGB(0, 176, 240)
            CurrentLot.Font.Color = RGB(0, 0, 0)
        Else
            CurrentLot.Interior.Color = RGB(255, 255, 255)
            CurrentLot.Font.Color = RGB(0, 0, 0)
        End If
                      
    Next i

End Sub

Here更多信息关于字典

相关问题