VBA - Import Macro: Retain Existing File Formatting

foolishpiano

New Member
Joined
Aug 19, 2016
Messages
28
Hi Everyone!

I have an import macro that I would like some help with, please. I would like to retain the formatting of the cell(s) and text(s) from my old workbook into the new workbook I am importing into. Is there a way to do this?

The code I am working with is:

Code:
Sub Import()
' Turn Screen Updating Off
    Application.ScreenUpdating = False
'make cell N1 formatted as Text
    Range("N1").NumberFormat = "@"
    
' Get customer workbook...
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook

' make weak assumption that active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook
' get the old workbook
filter = "Excel Workbooks (*.xls),*.xls"
caption = "Please Select an Input File "
On Error GoTo Canceled
    ChDrive "U:"
    ChDrive "U:\Compliance\Files In Process\" & Range("bx73") & "\"
customerFilename = Application.GetOpenFilename(filter, , caption)
Set customerWorkbook = Application.Workbooks.Open(customerFilename)
 
' assume range is A1 - AI33 in sheet1
' copy data from customer to target workbook
Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(1)
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)
targetSheet.Range("N1", "N1").Value = sourceSheet.Range("N1", "N1").Value
targetSheet.Range("W1", "W1").Value = sourceSheet.Range("W1", "W1").Value
targetSheet.Range("F5", "F5").Value = sourceSheet.Range("F5", "F5").Value
targetSheet.Range("V5", "AD7").Value = sourceSheet.Range("V5", "AD7").Value
targetSheet.Range("AI5", "AI7").Value = sourceSheet.Range("AI5", "AI7").Value
targetSheet.Range("F10", "F16").Value = sourceSheet.Range("F10", "F16").Value
targetSheet.Range("N12", "N13").Value = sourceSheet.Range("N12", "N13").Value
targetSheet.Range("N15", "N15").Value = sourceSheet.Range("N15", "N15").Value
targetSheet.Range("T10", "AD16").Value = sourceSheet.Range("T10", "AD16").Value
targetSheet.Range("S17", "S17").Value = sourceSheet.Range("S17", "S17").Value
targetSheet.Range("A23", "H23").Value = sourceSheet.Range("A23", "H23").Value
targetSheet.Range("A25", "H25").Value = sourceSheet.Range("A25", "H25").Value
targetSheet.Range("A27", "H27").Value = sourceSheet.Range("A27", "H27").Value
targetSheet.Range("A29", "H29").Value = sourceSheet.Range("A29", "H29").Value
targetSheet.Range("A31", "H31").Value = sourceSheet.Range("A31", "H31").Value
targetSheet.Range("A33", "H33").Value = sourceSheet.Range("A33", "H33").Value
' Save and Close old workbook
customerWorkbook.Save
customerWorkbook.Close
'Turn Screen Updating On
    Application.ScreenUpdating = True
    ActiveWindow.WindowState = xlMaximized
    
Canceled:
End Sub

I'm not sure if something like this is possible or not, but I'm willing to bet that someone here does.

Thank you all for your help!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Try like this
Code:
sourceSheet.Range("N1").Copy targetSheet.Range("N1")
sourceSheet.Range("W1").Copy targetSheet.Range("W1")
sourceSheet.Range("F5").Copy targetSheet.Range("F5")
sourceSheet.Range("V5:AD7").Copy targetSheet.Range("V5:AD7")
 
Upvote 0
You probably need to copy the cells rather than just the values:

Code:
sourceSheet.Range("N1", "N1").Copy targetSheet.Range("N1", "N1")
sourceSheet.Range("W1", "W1").Copy targetSheet.Range("W1", "W1")
sourceSheet.Range("F5", "F5").Copy targetSheet.Range("F5", "F5")
sourceSheet.Range("V5", "AD7").Copy targetSheet.Range("V5", "AD7")
sourceSheet.Range("AI5", "AI7").Copy targetSheet.Range("AI5", "AI7")
sourceSheet.Range("F10", "F16").Copy targetSheet.Range("F10", "F16")
sourceSheet.Range("N12", "N13").Copy targetSheet.Range("N12", "N13")
sourceSheet.Range("N15", "N15").Copy targetSheet.Range("N15", "N15")
sourceSheet.Range("T10", "AD16").Copy targetSheet.Range("T10", "AD16")
sourceSheet.Range("S17", "S17").Copy targetSheet.Range("S17", "S17")
sourceSheet.Range("A23", "H23").Copy targetSheet.Range("A23", "H23")
sourceSheet.Range("A25", "H25").Copy targetSheet.Range("A25", "H25")
sourceSheet.Range("A27", "H27").Copy targetSheet.Range("A27", "H27")
sourceSheet.Range("A29", "H29").Copy targetSheet.Range("A29", "H29")
sourceSheet.Range("A31", "H31").Copy targetSheet.Range("A31", "H31")
sourceSheet.Range("A33", "H33").Copy targetSheet.Range("A33", "H33")

WBD
 
Upvote 0
Try this:

Code:
targetSheet.Range("A33", "H33").Copy Destination:= sourceSheet.Range("A33")
 
