How to Avoid Auto Cell Formatting

madhuchelliah

Board Regular
Joined
Nov 22, 2017
Messages
226
Office Version
  1. 2019
Platform
  1. Windows
Hello guys, i am importing data from a XML file. The contents are consists of Alpha, numeric and symbols. After importing, the cell formats are showing as General. Some of my values are like Date and Time format. After pasting, Date like values changed to 8 digit number and some Time like values showing with AM or PM suffixes. I want to keep the values as it is. How to do that using macro? The code below shown is i am using for importing. Thank you.

Code:
Sub Import()
   Dim wb1 As Workbook
   Dim wb2 As Workbook
   Dim ws As Worksheet
   Dim PasteStart As Range
   Dim FileToOpen As String
   Set wb1 = ActiveWorkbook
   Set PasteStart = [Sheet2!C2]
   'Sheets("Sheet2").Cells.ClearContents
   Dim FileName As String
Dim folderPath As String
folderPath = "C:\"
FileName = folderPath & ThisWorkbook.Worksheets("Sheet1").Range("E4").Value & ".xml"
  FileToOpen = FileName _
   Set wb2 = Workbooks.Open(FileName:=FileToOpen)
   For Each ws In wb2.Worksheets
      If ws.Range("E1") = "" Then ws.Columns("E").Delete
      With ws.UsedRange
         .Copy PasteStart
         Set PasteStart = PasteStart.Offset(.Rows.Count)
      End With
   Next ws
   wb2.Close False
End Sub
 
Last edited:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try:
Code:
Sub Import()
    Dim wb1 As Workbook
    Dim wb2 As Workbook
    Dim ws As Worksheet
    Dim PasteStart As Range
    Dim FileToOpen As String
    Set wb1 = ActiveWorkbook
    Set PasteStart = [Sheet2!C2]
    'Sheets("Sheet2").Cells.ClearContents
    Dim FileName As String
    Dim folderPath As String
    folderPath = "C:\"
    FileName = folderPath & ThisWorkbook.Worksheets("Sheet1").Range("E4").Value & ".xml"
    FileToOpen = FileName
    Set wb2 = Workbooks.Open(FileName:=FileToOpen)
    For Each ws In wb2.Worksheets
        If ws.Range("E1") = "" Then ws.Columns("E").Delete
        With ws.UsedRange
            .Copy
            PasteStart.PasteSpecial xlPasteValues
            Set PasteStart = PasteStart.Offset(.Rows.Count)
        End With
   Next ws
   wb2.Close False
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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