我正在尝试获取具有特定值的第一行中单元格的列号。我使用的代码由于某些原因无法正常工作。
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
这会尝试匹配,并且如果函数上有错误,它将不会分配结果。
函数getColumnNo(hdrRow作为整数,columnName作为字符串)作为整数
Dim i As Integer
i = 1
getColumnNo = 0
Do While True
l = Len(Trim(Cells(hdrRow, i).Value))
If l = 0 Then Exit Function
If Cells(1, i).Value = columnName Then
getColumnNo = i
Exit Function
End If
i = i + 1
Loop
结束功能
Sub getColumnMapping()
Dim i作为整数,col作为字符串
i = 1设置colMapping = CreateObject(“ Scripting.Dictionary”)
按真做
With Sheets("All_Defects")
If Len(Trim(.Cells(1, i))) <> 0 Then
colMapping.Add Trim(.Cells(1, i).Value), .Cells(1, i).Address
Else
Exit Do
End If
End With
i = i + 1
循环
带有工作表(“ ColMapping”)
i = 1
Do While True
If Len(Trim(.Cells(i, 1).Value)) > 0 Then
col = colMapping(.Cells(i, 1).Value)
If Len(Trim(col)) > 0 Then
col = Left(col, InStr(2, col, "$") - 1)
col = "All_Defects!" & col & ":" & col
End If
.Cells(i, 2).Value = col
Else
Exit Do
End If
i = i + 1
Loop
.Visible = False
结尾为结束子