VBA Sub用于根据Top行和First Column定位单元格

时间:2015-06-23 18:01:17

标签: excel vba excel-vba

我的第一行是州名,例如VA CA,第一栏包含行业代码。需要一个VBA子,我点击这个名为Locate的按钮后,一个msgbox要求用户输入状态名称,第二个msgbox要求用户输入行业代码,vba找到状态和行业所在的列和行,找到值基于这2个坐标输入。

Sub Locate()
Dim siccode As Integer
Dim rngSearch As Range, rngFound, rngFound2 As Range
Dim r As Double
Dim c As Double
Dim states, cellvalue As String

siccode = InputBox(Prompt:="Please enter 2 digit SIC code: ", Title:="SIC   Code", Default:="SIC here")
states = InputBox(Prompt:="Please enter State name: ", Title:="State Name", Default:="State Name here")

Set rngSearch = Range("A:BB")
Set rngFound = rngSearch.Find(What:="siccode", LookIn:=xlValues, LookAt:=xlPart)
 If rngFound Is Nothing Then
 MsgBox "No claims at all"
 Else
 MsgBox rngFound.Row
 End If

Set rngSearch = Range("A:BB")
Set rngFound2 = rngSearch.Find(What:="states", LookIn:=xlValues, LookAt:=xlPart)
If rngFound2 Is Nothing Then
 MsgBox "No Claims at all"
 Else
 MsgBox rngFound2.Column
 End If

cellvalue = ActiveSheet.Cells(rngFound.Row, rngFound2.Column).value
MsgBox (cellvalue)
End Sub

它工作正常,直到if部分,无论输入什么,它总是没有任何声明。

1 个答案:

答案 0 :(得分:0)

在搜索行中,为什么siccode在引号中?它是一个整数变量,而不是一个字符串。状态也是如此,即变量。

Set rngFound = rngSearch.Find(What:="siccode", LookIn:=xlValues, LookAt:=xlPart)

Set rngFound2 = rngSearch.Find(What:="states", LookIn:=xlValues, LookAt:=xlPart)

尝试删除引号。