PDA

View Full Version : [SOLVED:] Import most recent xml file



Welsyntoffie
04-25-2018, 03:39 AM
Good day all.

Code for importing one xml file with one sheet...



Sub ImportXMLtoList()
Dim strTargetFile As String
Dim wb as Workbook

Application.Screenupdating = False
Application.DisplayAlerts = False
strTargetFile = "C:\example.xml"
Set wb = Workbooks.OpenXML(Filename:=strTargetFile, LoadOption:=xlXmlLoadImportToList)
Application.DisplayAlerts = True

wb.Sheets(1).UsedRange.Copy ThisWorkbook.Sheets("Sheet2").Range("A1")
wb.Close False
Application.Screenupdating = True


End Sub

This is working great if the file name is always example. The xml files I want to import contantly changes its name to Hourlyreport_dd.mm.yy hh.mm.

I have found other code that searches for the latest file.


'Force the explicit delcaration of variables
Option Explicit

Sub OpenLatestFile()

'Declare the variables
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date

'Specify the path to the folder
MyPath = "C:\Users\Domenic\Documents\"

'Make sure that the path ends in a backslash
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"

'Get the first Excel file from the folder
MyFile = Dir(MyPath & "*.xml", vbNormal)

'If no files were found, exit the sub
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If

'Loop through each Excel file in the folder
Do While Len(MyFile) > 0

'Assign the date/time of the current file to a variable
LMD = FileDateTime(MyPath & MyFile)

'If the date/time of the current file is greater than the latest
'recorded date, assign its filename and date/time to variables
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If

'Get the next Excel file from the folder
MyFile = Dir

Loop

'Open the latest file
Workbooks.Open MyPath & LatestFile

End Sub

Problem with this is that it only opens the xml file.

How do I combine the code to search latest xml file and import sheet 1.used range to sheet 1 of workbook runniglng the macro.

I tried replacing workbooks.open MyPath & latestfile with the import code but I get error message.

I have been all over google and searched in forums.

Any help appriciated.

p45cal
04-25-2018, 04:08 AM
Combining the two and not paying attention to style:
'Force the explicit delcaration of variables
Option Explicit
Sub OpenLatestFile()
'Declare the variables
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
'Dim strTargetFile As String
Dim wb As Workbook
Dim LatestDate As Date
Dim LMD As Date
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'Specify the path to the folder
MyPath = "C:\Users\Domenic\Documents\"
'Make sure that the path ends in a backslash
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
'Get the first Excel file from the folder
MyFile = Dir(MyPath & "*.xml", vbNormal)
'If no files were found, exit the sub
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
'Loop through each Excel file in the folder
Do While Len(MyFile) > 0
'Assign the date/time of the current file to a variable
LMD = FileDateTime(MyPath & MyFile)
'If the date/time of the current file is greater than the latest
'recorded date, assign its filename and date/time to variables
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
'Get the next Excel file from the folder
MyFile = Dir
Loop
Set wb = Workbooks.OpenXML(Filename:=MyPath & LatestFile, LoadOption:=xlXmlLoadImportToList)
Application.DisplayAlerts = True
wb.Sheets(1).UsedRange.Copy ThisWorkbook.Sheets("Sheet2").Range("A1")
wb.Close False
Application.ScreenUpdating = True
End Sub
untested.

Welsyntoffie
04-25-2018, 04:35 AM
Thank you sooo much. I am not close to my pc now. Will test it tonight.
If this works I wll be sorted.

Welsyntoffie
04-25-2018, 05:11 AM
I could not wait.
Used my laptop and created the files like the scenario I have.

I noticed where I went wrong... line 38. I did not add & LatestFile.

With your help, my workbook is now doing what I wanted it to do for the past week.

Your combined code works.

Thanks