VBA 代码集
Posted jerron
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VBA 代码集相关的知识,希望对你有一定的参考价值。
1. 读取本机网卡地址
Dim MyMac Set MyMac = GetObject("Winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration") For Each MyMacAddress In MyMac If MyMacAddress.IPEnabled = True Then MsgBox "本机网卡MAC地址是:" & MyMacAddress.MacAddress Exit For End If Next
2.从数据集读取数据至Sheet
Dim strDate As String, strEntity As String strDate = Format(Sheets("sheet1").[b2], "yyyy-MM-dd") strEntity = Sheets("sheet1").[b3] Dim strSql As String ‘查询语句, strSql = "select * from xxxx where ..." Sheets("sheet1").Range("A7:J65535").ClearContents ‘清除内容 Dim ds1 As ADODB.Recordset Set ds1 = runSql(strSql) ‘根据查询语句获得数据,并填充至A7 Worksheets("sheet10").Range("A7").CopyFromRecordset ds1 Set ds1 = Nothing
3. 数据库连接及执行
Option Explicit Public conn As ADODB.Connection Public Const strConn = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=ts123;Initial Catalog=SQLDB;Data Source=.;Connect Timeout=720; " Public Function runSql(strSql) As ADODB.Recordset Dim ds As ADODB.Recordset Set ds = New ADODB.Recordset OpenConn ds.Open strSql, conn Set runSql = ds CloseConn End Function Public Function OpenConn() If conn Is Nothing Then Set conn = New ADODB.Connection End If If conn.State <> 1 Then conn.Open strConn End If End Function
Public Function CloseConn() If conn.State = 1 Then Set conn = Nothing End If End Function
以上是关于VBA 代码集的主要内容,如果未能解决你的问题,请参考以下文章