|
代码全文如下:
- ' This code try to connect WPS ET,if failed then connect to Excel
- Dim ExcelApp
- On Error Resume Next
- ' try to connect to et or excel
- Set ExcelApp = GetObject(, "Excel.Application")
- If ExcelApp Is Nothing Then
- Set ExcelApp = GetObject(, "KET.Application")
- If ExcelApp Is Nothing Then
- Set ExcelApp = GetObject(, "ET.Application")
- If ExcelApp Is Nothing Then
- MsgBox "Run Excel or Kingsoft ET first.", vbInformation, "Information"
- WScript.Quit
- End If
- End If
- End If
- On Error Goto 0
- Dim Workbook, ActiveSheet
- Set Workbook = ExcelApp.ActiveWorkbook
- Set ActiveSheet = Workbook.ActiveSheet
- Dim MaxRow, MaxCol
- MaxRow = ActiveSheet.UsedRange.Row + ActiveSheet.UsedRange.Rows.Count-1
- MaxCol = ActiveSheet.UsedRange.Column + ActiveSheet.UsedRange.Columns.Count-1
- Dim Dict
- Set Dict = CreateObject("Scripting.Dictionary")
- Dim key, r
- For r = 2 To MaxRow
- key = ActiveSheet.Cells(r,1).value
- If dict.Exists(key) Then
- dict(key)=dict(key) & ";" & ActiveSheet.Cells(r,2).Value
- Else
- dict.Add key, ActiveSheet.Cells(r,2).Value
- End If
- Next
- Dim FileName, WB, IDs
- For Each FileName In dict.keys
- Set WB=ExcelApp.Workbooks.Add
- IDs = Split(dict(FileName),";")
- For r = 1 To UBound(IDs)+1
- wb.activesheet.cells(r,1)=IDs(r-1)
- Next
- WB.SaveAs workbook.path & "" & filename & ".xlsx"
- WB.Close false
- Next
复制代码 |
|