VBA Code to Keep Formatting

Glasgowsmile

Active Member
Joined
Apr 14, 2018
Messages
280
Office Version
  1. 365
Platform
  1. Windows
Good Morning,

I've got the following code below to pull data from another spreadsheet. It works fine, no issues but I want the data to look exactly the same in the current workbook I'm using when I pull the data over. How do I keep the formatting consistent so it doesn't expand columns/rows etc?

Code:
Sub PastPDI()   Dim wkbCrntWorkBook As Workbook
   Dim wkbSourceBook As Workbook
   Set wkbCrntWorkBook = ActiveWorkbook
   With Application.FileDialog(msoFileDialogOpen)
      .Filters.Clear
      .Filters.Add "Excel 2007-13", "*.xlsx; *.xls; *.xlsm; *.xlsa"
      .AllowMultiSelect = False
      .Show
      If .SelectedItems.Count > 0 Then
         Workbooks.Open .SelectedItems(1)
         Set wkbSourceBook = ActiveWorkbook
         Sheets("Property Segment Data").Range("A1:Z40").Copy wkbCrntWorkBook.Sheets("Sheet1").Range("A1")
         wkbCrntWorkBook.Sheets("Sheet1").Range("A1").CurrentRegion.EntireColumn.AutoFit
         wkbSourceBook.Close False
      End If
   End With
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Additionally, I discovered this is copying the Formula from the other sheet instead of just the Data. How do I keep the formatting and copy just the data (not the formulas)?
 
Upvote 0
Based on what I've read, I need to add PasteSpecial to this code but where should I add it?
 
Upvote 0
So you want to copy values and formats, but not formulas?

Try this.
Code:
Sheets("Property Segment Data").Range("A1:Z40").Copy 

wkbCrntWorkBook.Sheets("Sheet1").Range("A1").PasteSpecial  xlPasteValues
wkbCrntWorkBook.Sheets("Sheet1").Range("A1").PasteSpecial xlPasteFormats

PS If it's only number formats you want to retain this might work.
Code:
wkbCrntWorkBook.Sheets("Sheet1").Range("A1").PasteSpecial xlPasteValuesAndNumberFormats
 
Upvote 0
How would I change the height and width of rows and columns after I import the data?

I tried adding this column one to the bottom but doesn't seem to work.

Code:
Sub PastPDI()

   Sheets("Sheet1").Cells.ClearContents
   Dim wkbCrntWorkBook As Workbook
   Dim wkbSourceBook As Workbook
   Set wkbCrntWorkBook = ActiveWorkbook
   With Application.FileDialog(msoFileDialogOpen)
      .Filters.Clear
      .Filters.Add "Excel 2007-13", "*.xlsx; *.xls; *.xlsm; *.xlsa"
      .AllowMultiSelect = False
      .Show
      If .SelectedItems.Count > 0 Then
         Workbooks.Open .SelectedItems(1)
         Set wkbSourceBook = ActiveWorkbook
         Sheets("Property Segment Data").Range("B2:Z40").Copy
         wkbCrntWorkBook.Sheets("Sheet1").Range("A1").PasteSpecial xlPasteValues
         wkbCrntWorkBook.Sheets("Sheet1").Range("A1").PasteSpecial xlPasteFormats
         wkbSourceBook.Close False
      End If
   End With
   
   Columns("B").ColumnWidth = 23
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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