VBA - Losing formats when pasting later in code

jardenp

Active Member
Joined
May 12, 2009
Messages
373
Office Version
  1. 2019
  2. 2016
  3. 2013
  4. 2011
  5. 2010
Platform
  1. Windows
I'm copying data from one book to another and I'm losing very important leading zeroes in column A when I put things between my copy and paste codes. It seems the formatting isn't carrying over, but it does when there is nothing between the copy and paste.

When I use this
Code:
'Copy data to Shared Macros.xlsb sheet[INDENT=2]Range("A2").Select[/INDENT]
                Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
                Selection.Copy
                ActiveWorkbook.Close[INDENT=2]
Workbooks.add
[/INDENT]
                ActiveSheet.Paste
I get the following on the new sheet (this is what I want because it's exactly like the original sheet)[TABLE="class: grid, width: 100"]
<tbody>[TR]
[TD]Unit[/TD]
[/TR]
[TR]
[TD]00012[/TD]
[/TR]
[TR]
[TD]00123[/TD]
[/TR]
[TR]
[TD]01234[/TD]
[/TR]
[TR]
[TD]12345[/TD]
[/TR]
</tbody>[/TABLE]

When I use my real code
Code:
'Copy data to Shared Macros.xlsb sheet[INDENT=2]Range("A2").Select[/INDENT]
                Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
                Selection.Copy
                ActiveWorkbook.Close
                
                'Test if Shared Macros.xlsb is open. If not, open it
                Dim TestWorkbook As Workbook
            
                Set TestWorkbook = Nothing
                On Error Resume Next
                Set TestWorkbook = Workbooks("Shared Macros.xlsb")
                On Error GoTo 0
            
                If TestWorkbook Is Nothing Then
            
                    Workbooks.Open Filename:="S:\PLI Misc\Macro Codes\Shared Macros.xlsb"
                    Windows("Shared Macros.xlsb").Activate
                    Sheets("Equipment Master").Select
                    Range("A1").Select
            
                Else
                    Windows("Shared Macros.xlsb").Activate
                    Sheets("Equipment Master").Select
                    Range("A1").Select
                End If
                
                'Paste data
                Dim AddRow As Long
                AddRow = Range("A1").End(xlDown).Row + 1
                
                Range("A" & AddRow).Select

                ActiveSheet.Paste
I get [TABLE="class: grid, width: 100"]
<tbody>[TR]
[TD]Unit[/TD]
[/TR]
[TR]
[TD]12[/TD]
[/TR]
[TR]
[TD]123[/TD]
[/TR]
[TR]
[TD]1234[/TD]
[/TR]
[TR]
[TD]12345[/TD]
[/TR]
</tbody>[/TABLE]

Any suggestions on how I can keep the formatting?

Thanks,

Josh in IN
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi Jardenp, make a COPY of your workbook and try replacing this line of code:

Code:
ActiveSheet.Paste

With these lines of code instead:

Code:
With ActiveSheet     
     .PasteSpecial xlPasteFormats
     .PasteSpecial xlPasteValues
End With
 
Upvote 0
Thanks for the reply, Fishboy. That throws an error:

Run Time Error '1004'
PasteSpecial method of WorkSheet class failed
 
Upvote 0
Hmm, I'll go back to the drawing board and have another think. Maybe someone else will swoop in and answer your query before I get back to you as I am not in the office much longer today so I apologise if I go quiet for a while.
 
Upvote 0

Forum statistics

Threads
1,224,606
Messages
6,179,866
Members
452,948
Latest member
UsmanAli786

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