VBA Code to Keep Formatting

Glasgowsmile

Board Regular
Joined
Apr 14, 2018
Messages
152
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
 

Some videos you may like

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Glasgowsmile

Board Regular
Joined
Apr 14, 2018
Messages
152
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)?
 

Glasgowsmile

Board Regular
Joined
Apr 14, 2018
Messages
152
Based on what I've read, I need to add PasteSpecial to this code but where should I add it?
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,219
Office Version
  1. 365
Platform
  1. Windows
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
 

Glasgowsmile

Board Regular
Joined
Apr 14, 2018
Messages
152
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
 

Watch MrExcel Video

Forum statistics

Threads
1,123,135
Messages
5,599,917
Members
414,348
Latest member
KloppyM

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
Top