I have code that will open a closed workbook, copy whole data from Worksheet "SM" and paste as values into target workbook "Paste SM" worksheet.
The data comes most of the time with some columns merged. After it has been copied and pasted as values, all merged cells are unmerged and there are some empty rows that need to be filled with duplicates.
Since I do not always receive documents with cells merged the code needs to work within scenarios:
- Source Worksheet include merged cell through columns.
EX A1:A11 with value "QWERTY", A12:A23 with value "12345" etc.
Copied and paste as values - all unmerged - A1 - "QWERTY" A2-A11 blanks no value, A12 - "12345" A13-A23 blanks no value.
All blank cells to fill with duplicates, check whole worksheet. - Source Worksheet contains unmerged and merged cells.
Merged cells - same logic as for point 1.
Unmerged cells would look like A1 - "QWERTY" A2-A11 blanks no value, A12 - "12345" A13-A23 blanks no value in source worksheet.
It will remain same after copy and paste as values.
All blank cells to fill with duplicates, check whole worksheet.
Option Explicit
Public Sub Import_SM_DataFromAnotherWorkbook_and_Paste_As_Values()
' Get workbook...
Dim targetWorkbook As Workbook
Set targetWorkbook = Application.ThisWorkbook
' get the customer workbook
Dim Filter As String
Filter = "Text files (*.xlsb),*.xlsb,(*.xlsx),*.xlsx"
Dim Caption As String
Caption = "Please Select an input file "
Dim Ret As Variant
Ret = Application.GetOpenFilename(Filter, , Caption)
If VarType(Ret) = vbBoolean And Ret = False Then Exit Sub
Dim wb As Workbook
Set wb = Workbooks.Open(Ret)
'It will open source woorkbook, copy whole sheet and paste as values into current workbook. Will
close sourcebook afterwards
targetWorkbook.Worksheets("Paste SM").Range("A1").Resize(wb.Worksheets("Security Matrix").UsedRange.Rows.Count, wb.Worksheets("Security Matrix").UsedRange.Columns.Count).Value = wb.Worksheets("Security Matrix").UsedRange.Value
'close opened workbook without saving
wb.Close SaveChanges:=False
End Sub