我正在尝试用excel编写一个程序,用户输入一个值,它在其他工作表中搜索该值,找到具有该值的列,在该列的行中搜索晚于今天日期的日期,并返回第一列的内容,这些行的值晚于今天的日期。
到目前为止,我已经能够找到具有输入值的单元格,但是我遇到了如何引用找到的单元格的列来让整个列搜索日期的问题。
下面是我到目前为止所拥有的,我认为我应该在"If Worksheets(i).Cells(2,j).Value = Method Then“后面输入另一个If语句:"If Worksheets(i).Cells(?,?).Value >=Today() Then",但是我不确定如何引用我想要搜索的单元格,因为这些单元格取决于它们在前面语句中的位置。
Private Sub CommandButton1_Click()
totalsheets = Worksheets.Count
Method = Worksheets("Search a Method").Cells(3, 6).Value
For i = 1 To totalsheets
If Worksheets(i).Name <> "Search a Method" Then
lastcolumn = Worksheets(i).Cells(2, Columns.Count).End(xlToLeft).Column
For j = 2 To lastcolumn
If Worksheets(i).Cells(2, j).Value = Method Then
Worksheets("Search a Method").Activate
lastrow = Worksheets("Search a Method").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Search a Method").Cells(lastrow + 1, 1).Value = Worksheets(i).Name
End If
Next
End If
Next
End Sub发布于 2019-06-01 02:18:32
我将以我关于使用.Find()的建议为基础,下面的示例应该会给出一些指导:
Dim findrng As Range, col As Long, method As String
method = "dog"
With Sheets(1)
Set findrng = .Rows(2).Find(what:=method, LookIn:=xlValues, LookAt:=xlPart, searchorder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
End With
col = findrng.Column
Debug.Print col我将“猫”放在单元格(2,2)中,将“狗”放在单元格(2,3)中,因此在上面的代码中,即时窗口显示为"3",表示找到了列method。
上图:

https://stackoverflow.com/questions/56399270
复制相似问题