problems about range.findnext method

Elainaz

New Member
Joined
Apr 8, 2019
Messages
6
Hi all,
I'm new to vba and trying to use it dealing the following task that confusing me:
I have a series code number and want to search them in this table.
columnACode NumbercolumnBcolumnC
information1aaainformation1information1
information2bbbinformation2information2
information3aaainformation3information3
information4cccinformation4information4
information5dddinformation5information5
information6bbbinformation6information6

<tbody>
</tbody>

As you may see, the code numbers might appear more than once. I need it to return every row the target code number's in and make a new table in another worksheet.
Hope that I described this problem clearly because I'm not good at English actually.
I'm using "for each" and "range.findnext" to write the vba and here is the code I write:
Rich (BB code):
Sub fundsearching()
Dim rng2 As Range, rng3 As Range, fdrng As Range, row1 As Long, firstaddress As String
For Each rng3 In Sheet3.Range("A2:A59")    'a list of code numbers I need to look for
    Set rng2 = Sheet2.Range("B2:B7383")     'the database where all the information are, just like the table above
    Set fdrng = rng2.Find(rng3.Value, , xlValues, xlWhole)
    If Not fdrng Is Nothing Then
        Set firstaddress = fdrng.Address         'the system pops out an error here
        Do
            row1 = Sheet3.[D1].Value       'I used a counta function here in this cell counting the numbers of non-blank rows in the new table, so that I could write new information on the first blank row. I used [A62200].end(xlUP) but it didn't work out so I used this stupid way.
            Sheet1.Cells(row1, "A").Value = rng3.Value
            Sheet1.Cells(row1, "B").Value = Sheet2.Cells(fdrng.Row, "C").Value
            Sheet1.Cells(row1, "C").Value = Sheet2.Cells(fdrng.Row, "A").Value
            Sheet1.Range("D" & row1 & ":M" & row1).Value = Sheet2.Range("D" & fdrng.Row & ":M" & fdrng.Row).Value
            Set fdrng = rng2.FindNext(fdrng)
        Loop While fdrng.Address <> firstaddress
    End If
Next rng3
End Sub

Could anyone help me fixing this please? I'd really appreciate that.
 
Last edited by a moderator:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
You dont Set a string so just:

Code:
firstaddress = fdrng.Address
 
Upvote 0
Set is used with objects like sheets, ranges, shapes, tables etc
variable firstaddress is expecting a string not an object

Set is not required
Code:
firstaddress = fdrng.Address
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

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