|
楼主 |
发表于 2015-12-15 11:07
|
显示全部楼层
改善了VBA版省市区三级联动,增加了将数据导入Access数据库的功能。VBA代码如下:
- <BLOCKQUOTE>
- <P>Private Sub UserForm_Initialize()
- On Error Resume Next
- Dim oxmlDoc As DOMDocument
- Dim Node As IXMLDOMNode
- Dim oXmlNodes As IXMLDOMNodeList
- Set oxmlDoc = New DOMDocument
- oxmlDoc.async = False
-
- ComboBox1.Clear
- r = Range("a65536").End(xlUp).Row
- 'oxmlDoc.Load CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\省市区.xml" '若将xml文件放于桌面,则启用此句
- oxmlDoc.Load ThisWorkbook.Path & "\省市区.xml"
- Set oXmlNodes = oxmlDoc.SelectNodes("/ProvinceCity")
- For i = 0 To oXmlNodes(0).ChildNodes.Length - 1
- ComboBox1.AddItem oXmlNodes(0).ChildNodes(i).nodeName
- Next
- ComboBox1.ListIndex = 0
- End Sub</P>
- <P>Private Sub ComboBox1_Change()
- On Error Resume Next
- Dim oxmlDoc As DOMDocument
- Dim Node As IXMLDOMNode
- Dim oXmlNodes As IXMLDOMNodeList
- Set oxmlDoc = New DOMDocument
- oxmlDoc.async = False
-
- ComboBox2.Clear
- r = Range("a65536").End(xlUp).Row
- oxmlDoc.Load ThisWorkbook.Path & "\省市区.xml"
- Set oXmlNodes = oxmlDoc.SelectNodes("/ProvinceCity/" & ComboBox1.Value)
- For i = 0 To oXmlNodes(0).ChildNodes.Length - 1
- ComboBox2.AddItem oXmlNodes(0).ChildNodes(i).Attributes(0).Text
- Next
- End Sub</P>
- <P>Private Sub ComboBox2_Change()
- On Error Resume Next
- Dim oxmlDoc As DOMDocument
- Dim Node As IXMLDOMNode
- Dim oXmlNodes As IXMLDOMNodeList
- Set oxmlDoc = New DOMDocument
- oxmlDoc.async = False
-
- ComboBox3.Clear
- r = Range("a65536").End(xlUp).Row
- oxmlDoc.Load ThisWorkbook.Path & "\省市区.xml"
- Set oXmlNodes = oxmlDoc.SelectNodes("/ProvinceCity/" & ComboBox1.Value)
- For i = 0 To oXmlNodes(0).ChildNodes.Length - 1
- If oXmlNodes(0).ChildNodes(i).Attributes(0).Text = ComboBox2.Value Then
- For Each Node In oXmlNodes(0).ChildNodes(i).ChildNodes
- ComboBox3.AddItem Node.Attributes(0).Text
- Next
- End If
- Next
- End Sub</P>
- <P>Private Sub CommandButton1_Click()
- On Error Resume Next
- Dim oxmlDoc As DOMDocument
- Dim Node As IXMLDOMNode
- Dim oXmlNodes As IXMLDOMNodeList
- Set oxmlDoc = New DOMDocument
- oxmlDoc.async = False
-
- Cells.Clear
- [a1:c1] = Array("省", "市", "区"): [a1:c1].Font.Bold = True: [a1:c1].Font.Size = 20
- r = Range("a65536").End(xlUp).Row
- oxmlDoc.Load ThisWorkbook.Path & "\省市区.xml"
- Set oXmlNodes = oxmlDoc.SelectNodes("/ProvinceCity")
- For i = 0 To oXmlNodes(0).ChildNodes.Length - 1
- For j = 0 To oXmlNodes(0).ChildNodes(i).ChildNodes.Length - 1
- For k = 0 To oXmlNodes(0).ChildNodes(i).ChildNodes(j).ChildNodes.Length - 1
- n = n + 1
- Cells(r + n, 1) = oXmlNodes(0).ChildNodes(i).nodeName
- Cells(r + n, 2) = oXmlNodes(0).ChildNodes(i).ChildNodes(j).Attributes(0).Text
- Cells(r + n, 3) = oXmlNodes(0).ChildNodes(i).ChildNodes(j).ChildNodes(k).Attributes(0).Text
- Next
- Next
- Next
- End Sub</P>
- <P>Private Sub CommandButton2_Click()
- Dim cat As ADOX.Catalog
- Dim tbl As ADOX.Table
- On Error Resume Next
- Kill Replace(ActiveWorkbook.FullName, "xlsm", "mdb")
- Set cat = New ADOX.Catalog
- cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & Replace(ActiveWorkbook.FullName, "xlsm", "mdb") & ";"
- Set tbl = New ADOX.Table
- tbl.Name = ActiveSheet.Name
- tbl.Columns.Append "ID", adInteger
- tbl.Columns.Append Range("A1").Text, adVarWChar, 60
- tbl.Columns.Append Range("B1").Text, adVarWChar, 60
- tbl.Columns.Append Range("C1").Text, adVarWChar, 60
- cat.Tables.Append tbl
- Set cat = Nothing
-
- Dim cnn As ADODB.Connection
- Dim rst As ADODB.Recordset
- Dim i, Rw As Long, j As Integer
- Rw = Range("A65536").End(xlUp).Row
- Set cnn = New ADODB.Connection
- With cnn
- .Provider = "Microsoft.Jet.OLEDB.4.0"
- .Open Replace(ActiveWorkbook.FullName, "xlsm", "mdb")
- End With
- Set rst = New ADODB.Recordset
- rst.CursorLocation = adUseServer
- rst.Open Source:=ActiveSheet.Name, ActiveConnection:=cnn, CursorType:=adOpenDynamic, LockType:=adLockOptimistic, Options:=adCmdTable
- For i = 2 To Rw
- rst.AddNew
- For j = 1 To 3
- rst("ID") = i - 1
- rst(Cells(1, j).Value) = Cells(i, j).Value
- Next j
- rst.Update
- Next i
- rst.Close
- cnn.Close
- Set rst = Nothing
- Set cnn = Nothing
- MsgBox "省市区数据已全部导入数据库" & Replace(ActiveWorkbook.FullName, "xlsm", "mdb") & "中!"
- End Sub
- </P>
复制代码
|
|