Я использую Excel VBA, чтобы попытаться решить следующую проблему:
В столбце А мне дан список из 42 стран. В столбце D мне дана цена на Биг Мак в этой стране в долларах США. Строка 1 имеет заголовки, поэтому данные начинаются со строки 2. Мне нужно создать макрос, который позволит пользователю вводить 2 страны (Страна1 и Страна2), циклически перебирает столбец А, чтобы найти страны, в которых он находится, и соответствующие им Цены. Это должно сохранить местоположение ячейки страны в какую-то переменную, а цену - просто число. Если цена Country1 больше, чем у страны 2, название Country1 должно иметь цвет шрифта зеленый, а Country2 цвет шрифта красный. И наоборот.
В настоящий момент я получаю сообщение об ошибке «Переменная объекта или переменная блока не установлена» для моей переменной TheCell.
Если вы хотите проверить это, вот верхняя часть листа:

Я пытался поиграть с тем, что я затемнил TheCell. Я пытался сделать это вариант, но это не сработало. Я почти уверен, что Range правильный, потому что это фактическая ячейка.
Sub CountryComparison()
Dim Counter As Integer
Dim Country1 As String
Dim Country2 As String
Dim TheCell As Range
Dim Price1Cell As Range
Dim Price2Cell As Range
Dim Price1 As Single
Dim Price2 As Single
'The user inputs what countries they want to compare
Country1 = InputBox("Enter Country 1")
Country2 = InputBox("Enter Country 2")
'We are starting at row 2, column 1. Since we're going to check every row, I'm making counter a variable so that I can continuously add 1 to it after every loop.
Counter = 2
TheCell = Cells(Counter, 1)
'Here's my loop. It will select TheCell, and if it contains the name of Country1, then it will save that cell as Price1Cell (to be used later), and save the price of a Big Mac in that country (also to be used later). It does the same thing for Country2. And if neither is a match, it goes on to the next row. Since there are 42 rows, it does this until Counter is greater than 43 (maybe it should be until greater than 42, but that shouldn't matter.)
Do
TheCell.Select
If ActiveCell.Value = Country1 Then
Set Price1Cell = Range(ActiveCell.Address)
Price1 = ActiveCell.Offset(0, 3).Value
End If
If ActiveCell.Value = Country2 Then
Set Price2Cell = Range(ActiveCell.Address)
Price2 = ActiveCell.Offset(0, 3).Value
End If
Counter = Counter + 1
Loop Until Counter > 43
'Here's the final point. If Country1's price is greater than Country2's Price, then Country1 should be colored red and Country2 green. And vice-versa.
If Price1 > Price2 Then
Price1Cell.Font.Color = vbRed
Price2Cell.Font.Color = vbGreen
End If
If Price2 > Price1 Then
Price1Cell.Font.Color = vbGreen
Price2Cell.Font.Color = vbRed
End If
End Sub