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)
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)
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