我是VBA编码的新手,我想在工作表中找到第一个数字/短日期/日期列的列索引。
数据看起来像这样的一个例子:
id sex 2015 2016
2 M 1 3
4 F 7 5
3 F 8 9
在此示例中,答案应为3。我想将其设置为变量,以便以后使用此数字。
到目前为止,这是我的代码:
Sub find_value()
Dim c As Range
Dim firstAddress As String
With Worksheets(1).Range("A1:A500")
Set c = .Find(2015, lookin:=xlValues)
End Sub
如您所见,这只是一个静态解决方案。
任何想法都很棒!非常感谢
非常粗糙,但是这里有个函数可以让您获得想要的结果。您可以调用以result
供以后使用,也可以根据需要调用该函数。有目的地对此进行了细分,以便您了解如何find_value
定义。您可以选择合并代码。
Sub test()
Dim result As String
result = find_value(2015, 1) 'find year 2015, entry 1
Debug.Print result
End Sub
Function find_value(year As Integer, entry As Integer)
Dim yearVal, entryVal, foundVal As Integer
yearVal = Application.WorksheetFunction.Match(year, Worksheets(1).Range("1:1"), 0)
entryVal = Application.WorksheetFunction.Match(entry, Worksheets(1).Range(Worksheets(1).Cells(1, yearVal), Worksheets(1).Cells(500, yearVal)), 0)
foundVal = Cells(entryVal, yearVal + 1)
find_value = foundVal
End Function
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句