Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 在Excel中按单元格值获取列号_Vba_Excel - Fatal编程技术网

Vba 在Excel中按单元格值获取列号

Vba 在Excel中按单元格值获取列号,vba,excel,Vba,Excel,我试图用一个特定的值获取第一行单元格的列号。由于某种原因,我正在使用的代码不起作用 Dim colNum As Integer 'sheetName is a String With ActiveWorkbook.Sheets(sheetName) colNum = column(.Match("ID", 1:1, 0)) 它告诉我它需要一个“列表分隔符或”。我如何让它工作 谢谢请尝试以下代码: Dim colNum As Integer 'sheetName is a String colN

我试图用一个特定的值获取第一行单元格的列号。由于某种原因,我正在使用的代码不起作用

Dim colNum As Integer
'sheetName is a String
With ActiveWorkbook.Sheets(sheetName)
colNum = column(.Match("ID", 1:1, 0))
它告诉我它需要一个“列表分隔符或”。我如何让它工作

谢谢

请尝试以下代码:

Dim colNum As Integer
'sheetName is a String
colNum = WorksheetFunction.Match("ID", ActiveWorkbook.Sheets(sheetname).Range("1:1"), 0)

注意,如果没有找到匹配项,它将返回一个错误;试试这个来处理那个

Dim Result As Variant
If Not VBA.IsError(Application.Match(...)) Then
    Result = Application.Match(...)
End If
这将尝试匹配,如果函数出现错误,则不会指定结果


工作正常,我想要什么,谢谢