我正在尝试使用Vlookup函数,以便在另一个工作表上查找状态代码的副本。但是范围与字符串不匹配。范围实际上只是50个州,我正在尝试使其匹配。
我尝试签出以确保状态缩写为字符串。我也尝试过将范围转换为字符串,但这也导致了错误。奇怪的是,如果我仅将特定状态匹配在一起,就表示它们相等。
Sub State_Assignment()
Application.ScreenUpdating = False
'On Error Resume Next
' State_Assignment Macro
Dim Counter As Integer
Counter = 1
Dim Other As Integer
Other = 0
Dim State As String
State = " "
'
'First, we will check for specialty brokers.
'Check if Specialty Broker requires a state to assign. In this case, we are making sure to include N and n as options, due to future proofing.
If Worksheets("SBSS_Assignment_Tool").Range("G3").Value = "None" Then
ElseIf CStr(Application.VLookup(G3, Worksheets("Special_Cases").Range("A3:A100"), 2)) = "N" Or CStr(Application.VLookup(G3, Worksheets("Special_Cases").Range("A3:A100"), 2)) = "n" Then
E5.Value = WorksheetFunction.VLookup(G3, Worksheets("Special_Cases").Range("A3:A100"), 3)
'If State is needed for Specialty Broker, make sure operator knows. In this case, we are making sure to include Y and y as options, due to future proofing.
ElseIf "Y" = CStr(Application.VLookup(G3, Worksheets("Special_Cases").Range("A3:A100"), 2)) Or "y" = CStr(Application.VLookup(G3, Worksheets("Special_Cases").Range("A3:A100"), 2)) Then
MsgBox ("State is reqired to assign SBSS for this broker.")
'This only leaves the case of Yes and the state is specified.
Else
' Select the cell of the Broker we are looking at.
Worksheets("Special_Cases").Activate
Range("A3:A100").Find(CStr(Application.VLookup(G3, Worksheets("Special_Cases").Range("A3:A100"), 1))).Select
' Now check to make sure the State isn't the cell in the same row as it.
If ActiveCell.Offset(0, 2) = Worksheets("SBSS_Assignment_Tool").Range(G20).Value Then
Worksheets("SBSS_Assignment_Tool").Range(J7).Value = ActiveCell.Offset(0, 3)
'See if there are more states to check, by seeing if there is a empty cell below. We use the Counter varible as future-proofing, so specialty brokers can have as many states as they need.
'The "Other" state specification can also be a problem, so if we see it, we will remeber it's cell and use it if none of the other states match up.
ElseIf ActiveCell.Offset(Counter, 0) = " " Then
If ActiveCell.Offset(Counter, 2) = Worksheets("SBSS_Assignment_Tool").Range(G20).Value Then
Worksheets("SBSS_Assignment_Tool").Range(J7).Value = ActiveCell.Offset(Counter, 3)
ElseIf ActiveCell.Offset(Counter, 2) = "Other" Then
Other = Counter
End If
Counter = Counter + 1
Else
'If we check all the states and none match, we use the "Other" cell.
Worksheets("SBSS_Assignment_Tool").Range(J7).Value = ActiveCell.Offset(Other, 3)
End If
End If
' Looks for State From Drop-Down List and Gather Rules From Cell To The Right of It
Worksheets("SBSS_Assignment_Tool").Range("J20") = Application.VLookup(Worksheets("SBSS_Assignment_Tool").Range("G20").Value, Worksheets("State_Assignments").Range("A2:A100"), 2)
'Check for Special Broker Rules, first ruling out the No's
MsgBox (CStr(Worksheets("SBSS_Assignment_Tool").Range("G20").Value) + " ")
State = Worksheets("SBSS_Assignment_Tool").Range("G20").Value
MsgBox (CStr(Worksheets("State_Assignments").Range("A16").Value) + " ") '
MsgBox (Worksheets("SBSS_Assignment_Tool").Range("G20").Value = Worksheets("State_Assignments").Range("A16").Value)
Dim X As Range
Set X = Worksheets("State_Assignments").Range("A1:A100")
MsgBox (Application.VLookup(State, Worksheets("State_Assignments").Range("A2:A51"), 3))
If Application.VLookup(Worksheets("SBSS_Assignment_Tool").Range("G20").Value, Worksheets("State_Assignments").Range("A1:A100"), 3) = "N" Then
Else
'Selec the cell of the SBSS
Worksheets("Special_Cases").Range("A1:AA1").Find(Application.VLookup(G20, Worksheets("State_Assignments").Range("A1:A100"), 3)).Select
J28.Value = ActiveCell.Offset(1, 0)
End If
消息框可以正常工作,但是一旦获得Vlookups,便是它开始挣扎的时候了。