我想知道是否有人可以随时提供帮助。
我在Excel中创建了一个userform
并使用引用号,它将返回表中给定行的值。我目前收到的问题是,当我最初打开工作簿时,find函数不起作用,并返回代码中给出的Msgbox
“ID不存在”。
但是,如果我使用userform
在表格中创建一个新行,那么在此之后,find函数工作正常。有没有人有任何建议?
Private Sub Find_Click()
Dim searchRange As Range
Dim foundCell As Range
Dim mysearch As String
mysearch = Me.Search.Value
With ThisWorkbook.Sheets("Master Data")
Set searchRange = .Range("A2", .Range("A" & .Rows.Count).End(xlUp))
End With
Set foundCell = searchRange.Find(what:=mysearch, Lookat:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not foundCell Is Nothing Then
Me.RsnDc = foundCell.Offset(0, 4).Value
Me.BDM.Value = foundCell.Offset(0, 6).Value
Me.MIns.Value = foundCell.Offset(0, 7).Value
Me.EUs.Value = foundCell.Offset(0, 8).Value
Me.In.Value = foundCell.Offset(0, 9).Value
Me.Pr.Value = foundCell.Offset(0, 10).Value
Me.Qu.Value = foundCell.Offset(0, 11).Value
Me.ReCd.Value = foundCell.Offset(0, 12).Value
Me.ReOrCd.Value = foundCell.Offset(0, 13).Value
Me.Ttl.Value = foundCell.Offset(0, 5).Value
Me.Va.Value = Me.Total.Value / 1.2
Me.VT.Value = Me.Total.Value - Me.Value.Value
Me.R.Value = foundCell.Offset(0, 17).Value
Me.App.Value = foundCell.Offset(0, 18).Value
Me.L1.Value = foundCell.Offset(0, 19).Value
Me.L2.Value = foundCell.Offset(0, 20).Value
Me.CY.Value = foundCell.Offset(0, 21).Value
Me.PC.Value = foundCell.Offset(0, 22).Value
Me.SN1.Value = Left(foundCell.Offset(0, 23).Value, 2)
Me.SN2.Value = Mid(foundCell.Offset(0, 23).Value, 3, 2)
Me.SN3.Value = Right(foundCell.Offset(0, 23).Value, 2)
Me.ANCT.Value = foundCell.Offset(0, 24).Value
Else
MsgBox "ID does not exist."
End If
End Sub
答案 0 :(得分:0)
我注释掉了所有其他文本框并删除了#34; Me"来自" Search.Value"它工作正常。所以它可以在" Me"或者在其他文本框列表中的某个位置。尝试将它们一次评论一半以缩小范围。
Private Sub CommandButton1_Click()
Dim searchRange As Range
Dim foundCell As Range
Dim mysearch As String
mysearch = Search.Value
With ThisWorkbook.Sheets("Master Data")
Set searchRange = .Range("A2", .Range("A" & .Rows.Count).End(xlUp))
End With
Set foundCell = searchRange.Find(what:=mysearch, Lookat:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not foundCell Is Nothing Then
' Me.RsnDc = foundCell.Offset(0, 4).Value
' Me.BDM.Value = foundCell.Offset(0, 6).Value
' Me.MIns.Value = foundCell.Offset(0, 7).Value
' Me.EUs.Value = foundCell.Offset(0, 8).Value
' Me.In.Value = foundCell.Offset(0, 9).Value
' Me.Pr.Value = foundCell.Offset(0, 10).Value
' Me.Qu.Value = foundCell.Offset(0, 11).Value
' Me.ReCd.Value = foundCell.Offset(0, 12).Value
' Me.ReOrCd.Value = foundCell.Offset(0, 13).Value
' Me.Ttl.Value = foundCell.Offset(0, 5).Value
' Me.Va.Value = Me.Total.Value / 1.2
' Me.VT.Value = Me.Total.Value - Me.Value.Value
' Me.r.Value = foundCell.Offset(0, 17).Value
' Me.App.Value = foundCell.Offset(0, 18).Value
' Me.L1.Value = foundCell.Offset(0, 19).Value
' Me.L2.Value = foundCell.Offset(0, 20).Value
' Me.cy.Value = foundCell.Offset(0, 21).Value
' Me.pc.Value = foundCell.Offset(0, 22).Value
' Me.SN1.Value = Left(foundCell.Offset(0, 23).Value, 2)
' Me.SN2.Value = Mid(foundCell.Offset(0, 23).Value, 3, 2)
' Me.SN3.Value = Right(foundCell.Offset(0, 23).Value, 2)
' Me.ANCT.Value = foundCell.Offset(0, 24).Value
Beep
MsgBox "FOUND IT!"
Else
MsgBox "ID does not exist."
End If
End Sub
答案 1 :(得分:0)
问题似乎是在搜索范围列A中使用公式来指定搜索条件。我现在已将其构建到用户表单中,它似乎是一个梦想。谢谢你的时间。