2011-04-15 103 views

回答

1

這是我學會了如何用VBA,非常簡單和靈活的做...

檢查:

'lastCol = Range("a1").End(xlToRight).Column 

在撒哈拉沙漠kurssitToXML (Kurssi =芬蘭語課程):

Sub kurssitToXML() 
    Dim Filename As Variant 
    Dim Rng As Range 
    Dim r As Long, c As Long 
    Dim dRetVal As Variant 

    Worksheets("Kurssiluettelo").Activate 
    'Set the range 

    ' IS THIS WHAT YOU ARE LOOKING FOR? 
    'lastCol = Range("a1").End(xlToRight).Column 
    'lastRow = Cells(65536, lastCol).End(xlUp).Row 
    'Rng = Range("a1", Cells(lastRow, lastCol)) 

    lastCol = Range("a1").End(xlToRight).Column 
    lastRow = Range("a1").End(xlDown).Row 
    Set Rng = Range("a1", Cells(lastRow, lastCol)) 

    ' Get a file name 
    Filename = Application.GetSaveAsFilename(_ 
     InitialFileName:="d:\kurssit.xml", _ 
     fileFilter:="XML Files(*.xml), *.xml") 
    If Filename = False Then Exit Sub 

' Open the text file 
    Open Filename For Output As #1 

' Write the <xml> tags 
    Print #1, "<?xml version=""1.0"" encoding=""ISO-8859-1"" standalone=""yes""?>" 
    Print #1, "<KurssitList xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">" 

' Loop through the cells 
    For r = 2 To Rng.Rows.Count 
     Print #1, "<Kurssi>" 
     For c = 1 To Rng.Columns.Count 
      Print #1, "<" & Rng.Cells(1, c) & ">"; 
      If IsDate(Rng.Cells(r, c)) Then 
       Print #1, Format(Rng.Cells(r, c), "dd.mm.yyyy"); 
      Else 
       Print #1, Rng.Cells(r, c).Text; 
      End If 
      Print #1, "</" & Rng.Cells(1, c) & ">" 
     Next c 
     Print #1, "</Kurssi>" 
    Next r 
' Close the table 
    Print #1, "</KurssitList>" 

' Close the file 
    Close #1 
... 

我希望這會有所幫助。