Upvote 0
Thank you guys so much for your help! I now have:

Code:
Sub Import()
' Turn Screen Updating Off
    Application.ScreenUpdating = False
'make cell N1 formatted as Text
    Range("N1").NumberFormat = "@"
    
' Get customer workbook...
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook

' make weak assumption that active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook
' get the old workbook
filter = "Excel Workbooks (*.xls),*.xls"
caption = "Please Select an Input File "
On Error GoTo Canceled
    ChDrive "U:"
    ChDrive "U:\Compliance\Files In Process\" & Range("bx73") & "\"
customerFilename = Application.GetOpenFilename(filter, , caption)
Set customerWorkbook = Application.Workbooks.Open(customerFilename)
 
' assume range is A1 - AI33 in sheet1
' copy data from customer to target workbook
Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(1)
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)
sourceSheet.Range("N1").Copy targetSheet.Range("N1")
sourceSheet.Range("W1").Copy targetSheet.Range("W1")
sourceSheet.Range("F5").Copy targetSheet.Range("F5")
sourceSheet.Range("v5:AD7").Copy targetSheet.Range("V5:AD7")
sourceSheet.Range("AI5:AI17").Copy targetSheet.Range("AI5:AI17")
sourceSheet.Range("F10:F15").Copy targetSheet.Range("F10:F15")
sourceSheet.Range("N12:N13").Copy targetSheet.Range("N12:N13")
sourceSheet.Range("N15").Copy targetSheet.Range("N15")
sourceSheet.Range("T10:AD16").Copy targetSheet.Range("T10:AD16")
sourceSheet.Range("S17").Copy targetSheet.Range("S17")
sourceSheet.Range("A23:H23").Copy targetSheet.Range("A23:H23")
sourceSheet.Range("A25:H25").Copy targetSheet.Range("A25:H25")
sourceSheet.Range("A27:H27").Copy targetSheet.Range("A27:H27")
sourceSheet.Range("A29:H29").Copy targetSheet.Range("A29:H29")
sourceSheet.Range("A31:H31").Copy targetSheet.Range("A31:H31")
sourceSheet.Range("A33:H33").Copy targetSheet.Range("A33:H33")

' Save and Close old workbook
customerWorkbook.Save
customerWorkbook.Close
'Turn Screen Updating On
    Application.ScreenUpdating = True
    ActiveWindow.WindowState = xlMaximized
    
Canceled:
End Sub

But! Now no information is copied into my new workbook; the code just seems to open the old workbook. No information is being imported to the new workbook.

Any ideas as to why? I've also tried WBD's suggestion with:

Code:
sourceSheet.Range("N1", "N1").Copy targetSheet.Range("N1", "N1")
sourceSheet.Range("W1", "W1").Copy targetSheet.Range("W1", "W1")
sourceSheet.Range("F5", "F5").Copy targetSheet.Range("F5", "F5")
sourceSheet.Range("V5", "AD7").Copy targetSheet.Range("V5", "AD7")
sourceSheet.Range("AI5", "AI7").Copy targetSheet.Range("AI5", "AI7")
sourceSheet.Range("F10", "F16").Copy targetSheet.Range("F10", "F16")
sourceSheet.Range("N12", "N13").Copy targetSheet.Range("N12", "N13")
sourceSheet.Range("N15", "N15").Copy targetSheet.Range("N15", "N15")
sourceSheet.Range("T10", "AD16").Copy targetSheet.Range("T10", "AD16")
sourceSheet.Range("S17", "S17").Copy targetSheet.Range("S17", "S17")
sourceSheet.Range("A23", "H23").Copy targetSheet.Range("A23", "H23")
sourceSheet.Range("A25", "H25").Copy targetSheet.Range("A25", "H25")
sourceSheet.Range("A27", "H27").Copy targetSheet.Range("A27", "H27")
sourceSheet.Range("A29", "H29").Copy targetSheet.Range("A29", "H29")
sourceSheet.Range("A31", "H31").Copy targetSheet.Range("A31", "H31")
sourceSheet.Range("A33", "H33").Copy targetSheet.Range("A33", "H33")

However, the result was the same.

Thank you all for your help and ideas! I greatly appreciate it.
 
Upvote 0
What happens if you remove this line
Code:
On Error GoTo Canceled
 
Upvote 0
I get a runtime error '1004': we can't do that to a merged cell, then the
Code:
sourceSheet.Range("N1").Copy targetSheet.Range("N1")
is highlighted.

Most of these cells are merged cells, if that makes a difference. I apologize for neglecting to mention that earlier.
 
Upvote 0
Thank you, Fluff, for all of your input today. I did a test sheet and using the code you suggested:
Code:
sourceSheet.Range("N1").Copy targetSheet.Range("N1")
will produce the results I want. I think I'm having problems because of the merged cells. I'll have to redesign our current workbook to remove merged cells. Once I do that I should be able to import the formatting without any difficulty.

Thank you!
 
Upvote 0

Forum statistics

Threads
1,224,847
Messages
6,181,324
Members
453,032
Latest member
Pauh

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