2017-08-04 435 views
1

我工作的Excel電子表格,拉從SQL數據庫選擇列表,然後填充表。然後提示用戶掃描零件號碼。我試圖在A列中找到部件號,並返回部件號的行。VBA - 使用SearchRange查找變量值

部件號開始作爲Variant類型,但認爲類型是問題,我通過其值設置爲另一個變量它轉換成字符串。

最後,我發現這個代碼片斷(我試過很多,至今沒有工作過),並在您指定的號碼(123456789012),如下面的代碼它的工作原理。如果使用變量sPart或scanPart替換該數字,則不起作用。

我需要它的工作找到變量SPART(String)或scanPart(變體)的行。我究竟做錯了什麼?

Function ReturnRowNumber() 

Dim ws As Excel.Worksheet 
Set ws = ActiveSheet 

Dim scanPart As Variant 
Dim sPart As String 
Dim FoundRow As String 

scanPart = InputBox("Scan the first part number", "Part Number") 'Get Part Number 

sPart = scanPart 

MsgBox sPart 

Dim SearchRange As Range 
Dim FindRow As Range 
Set SearchRange = Range("A1", Range("A65536").End(xlUp)) 
Set FindRow = SearchRange.Find(123456789012#, LookIn:=xlValues, lookat:=xlWhole) 
MsgBox FindRow.Row 

End Function 

在此先感謝您的幫助!

達納

回答

3

寫在您的文章不會有任何效果,因爲語法不正確,尤其是在.Find語句中的代碼。或許,這個問題被扔你在 錯了方向,以爲這是一個數據類型的問題。

請參閱該代碼。測試了字符串和數字值。

Option Explicit 

Sub Test() 

    Dim sPart As String 
    sPart = InputBox("Scan the first part number", "Part Number") 'Get Part Number 

    MsgBox ReturnRowNumber(sPart) 

End Sub 


Function ReturnRowNumber(sPart As String) As Long 

    Dim ws As Worksheet 
    Set ws = Worksheets("Sheet1") 'be more explicit than 'ActiveSheet 

    Dim SearchRange As Range 
    Dim FindRow As Range 
    Set SearchRange = ws.Range("A1", ws.Range("A65536").End(xlUp)) 
    Set FindRow = SearchRange.Find(sPart, LookIn:=xlValues, lookat:=xlWhole) 

    If Not FindRow Is Nothing Then ReturnRowNumber = FindRow.Row 

End Function 
+0

也許一部分的困惑是信息在Excel中的哪種類型。我們的一些零件號碼都是數字,其他的是數字和連字符,還有一些是字母和數字。 Excel中字段的格式目前被歸類爲文本。那會使它成爲一個字符串,對嗎? –

+0

@DanaSkyware - 無論這個代碼應該工作的格式。由於字符串變量會將收集的任何內容轉換爲字符串類型。 –

+0

使用您的代碼時,當查找部件號1504915-0013時,它返回了一行'0'。我粘貼了當前電子表格截圖的鏈接。 https://www.dropbox.com/s/zzbvvgnsqxpa0wj/spreadsheet_sample.png?dl=0 –

1

從你的代碼和你的問題的描述聽起來好像發生了什麼事是你要定義一個變量爲Variant,但Varian確實是一個String(因此,千萬不要用Variant除非你有一個非常具體的原因)。

喜歡的東西:

Dim Foo as Variant 
Dim Bar as Long 

Foo = "123" 
Bar = "123" 

是基本上像說:

Dim Foo as String 
Dim Bar as Long 

Foo = "123" 
Bar = CLng("123") 

之所以這樣,是很重要的是因爲:

12345 <> "12345" 

他們是不同的值。一個是Long表示,另一個是String表示。

解決您的問題?這取決於你的初始值如何存儲。如果您使用條形碼的String表示,那麼您想要將條形碼聲明爲String,並使用String進行搜索。看起來你確實在存儲整個號碼,如果是這種情況,你會想要將你的條碼聲明爲Long

如果由於某種詛咒,你將條形碼存儲爲Strings並作爲整數,那麼你需要選擇一個或另一個,或者你需要使用更強大的查找方法(我推薦字典)。

0
Function ReturnRowNumber() 

Dim ws As Excel.Worksheet 
Set ws = ActiveSheet 

Dim scanPart As Variant 
Dim sPart As String 
Dim FoundRow As String 
Dim xlCell as Range 
scanPart = InputBox("Scan the first part number", "Part Number") 'Get Part Number 

sPart = scanPart  

Dim SearchRange As Range 
Dim FindRow As Range 
Set SearchRange = Range(Range("A1"), Range("A1").End(xlDown)) 
Set xlCell = ActiveCell 
Application.ScreenUpdating = False 
SearchRange.Select 
Set FindRow = SearchRange.Find(What:=sPart, After:=ActiveCell, SearchOrder:=xlByRows) 
xlCell.Select 
Application.ScreenUpdating = True 
MsgBox FindRow.Row 

End Function 

它看起來像你的查找功能沒有正確格式。上面的代碼完全適合我

+0

您的代碼在Set FindRow行上給出運行時錯誤13類型不匹配。 –

+0

我不知道它爲什麼會拋出這個錯誤,但我確實知道它何時拋出它。噹噹前選定的單元格不是定義的搜索範圍的一部分時,似乎會顯示錯誤。我編輯了上面的代碼來處理這個問題。 –