Search VBA excel

Alcat03

New Member
Joined
Nov 7, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
I am having trouble with this VBA code. I have a reset button that selects 20 random numbers between 1 and 10. Then I have a search button that is supposed to list all the numbers you want to search, but my code is only listing the last row my number that i asked for is found and not listing all the rows the number is found in. How do I code for all the rows the number searched for us listed and output on the excel sheet?
VBA Code:
[
Dim num As Integer
Dim i As Integer
Dim location As Integer

num = InputBox("Enter a number to search.")
location = 0
For i = 1 To 10
    If Cells(i, 1) = num Then
        location = i
    End If
Next i

If location = 0 Then
    MsgBox (num & " is not listed.")
Else
    MsgBox (num & " is in row " & location & ".")
End If

/CODE]
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Use your macro with these changes:
VBA Code:
Option Explicit
Sub test()
    Dim num As Integer
    Dim i As Integer
    Dim location As String                        '<-changed
    num = InputBox("Enter a number to search.")
    location = ""                                 '<-changed
    For i = 1 To 10
        If Cells(i, 1) = num Then location = location & " | " & CStr(i) '<-changed
    Next i
    If location = "" Then                         '<-changed
        MsgBox (num & " is not listed.")
    Else
        MsgBox (num & " is in row " & location & ".") '<-changed
    End If
End Sub
 
Upvote 0
Solution
Try this:
VBA Code:
Option Base 1
Sub FindNumber()
Dim num As Integer
Dim i As Integer, x As Integer
Dim location(), numRows As String

num = InputBox("Enter a number to search.")

For i = 1 To 10
    If Cells(i, 1) = num Then
        ReDim Preserve location(x)
        location(x) = i
        x = x + 1
        
    End If
Next i
On Error Resume Next
If UBound(location) = 0 Then
    MsgBox (num & " is not listed.")
Else
    For i = 0 To UBound(location)
        numRows = numRows & location(i) & ", "
    Next i
    numRows = Left(numRows, Len(numRows) - 2)
    MsgBox (num & " is in row " & numRows & ".")
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top