从列中查找值

时间:2017-03-25 14:05:47

标签: excel vba

我有两张纸。

数据

Column A   Column B    Column C     Column D     Column E

1234

第1页

Cell N3 = 1234

我正在使用此代码尝试在A-E列中找到值。

但由于某种原因,它总是会返回错误的结果。

Set c = Selection.Find(What:=Worksheets(1).Range("N3").Value, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

请有人告诉我我哪里出错了吗?

完整代码:

Sub PhoneBook()
'Start Phone Book Directory Code
Dim Contact As String
Dim Email As String
Dim Phone As String
Dim Fax As String

'Start FIND
Dim c As Variant

With Worksheets("Contacts").Range("A2:E10000")

Set c = Selection.Find(What:=Worksheets(1).Range("N3").Value, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)


If c Is Nothing Then

'Introduce FailSafe, escape code if no result found
ThisWorkbook.Worksheets(1).Shapes("Suggest").Visible = False
ThisWorkbook.Worksheets(1).Shapes("Close").Visible = False
'ActiveSheet.Unprotect Password:="SecretPassword"

Else

'Check values are not blank
If c.Offset(0, 1).Value <> "" Then
Contact = "Contact: " & c.Offset(0, 1).Value & Chr(10)
Else
Contact = "Contact: No Contact Held" & Chr(10)
End If

If c.Offset(0, 2).Value <> "" Then
Email = "Email: " & c.Offset(0, 2).Value & Chr(10)
Else
Email = "Email: No Email Held" & Chr(10)
End If

If c.Offset(0, 3).Value <> "" Then
Phone = "Phone: " & c.Offset(0, 3).Value & Chr(10)
Else
Phone = "Phone: No Phone Held" & Chr(10)
End If

If c.Offset(0, 4).Value <> "" Then
Fax = "Fax: " & c.Offset(0, 4).Value
Else
Fax = "Fax: No Fax Held"
End If

If IsNumeric(c.Value) Then
what_found = c.Offset(0, 1).Value
Else
what_found = c.Value
End If

'Show Contacts
ThisWorkbook.Worksheets("Data").Range("I2").Value = "Hello," & vbNewLine & "Have you tried to contact " & what_found & " about your issue?" & vbNewLine _
& Contact & Email & Phone & Fax

'ThisWorkbook.Worksheets(1).Shapes("Suggest").TextFrame.AutoSize = True
CenterShape ThisWorkbook.Worksheets(1).Shapes("Suggest")
RightShape ThisWorkbook.Worksheets(1).Shapes("Close")
ThisWorkbook.Worksheets(1).Shapes("Suggest").Visible = True

'Show Close Button
'ThisWorkbook.Worksheets(1).Shapes("Close").OnAction = "HideShape"
ThisWorkbook.Worksheets(1).Shapes("Close").Visible = True

'Protect sheet
'ActiveSheet.Protect Password:="SecretPassword", userinterfaceonly:=True
'ActiveSheet.Shapes("Suggest").Locked = True

End If
End With


End Sub




Public Sub CenterShape(o As Shape)
o.Left = ActiveWindow.VisibleRange(1).Left + (ActiveWindow.VisibleRange.Width / 2 - o.Width / 2)
o.Top = ActiveWindow.VisibleRange(1).Top + (ActiveWindow.VisibleRange.Height / 2 - o.Height / 2)
End Sub

Public Sub RightShape(o As Shape)
o.Left = ThisWorkbook.Worksheets(1).Shapes("Suggest").Left + (ThisWorkbook.Worksheets(1).Shapes("Suggest").Width / 1.01 - o.Width / 1.01)
o.Top = ThisWorkbook.Worksheets(1).Shapes("Suggest").Top + (ThisWorkbook.Worksheets(1).Shapes("Suggest").Height / 30 - o.Height / 30)
End Sub

2 个答案:

答案 0 :(得分:0)

正如Jeeped建议的那样,删除范围的Selection对象,而不是使用您在“With”语句中定义的范围。另外,我将“After”参数更改为对“With”语句中定义的范围中的第一个单元格的引用;并且,“LookIn”参数更改为xlValues。

...
With Worksheets("Contacts").Range("A2:E10000")

Set c =.Find(What:=Worksheets(1).Range("N3").Value, After:=.Cells(1), LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)    
...

此搜索查看从A到E的所有列。如果找到匹配值,则Find方法可以返回对列A以外的列中的单元格的引用。这可能会为您的其余代码产生一些不寻常的结果,因为您使用偏移来获取其他联系号码。您可能需要考虑为每个列定义常量,并使用“Column”参数的常量来调用Cells属性。

例如:

为Phonebook()开头的列声明常量。

Const lCOLUMN_EMAIL as long = 3

使用Cells属性返回值,如下面的电子邮件示例所示:

If .Cells(c.Row, lCOLUMN_EMAIL).Value <> "" Then
Email = "Email: " & .Cells(c.Row, lCOLUMN_EMAIL).Value & Chr(10)
Else
Email = "Email: No Email Held" & Chr(10)
End If

将搜索范围限制为A列或列中包含值的列。 (如果要搜索所有列中的所有单元格,请忽略此部分。)

Set c = .Columns(1).Find(What:=Worksheets(1).Range("N3").Value, After:=.Cells(1), LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False

答案 1 :(得分:0)

您无需使用选择。它仅在选择中检查范围。 如果您知道要检查的范围,请使用工作表(“联系人”)。范围(“A2:E10000”)然后 更改代码如下所示

设置c = .Find(What:= Worksheets(1).Range(“N3”)。Value,After:=。Range(“A1”),LookIn:= xlFormulas,_ LookAt:= xlWhole,SearchOrder:= xlByRows,SearchDirection:= xlNext,_ MatchCase:= False,SearchFormat:= False)