我有两张纸,我在这里工作。用户将从其中按下此按钮的sheet1包含一个名为“each”的列,该列只是与每个报告相关的日期列表。Sheet2 (截止日期)还包含内阁日期列,该列与在此日期之前要满足的一系列截止日期匹配(在同一行中)。//例如,1月份内阁中的任何报告草稿的截止日期都在同一行中。//
我想做一个按钮,当按下它时,将在sheet2中找到与活动单元格(在sheet1上)中的日期相匹配的内阁日期,以及拾取它所在的标题( searchCol),然后从sheet2返回正确的内阁日期中该事件的确切截止日期值。
我目前遇到的问题是,当我运行此按钮时,它似乎没有从sheet2中提取cause日期值,并导致带注释的行求值为nothing/true,并跳过打印截止日期的代码块。
当我按下这个的时候,我得到的只是"deadline not found“。即使实际上有一个值,它也应该回升。
Option Explicit
Private Sub CommandButton1_Click()
Dim cabDate As String, findme As Range
Dim searchCol As Integer
searchCol = ActiveCell.Column
cabDate = WorksheetFunction.Index(Range("A1:O9999"), ActiveCell.Row, 2)
If Not IsEmpty(cabDate) Then
With Worksheets("Deadlines").Range("B:B")
Set findme = Application.WorksheetFunction.VLookup(cabDate, Sheets("Deadlines").Range("A1:O9999"), 1, False)
If Not findme Is Nothing Then
Dim findmeAddr As String
findmeAddr = findme.Address(True, True)
Dim printable As String
printable = findme.Offset(0, searchCol).Value
MsgBox (printable)
Else
MsgBox ("Deadline not found")
End If
End With
Else
MsgBox ("invalid or non existent cabinet date")
End If
End Sub并不是所有的人都有编码经验,所以任何帮助都将不胜感激
编辑:

发布于 2017-01-04 21:46:23
您混淆了所有不同类型的变量以及每个函数返回的类型。
WorksheetFunction.Index -返回某个单元格(或单元格范围)的值,因此需要定义cabDate As String。因此,您需要使用If Not IsEmpty(cabDate) Then而不是使用If Not cabDate Is Nothing Then (因为它不是Range)。
使用Set findme的Find方法返回一个Range,因此以后不能将其与findme = findme.Address(True, True)一起使用,但是需要定义另一个变量As String,然后才能读取该变量的String地址。
最后,由于您希望从printable中读取值,因此它需要是As String或As Integer,而不是Range。
代码
Option Explicit
Private Sub CommandButton1_Click()
Dim cabDate As String, findme As Range
Dim searchCol As Integer
searchCol = ActiveCell.Column
cabDate = WorksheetFunction.Index(Range("A1:O9999"), ActiveCell.Row, 2)
If Not IsEmpty(cabDate) Then
With Worksheets("Deadlines").Range("B:B")
Set findme = .Find(what:=cabDate)
If Not findme Is Nothing Then
Dim findmeAddr As String
findmeAddr = findme.Address(True, True)
Dim printable As String
printable = findme.Offset(0, searchCol).Value
MsgBox (printable)
Else
MsgBox ("Deadline not found")
End If
End With
Else
MsgBox ("invalid or non existent cabinet date")
End If
End Sub发布于 2017-01-05 00:16:12
试一试
Set findme = .Find(what:=cabDate, lookIn:=xlValues, lookAt:=xlWhole)由于Range对象的Find()方法即使在Excel UI中进行设置,也会记住这些参数的最后设置
并且要确保ActiveCell是您实际期望的样子!
https://stackoverflow.com/questions/41463470
复制相似问题