Find and Copy VBA Modification

bobsburgers

Board Regular
Joined
Jun 25, 2017
Messages
60
Hi, all!

I'm working on a small inventory tracking system, and have run into a bug that I can't find a solution to.

The "Item Return" function (button activated VBA macro) searches the active worksheet for a string and, once found, copies the string from Column D to Column H.

The bug that I have run into is when we have multiple occurrences of the same string in the active worksheet. My current code will find all occurrences, and complete the function until each occurrence has been found and copied from D to H. However, based on our standard operating procedures, we need to check in each item by hand.

With that said, is there anyway to modify the code below (or perhaps find a new way to go about this function) so that in the event of multiple occurrences, rather than automatically filling each corresponding H cell, the function simply activates the cell with the next occurrence?

I was thinking maybe some sort of constraint where the function only works if the copy destination (column H) is empty; however, I haven't been able to implement that myself.

Here is the code, and I have provide screenshots, examples, and worksheets if that helps!

Code:
Sub Item_Return()


    Dim scanstring As String
    Dim foundscan As Range
    Dim ws As Worksheet
    Dim foundscan_address As String
    
Set ws = ActiveSheet


scanstring = InputBox("Please enter a value to search for", "Enter value")


With ws.Columns("D")
    
    Set foundscan = .Find(What:=scanstring, LookIn:=xlValues, LookAt:=xlWhole, _
                          SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                          MatchCase:=False, SearchFormat:=False)
                      
    If Not foundscan Is Nothing Then
foundscan_address = foundscan.Address

Do
        foundscan.Offset(0, 4).Value = scanstring
        ws.Activate
        foundscan.Activate
        ActiveWindow.ScrollRow = foundscan.Row

        Set foundscan = .FindNext(foundscan)

Loop While Not foundscan Is Nothing And foundscan.Address <> foundscan_address

    Else
        MsgBox scanstring & "  was not found"
    End If
    
End With

End Sub

Thanks for the help everyone!! :cool:

Best,

Bob
 
Change
Code:
foundscan.Offset(0, 4).Activate
to
Code:
foundscan.Offset(0, 4).Value = scanstring

Thank you so much NoSparks! With the small scale tests, this seems to be absolutely perfect!

Now time to implement it into the running prototype and see how it holds up! Thank you again so much!!

Best,

Bob
 
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hello all,

I am working on a piece of VBA script which can copy files with a certain string in the file name form one folder to other. For eg.: all files which have something like 'finance' in its name should be copied over to another folder. Please help me out!
 
Upvote 0
Greetings RZVPS and welcome to MrExcel :)

I would suggest you start your own thread, as it can get quite confusing for 'answerers' to be trying to answer multiple people in the same thread.

Hope that helps,

Mark
 
Upvote 0
Change
Code:
foundscan.Offset(0, 4).Activate
to
Code:
foundscan.Offset(0, 4).Value = scanstring

Hi, NoSparks!

Sorry, I thought I had replied - but this modification is perfect!

Time to start testing it on a larger scale - thank you again so much!!

Best,

Bob
 
Upvote 0

Forum statistics

Threads
1,224,832
Messages
6,181,234
Members
453,026
Latest member
cknader

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