last day (14 days later) » 

12:31 PM
0
A: setting a column format as text using vba

Siddharth Rout I would like to keep the format of column "E" as text Change xSWb.Sheets(1).Range("E1").NumberFormat = "@" to xSWb.Sheets(1).Columns(5).NumberFormat = "@" Also with the way you are copying, you will overwrite the formatting. you need to do .PasteSpecial xlPasteValues Is this what y...

 
acr
thanks its changing the format as text, but still its not pasting the way I wanted, for example 00060, pasting as 60. also I am getting a warning saying large amount of data in clipboard
 
Regarding large amount of data in clipboard add one line Application.CutCopyMode = False before xWb.Close False And for the other it shouldn't be doing that.
What happens if instead of Workbooks.OpenXML you use Workbooks.Open?
 
acr
No change if I put Workbooks.Open
 
Can I see the excel files?
 
acr
E column has hexadecimal values. For any cell which has any letter from A to F its works fine. if the content has only digit, it is removing prefix zeros
 
12:35 PM
Hi
few questions...
 
acr
here is the sample of the xml
 
1. do all xml files have same number of columns?
 
acr
yes
 
and what is that? Column range?
 
acr
yes
 
12:37 PM
I mean what is that column range? A to B? A to K?
 
acr
it will be A to AK
 
ok hold on
Try this
xSWb.Sheets(1).Columns(5).NumberFormat = "@"

Dim lRow As Long, rng As Range

Do While xFile <> ""
    Set xWb = Workbooks.OpenXML(xStrPath & "\" & xFile)

    With xWb.Sheets(1)
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        Set rng = Range("A1:AK" & lRow)
    End With

    With xSWb.Sheets(1)
        xCount = .Range("A" & .Rows.Count).End(xlUp).Row + 2

        .Range("A" & xCount & ":AK" & xCount).Value = rng.Value
    End With

    xWb.Close False
    DoEvents

    xFile = Dir()
 
acr
i thinks its clearing all data..dont see anything in sheet..
 
Do all columns have Same data. Especially Col A?
 
acr
no
it will change
 
12:43 PM
Ah! then we will have to find the last row in another way.. one moment
Try this
xSWb.Sheets(1).Columns(5).NumberFormat = "@"
Dim lRow As Long, rng As Range
Do While xFile <> ""
    Set xWb = Workbooks.OpenXML(xStrPath & "\" & xFile)
    With xWb.Sheets(1)
        lRow = .Cells.Find(What:="*", After:=.Range("A1"), _
                Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
                SearchDirection:=xlPrevious, MatchCase:=False).Row
        Set rng = Range("A1:AK" & lRow)
    End With
    With xSWb.Sheets(1)
        xCount = .Cells.Find(What:="*", After:=.Range("A1"), _
Sorry... my bad.. one moment
.Range("A" & xCount & ":AK" & xCount).Value = rng.Value
should be
.Range("A" & xCount & ":AK" & xCount + rng.Rows.Count - 1).Value = rng.Value
 
acr
ok, let me try
 
The thing is I cant test so I am just writing and sharing the code:D
 
acr
i think it has some error.
Sub Import_XML_Files()
Dim xWb As Workbook
Dim xSWb As Workbook
Dim xStrPath As String
Dim xFileDialog As FileDialog
Dim xFile As String
Dim xCount As Long
On Error GoTo ErrHandler
Worksheets("RawData").Activate
MsgBox "Select the folder where you have copied XML files"
If WorksheetFunction.CountA(Cells) = 0 Then
Else
MsgBox "Found some XML data is already imported.This old data will be ersased and will do a fresh XML import"
Sheets("RawData").UsedRange.ClearContents

End If
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
This is my entire code and showing up the error handler
 
One moment. creating a sample to test the code
Ok tested the code. there was a typo. try this
xSWb.Sheets(1).Columns(5).NumberFormat = "@"
Dim lRow As Long, rng As Range
Do While xFile <> ""
    Set xWb = Workbooks.OpenXML(xStrPath & "\" & xFile)
    With xWb.Sheets(1)
        lRow = .Cells.Find(What:="*", After:=.Range("A1"), _
                Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
                SearchDirection:=xlPrevious, MatchCase:=False).Row
        Set rng = .Range("A1:AK" & lRow)
    End With
    With xSWb.Sheets(1)
        xCount = .Cells.Find(What:="*", After:=.Range("A1"), _
 
acr
ok
still its going to error handler for me.
i am trying with the xml file updated above
 
12:59 PM
Comment the line On Error GoTo ErrHandler and see which line is giving you the error
 
acr
    xCount = .Cells.Find(What:="*", After:=.Range("A1"), _
            Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
            SearchDirection:=xlPrevious, MatchCase:=False).Row + 2
this is ths sample xml which I an using now
 
strange. That line works for me
Can you zip few xml files and upload it to a free file sharing site and share the link here?
 
acr
sure
 
Also paste the complete code here so I can see it
Hey Can I work on thin in 2 hours? I am already late for the gym :P
 
acr
sure, thank you very much for your help
 
1:07 PM
so 2 things before i go... 1. Paste complete code here 2. Share the link of xml files :)
catch up with you in 2 hours after the gym
 
acr
here is the link
I have added 2 xmls and clone copy of my excel. code is included in the excel
there is a change in the column its column C instead of E. I have updated the value from 5 to 3 in the code
and I suppose to get 000F0 and 00010 in C columns
 
 
2 hours later…
3:45 PM
Ok I am back
@acr
 
3:56 PM
I understood what the problem is :) When you are opening the file using Workbooks.OpenXML(xStrPath & "\" & xFile), the formatting is already lost. There is nothing wrong with my copy code. Let me think of a better way to read the file
 

last day (14 days later) »