首页 > 编程语言 > 详细

VBA 代码集

时间:2016-06-10 12:17:19      阅读:155      评论:0      收藏:0      [点我收藏+]

 

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 代码集

原文:http://www.cnblogs.com/jerron/p/5573406.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!