Open File and Reference Workbook

abulkhairi

New Member
Joined
Mar 5, 2018
Messages
1
I have a code that searching for values and if match is found overwriting existing values, if not match found adding new records. The code does what I need except of one thing.

I want to replace Sheet2 reference with an external user-selected workbook. I presume that I have to use Application.FileDialog(msoFileDialogFilePicker) to do this.

Any thoughts are welcome.

Code:
[COLOR=#333333]Option Explicit[/COLOR]<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Sub Replace_From_List()
Dim cell As Range
Dim rngFind As Range
Dim Found As Range
Dim counter As Long
Dim lastrow As Long

    With ActiveWorkbook.Sheets("Sheet2")
        Set rngFind = .Range("B2", .Range("B" & Rows.Count).End(xlUp))
    End With
    
    For Each cell In rngFind
        Set Found = ActiveWorkbook.Sheets("Sheet1").Range("B2:B10000").Find(What:=cell.Value, _
                                                       LookIn:=xlValues, _
                                                       LookAt:=xlPart, _
                                                       MatchCase:=False)
                                                       
        If Not Found Is Nothing Then
            Found.Offset(, 1).Value = cell.Offset(, 2).Value
            Found.Offset(, 2).Value = cell.Offset(, 3).Value
            Found.Offset(, 3).Value = cell.Offset(, 4).Value
            Found.Offset(, 4).Value = cell.Offset(, 5).Value
            counter = counter + 1
        End If
        If Found Is Nothing Then
        lastrow = ActiveWorkbook.Sheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Row
            Cells(lastrow + 1, 1).Value = lastrow
            Cells(lastrow + 1, 2).Value = cell.Offset(0, 0).Value
            Cells(lastrow + 1, 3).Value = cell.Offset(0, 2).Value
            Cells(lastrow + 1, 4).Value = cell.Offset(0, 3).Value
            Cells(lastrow + 1, 5).Value = cell.Offset(0, 4).Value
            Cells(lastrow + 1, 6).Value = cell.Offset(0, 5).Value
            Cells(lastrow + 1, 7).Value = cell.Offset(0, 6).Value
        End If </code>[COLOR=#333333]    Next cell[/COLOR]
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Cross posted https://www.excelforum.com/excel-pr...-file-and-reference-workbook.html#post4857230

Cross-Posting
While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

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