Home |
Search |
Today's Posts |
#14
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Try commenting out the line Set xmlDom = Nothing and see if the problem still
exist. I would think a firewall or a virsus protection software package would behave like this and not a software package like excel/vba. I suspect that setting an object to nothing may have potential problems and is a more likely cause. If it does solve the problem I would notify microsoft because this is a problem that definiely need to be fixed. "Virgil" wrote: The final for others is below and works a treat. HOWEVER i have been driven crazy by Excel or the VBA IDE DELETING lines on me... Base file is a DBF file and macro lives in an XLS file. Every time i would restart the Set xmlDom = New MSXML2.DOMDocument line was removed. I went to modify a MsgBox and several of the MsgBox's where deleted. Other code lines were randomly deleted between runs. I am using Excel 2007 and perhaps this has issues ... as i used to use Excel 2003. Cheers ================= ' ' This function will read an XML file which has DBF updates and ' update the records in the current Sheet with this data. ' This was designed for Trends but could be used for other DBFs ' The column name to identify the row is coded to be "NAME" ' Sub Excel_Row_Updates() Dim RowCount As Long, ColCount As Long, RowUpdateCount As Long ' Add a reference to Microsoft XML, v6.0 Dim xmlFile As Variant Dim xmlrow As Variant Dim xmlcol As Variant Dim NameHeader As Variant Dim NameCol As Variant Dim LastRow As Variant Dim c As Variant Dim NameXmlCol As Long Dim NameDbfRow As Long Dim NameDbfCol As Long Dim FieldUpdates As Long Dim xmlDom As MSXML2.DOMDocument Set xmlDom = New MSXML2.DOMDocument FieldUpdates = 0 RowCount = 0 RowUpdateCount = 0 xmlFile = Application.GetOpenFilename("Select XML file (*.xml), *.xml") If xmlFile < False Then xmlDom.load (xmlFile) ' We assume that the macro is run from the main Excel page With ActiveSheet Debug.Print ActiveSheet.Name ' Find the column name is in Set NameHeader = .Rows(1).Find(what:="NAME", _ LookIn:=xlValues, lookat:=xlWhole) If NameHeader Is Nothing Then MsgBox ("Error : Cannot find NAME in header row") Set xmlDom = Nothing Exit Sub Else NameCol = NameHeader.Column End If For Each xmlrow In xmlDom.documentElement.childNodes RowCount = RowCount + 1 ' Find the NAME value for each XML record set For Each xmlcol In xmlrow.childNodes If xmlcol.tagName = "NAME" Then Exit For End If Next xmlcol ' Find the row that the NAME is in in the DBF / EXCEL NameDbfRow = 0 Set c = .Columns(NameCol).Find(what:=xmlcol.Text, _ LookIn:=xlValues, lookat:=xlWhole) If c Is Nothing Then GoTo Continue ' Row does not exist, consider not fatal as sheet maybe a subset Else ' We know the row the "NAME" item value is in NameDbfRow = c.Row RowUpdateCount = RowUpdateCount + 1 End If ' OK we have found the row the data is in so lets update the other columns For Each xmlcol In xmlrow.childNodes If xmlcol.tagName < "NAME" Then ' Update the DBF row with the value for this name Set c = .Rows(1).Find(what:=xmlcol.tagName, _ LookIn:=xlValues, lookat:=xlWhole) If c Is Nothing Then ' Although the row does not have to exist, if the row does exist then ' the columns must exist. MsgBox (xmlcol.tagName & " does not exist in the DBF file.") GoTo Abort Else 'Field column found in DBF record NameDbfCol = c.Column If NameDbfRow 1 Then ' Security check, has to be 1 .Cells(NameDbfRow, NameDbfCol) = xmlcol.Text FieldUpdates = FieldUpdates + 1 End If End If End If Next xmlcol Continue: Next xmlrow End With MsgBox (RowUpdateCount & " rows updated from " & RowCount & " records, please save file.") Abort: Set xmlDom = Nothing Else MsgBox ("Aborting macro as no XML file was slected") End If End Sub |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
import from excel to access with macro? | Excel Programming | |||
Excel Import Macro | Excel Discussion (Misc queries) | |||
Import Macro in PERSONAL.XLS will not import to my main document | Excel Programming | |||
Import Data Excel Macro | Excel Discussion (Misc queries) | |||
Use / Import Win32API in Excel macro | Excel Programming |