Col B: name Col A: number

gsx

New Member
Joined
Nov 28, 2009
Messages
13
Dear all<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
I have a code to search range A1:B250, where a user is given the opportunity to search among 250 foundations. The name of the foundation is located in column B, its number in column A. They often contain similar words (letters) and search often return several suggestions. My variable NewRange present the hits, which is forwarded to a ListBox where the user can select the correct one.
My question is if someone can help me to adjust my macro to return not only the name but the corresponding number as well (same row, the cell to left).<o:p></o:p>
Rich (BB code):
Sub Copy_To_Another_Range() 'http://www.rondebruin.nl/find.htm (adjusted)
Rich (BB code):
Rich (BB code):
Dim FirstAddress As String
Dim StrPrompt As String
Dim MyArr As Variant
Dim rng As Range
Dim Rcount As Long
Dim i As Long
Dim NewRange As Range
Rich (BB code):
With Application
   .ScreenUpdating = False
   .EnableEvents = False
End With
 
MyArr = Array(InputBox(StrPrompt))
Set NewRange = Sheets("SUMMER konto").Range("M10")
 
With Sheets("LISTE LEGATER").Range("A1:B300")<o:p></o:p>
   Rcount = 0<o:p></o:p>
   For i = LBound(MyArr) To UBound(MyArr)<o:p></o:p>
       Set rng = .Find(what:=MyArr(i), _
                       After:=.Cells(.Cells.Count), _
                       LookIn:=xlFormulas, _
                       lookat:=xlPart, _
                       SearchOrder:=xlByRows, _
                       SearchDirection:=xlNext, _
                       MatchCase:=False)
       If Not rng Is Nothing Then
           FirstAddress = rng.Address
 
           Do
               Rcount = Rcount + 1
 
               rng.Copy NewRange.Range("A" & Rcount)<o:p></o:p>
               Set rng = .FindNext(rng)
 
           Loop While Not rng Is Nothing And rng.Address <> FirstAddress
       End If
   Next i
End With<o:p></o:p>
With Application
   .ScreenUpdating = True
   .EnableEvents = True
End With
Call UserForm
End Sub
<o:p></o:p>
Best regards
Geir<o:p></o:p>
 
Last edited:

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
but unfortunately from current active window (my active window in code is ("LISTE LEGATER")
You can put a full stop (period) before
Cells
to make it
.Cells
to ensure the data comes from the sheet you're searching.
 
Upvote 0
Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
thanks to you, my code is now complete.
it's a very good code for searching. Beside being fast it will give a helpful result even when the user don't know the exact spelling.
I have built a listbox which populate the result, the user then can select from (to produse a trial balance). <o:p></o:p>

With gratitude
Geir<o:p></o:p>

Rich (BB code):
Rich (BB code):
Rich (BB code):
 Sub Copy_To_Another_Range() 'http://www.rondebruin.nl/find.htm<o:p></o:p>
    Dim FirstAddress As String<o:p></o:p>
    Dim StrPrompt As String<o:p></o:p>
    Dim MyArr As Variant<o:p></o:p>
    Dim rng As Range<o:p></o:p>
    Dim Rcount As Long<o:p></o:p>
    Dim i As Long<o:p></o:p>
    Dim NewRange As Range<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
    With Application<o:p></o:p>
        .ScreenUpdating = False<o:p></o:p>
        .EnableEvents = False<o:p></o:p>
    End With<o:p></o:p>
    <o:p></o:p>
    MyArr = Array(InputBox(StrPrompt))<o:p></o:p>
    Set NewRange = Sheets("SUMMER konto").Range("M10")<o:p></o:p>
    <o:p></o:p>
    With Sheets("LISTE LEGATER").Range("A1:B300")<o:p></o:p>
<o:p> </o:p>
        Rcount = 0<o:p></o:p>
<o:p> </o:p>
        For i = LBound(MyArr) To UBound(MyArr)<o:p></o:p>
<o:p> </o:p>
            Set rng = .Find(what:=MyArr(i), _<o:p></o:p>
                            After:=.Cells(.Cells.Count), _<o:p></o:p>
                            LookIn:=xlFormulas, _<o:p></o:p>
                            lookat:=xlPart, _<o:p></o:p>
                            SearchOrder:=xlByRows, _<o:p></o:p>
                            SearchDirection:=xlNext, _<o:p></o:p>
                            MatchCase:=False)<o:p></o:p>
            If Not rng Is Nothing Then<o:p></o:p>
                FirstAddress = rng.Address<o:p></o:p>
            <o:p></o:p>
                Do<o:p></o:p>
                    Rcount = Rcount + 1<o:p></o:p>
                    <o:p></o:p>
                    'gives the number<o:p></o:p>
                    rng.Offset(, -1).Copy NewRange.Range("A" & Rcount).Offset(, -1)<o:p></o:p>
                    'the name<o:p></o:p>
                    rng.Copy NewRange.Range("A" & Rcount)<o:p></o:p>
                <o:p></o:p>
                    Set rng = .FindNext(rng)<o:p></o:p>
                    <o:p></o:p>
                Loop While Not rng Is Nothing And rng.Address <> FirstAddress<o:p></o:p>
            End If<o:p></o:p>
        Next i<o:p></o:p>
    End With<o:p></o:p>
<o:p> </o:p>
    With Application<o:p></o:p>
        .ScreenUpdating = True<o:p></o:p>
        .EnableEvents = True<o:p></o:p>
    End With<o:p></o:p>
End Sub
<o:p></o:p>
 
Upvote 0
I know
"complete" was a wrong word to use. I will certainly, as best as I can, construct the search to prevent a crash
best regards
 
Upvote 0

Forum statistics

Threads
1,224,584
Messages
6,179,691
Members
452,938
Latest member
babeneker

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