Sheet1
上有45列
每次列不在同一个地方时,在Sheet1
中有两个重要的列是工作国家和基础国家/ ,我正在检查如果两列中的值相同或不同:如果它们相同,则列状态中的输出应为本地,否则 Expat 。
但是当我尝试计算我的公式时,结果是 #Name?
你能帮我解决这个问题吗?我附上了工作表的屏幕截图
我的Excel文件也包含VBA模块,所以我可以使用公式或VBA代码。
这是我用过的公式:
=IF(Work Geography,A2)=IFS(Work Country,B2)
答案 0 :(得分:2)
Work Geography
和Work Country
),但您可能忘记定义它们。Work Geography
或Work Country
或两者中,因为名称不能包含空格字符。IF
函数可能会导致意外结果。 我将使用公式:
=IF(A2=B2, "Local", "Expat")
感谢@Zac的澄清 这就是我想出的:
Option Explicit
Sub CheckCountries()
Dim lRow As Long
Dim MySheet As Worksheet
Dim rngGeo As Range, rngCountry As Range, rngStatus As Range
Dim rngData As Range
' Define Sheet1
Set MySheet = ThisWorkbook.Worksheets("Sheet1")
' I assumed headers are constantly on the first row
' Find cells with needed headers
Set rngGeo = MySheet.Rows(1).Find("Work Geography", , xlValues, xlWhole, xlByColumns)
Set rngCountry = MySheet.Rows(1).Find("Work Country", , xlValues, xlWhole, xlByColumns)
Set rngStatus = MySheet.Rows(1).Find("Status", , xlValues, xlWhole, xlByColumns)
' Your big table, excluding headers
With MySheet.Range("A1").CurrentRegion
Set rngData = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count)
End With
' Define needed columns
Set rngGeo = Intersect(rngData, rngGeo.EntireColumn)
Set rngCountry = Intersect(rngData, rngCountry.EntireColumn)
Set rngStatus = Intersect(rngData, rngStatus.EntireColumn)
' Calculate
rngStatus.Value _
= Evaluate("=IF(" & rngGeo.Address & "=" & rngCountry.Address & ", " & _
"""Local"", " & _
"""Expat"")")
End Sub