Open workbook, paste then save matching values

Rymare

New Member
Joined
Apr 20, 2018
Messages
37
I have a workbook and sheet, in the code it's called sh_1 (or ThisWorkbook) and I'm trying to get the values from columns O - T to copy and paste into columns D-H in another workbook and sheet called sh_3 (or iWorkbook), IF the corresponding value in column B matches in each worksheet. Now I've got most of it down, however during the process it copies and pastes but sets sh_3 as read-only so the user can't save the changes made by the code unless they save it under a different name. I need it to copy, paste then save changes and close the workbook sh_3--I can't have multiple copies of the same sheet floating around.

How can I do this so that after it's done copy and pasting, the file (sh_3) saves and closes?
I checked and the sh_3 is not shared or protected.

Here's what it looks like (roughly--except my data isn't about books, it's just sensitive so I can't show it)
DHyaVz0.png



Code:
Sub SubmitToIansDumbWay()
    
Dim file_path As String
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")
file_path = FileSelectBox("*.xlsx")
xlApp.Visible = True
xlApp.Workbooks.Open FileName:=file_path




Application.ScreenUpdating = False
    Dim iWorkOrder As Workbook: Set iWorkOrder = Workbooks.Open(file_path)
    Dim AVals As New Dictionary
    Dim i As Long, j As Long, lastRow1 As Long, lastRow2 As Long
    Dim sh_1, sh_3 As Worksheet
    Dim MyName As String


    Set sh_1 = ThisWorkbook.Sheets("WOs")
    Set sh_3 = iWorkOrder.Sheets("Reviewed WOs")


    With sh_1
        lastRow1 = .Range("B:B").Rows.Count 'last row in spreadsheet
        lastRow1 = .Cells(lastRow1, 2).End(xlUp).Row 'last used row in column b
        'load the AVal dict
        For j = 2 To lastRow1
            MyName = .Cells(j, 2).Value
            If Len(MyName) > 0 Then AVals.Add MyName, .Cells(j, 2).Value
        Next j
    End With


    With sh_3
        lastRow2 = .Range("A:A").Rows.Count
        lastRow2 = .Cells(lastRow2, 2).End(xlUp).Row 'last used row in column b
        For i = 2 To lastRow2
            MyName = .Cells(i, 2).Value
            If AVals.Exists(MyName) Then
                .Cells(i, 4).Value = sh_1.Cells(i, 16)
                .Cells(i, 5).Value = sh_1.Cells(i, 17)
                .Cells(i, 6).Value = sh_1.Cells(i, 18)
                .Cells(i, 7).Value = sh_1.Cells(i, 19)
                .Cells(i, 8).Value = sh_1.Cells(i, 20)
            End If
         Next i
    End With
    
Application.ScreenUpdating = True
End Sub


Function FileSelectBox(ByRef FileType As String, Optional ByVal DefaultDir As String) As String
    Dim a As Object, FileName As String, varFile As Variant
    Set a = Application.FileDialog(msoFileDialogFilePicker)
    With a
        .AllowMultiSelect = False
        .Title = "Select File..."
        .Filters.Clear
        .Filters.Add "Excel Files", FileType
        If Not IsMissing(DefaultDir) And DefaultDir <> "" Then .InitialFileName = DefaultDir
        If .Show = True Then
            For Each varFile In .SelectedItems
                FileSelectBox = varFile
            Next varFile
        End If
    End With
End Function
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi there, to close and save a workbook, you could use something along the lines of:

Code:
iWorkOrder.Close True
 
Upvote 0
Hi there, to close and save a workbook, you could use something along the lines of:

Code:
iWorkOrder.Close True

That only lets me save a copy of iWorkOrder. It won't let me make the changes to the actual workbook and sheet. It opens it as read only, makes the changes then asks me to save a copy.
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,937
Members
449,196
Latest member
Maxkapoor

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