搜索由数字 VBA 定义的列
Searching column defined by number VBA
基本上我有一个函数可以在某个工作表中搜索列标题,然后 returns 列号。
我还需要另一个函数来搜索此列的指定值。将两者结合起来对我来说会很好,但我不确定如何使用列号来表达要搜索的范围。
我的代码大纲如下;没什么好说的,我只是不知道在 Range 位中放什么
干杯!
Function getValue(...parameters...) As Double
col = getColumnNumber(worksheetName, columnTitle) 'get column number
With Worksheets(worksheetName).Range(****)
...conditions for which value
End With
End Function
试试看
With Worksheets(worksheetName).Columns(col)
并且您可以使用 .Cells
访问单元格,就像任何其他范围一样。
你可以使用VBA中的watch/variable window或者像这样使用TypeName
函数来查看各种表达式和变量的类型:
Debug.Print TypeName(Worksheets("Sheet1").Columns(1))
,其中 returns Range
.
使用 Worksheet(wsName).Columns(columnNumber)
引用您的目标列。
以下是解决问题的示例代码:
选项显式
Function getColumnNumber(wsTarget As Worksheet, columnTitle As String) As Integer
Dim rngHeader As Range
Dim colNum As Integer
Dim rngFind As Range
Set rngHeader = wsTarget.Range("1:1")
Set rngFind = rngHeader.Find(What:=columnTitle, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True)
If Not rngFind Is Nothing Then
getColumnNumber = rngFind.Column
End If
End Function
Function valueExists(colName As String, findVal As String) As Boolean
Dim colNum As Integer
Dim wsTarget As Worksheet
Dim rngFindVal As Range
Set wsTarget = ThisWorkbook.Worksheets(1)
colNum = getColumnNumber(wsTarget, colName)
If colNum > 0 Then
Set rngFindVal = wsTarget.Columns(colNum).Find(What:=findVal, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True)
If Not rngFindVal Is Nothing Then
valueExists = True
Else
valueExists = False
End If
Else
MsgBox "Column header not found!", vbCritical
valueExists = False
End If
End Function
Sub test()
MsgBox valueExists("myHeader", "myVal")
End Sub
基本上我有一个函数可以在某个工作表中搜索列标题,然后 returns 列号。
我还需要另一个函数来搜索此列的指定值。将两者结合起来对我来说会很好,但我不确定如何使用列号来表达要搜索的范围。
我的代码大纲如下;没什么好说的,我只是不知道在 Range 位中放什么
干杯!
Function getValue(...parameters...) As Double
col = getColumnNumber(worksheetName, columnTitle) 'get column number
With Worksheets(worksheetName).Range(****)
...conditions for which value
End With
End Function
试试看
With Worksheets(worksheetName).Columns(col)
并且您可以使用 .Cells
访问单元格,就像任何其他范围一样。
你可以使用VBA中的watch/variable window或者像这样使用TypeName
函数来查看各种表达式和变量的类型:
Debug.Print TypeName(Worksheets("Sheet1").Columns(1))
,其中 returns Range
.
使用 Worksheet(wsName).Columns(columnNumber)
引用您的目标列。
以下是解决问题的示例代码:
选项显式
Function getColumnNumber(wsTarget As Worksheet, columnTitle As String) As Integer
Dim rngHeader As Range
Dim colNum As Integer
Dim rngFind As Range
Set rngHeader = wsTarget.Range("1:1")
Set rngFind = rngHeader.Find(What:=columnTitle, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True)
If Not rngFind Is Nothing Then
getColumnNumber = rngFind.Column
End If
End Function
Function valueExists(colName As String, findVal As String) As Boolean
Dim colNum As Integer
Dim wsTarget As Worksheet
Dim rngFindVal As Range
Set wsTarget = ThisWorkbook.Worksheets(1)
colNum = getColumnNumber(wsTarget, colName)
If colNum > 0 Then
Set rngFindVal = wsTarget.Columns(colNum).Find(What:=findVal, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True)
If Not rngFindVal Is Nothing Then
valueExists = True
Else
valueExists = False
End If
Else
MsgBox "Column header not found!", vbCritical
valueExists = False
End If
End Function
Sub test()
MsgBox valueExists("myHeader", "myVal")
End Sub