Как избежать несоответствия типов в этом случае? - PullRequest
1 голос
/ 26 июня 2019

Я пытаюсь использовать функцию 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, они начинают колебаться.

...