ã¯ããã«
ãVisual Basicãç¬èªã®ã¯ã©ã¹ãä½æãã¦ä½¿ããããã«ãOfficeã«æè¼ããã¦ããVBAã§ãç¬èªã®ã¯ã©ã¹ãããã¸ã§ã¯ãã«çµã¿è¾¼ããã¨ãã§ãã¾ããä½æããã¯ã©ã¹ã¯ãåOfficeã¢ããªã±ã¼ã·ã§ã³ã§ãã¤ã³ãã¼ãããã¨ã¯ã¹ãã¼ãããã§ãããããæ±ç¨æ§ã®ããã¯ã©ã¹ã1ã¤ä½æããã°ä»ã®ã¢ããªã±ã¼ã·ã§ã³ã®VBAã§ãå©ç¨ã§ããããã«ãªãã¾ãã
ãããã§ãæ¬ç¨¿ã§ã¯Excel 2003ã§å©ç¨ã§ããã¯ã©ã¹ãä½æããªãããVBAã§ã¯ã©ã¹ãä½ãæ¹æ³ãç´¹ä»ãã¾ãã
対象èªè
ãExcel VBAã使ã£ã¦ãã¯ãï¼ããã°ã©ã ï¼ãä½ã£ããã¨ã®ãã人ãVisual Basicã使ãã人ã対象ã§ãã
å¿ è¦ãªç°å¢
ãExcel 2000ã2002ã2003ã使ç¨ã§ããç°å¢ã
ã¯ã©ã¹ã¯ä¾¿å©
ãVBAã§ã¯ã©ã¹ãä½æããã¡ãªããã¯ãè¤éãªå¦çã1ã¤ã®ãªãã¸ã§ã¯ãã«ã¾ã¨ããããããã£ãã¡ã½ãããéãã¦ãã®æ©è½ãå¼ã³åºããããã©ã¡ã¼ã¿ãè¨å®ãããã¨ãã§ããç¹ã§ããããã¦ãããããæ±ç¨æ§ã®ããå½¢ã«ä»ä¸ãã¦ããã°ãä»ã®ããã¸ã§ã¯ãã«çµã¿è¾¼ãã§ä½¿ç¨ã§ããããã«ãªãã¾ãã
ãã¾ããã¯ã©ã¹ã«ã¯ããããã£ãã¡ã½ãããçµã¿è¾¼ãã§ä½¿ããã¨ãã§ããç¹ã大ããªã¡ãªããã§ããæ±ç¨æ§ã®ããããã·ã¼ã¸ã£ãä½æããã°ãä½ãã¯ã©ã¹ãä½æããªãã¦ãã³ã¼ããã³ãã¼ããã°è¯ãã®ã§ãããè¤éãªã³ã¼ãã®å ´åãã©ãã§ã©ã®ããã·ã¼ã¸ã£ã使ãã°ããã®ããæ¸ãã人ããã³ã¼ãã®ä½¿ãæ¹ãåãããªããã¨ããåé¡ç¹ãããã¾ããããããã¯ã©ã¹ã§ããã°ãå®è£ ãããåæ©è½ã»è¨å®ãããããã£ã¨ã¡ã½ããã§å ¬éããå½¢æ ãåãã¾ãããããã®èª¬æããããã°èª°ã§ãçµã¿è¾¼ãã§ä½¿ç¨ã§ãããããã³ã¼ãã使ãå´ã¯åããããããªãã¾ãã
ãã¾ããä½æããã¯ã©ã¹ã¯ãåç¬ã®ãã¡ã¤ã«ã«ã¨ã¯ã¹ãã¼ããããããããå¥ã®ãã¡ã¤ã«ã§ã¤ã³ãã¼ããã¦ä½¿ããã¨ãã§ããã®ã§ãä¾ãã°æ±ç¨ã®ã¯ã©ã¹ãä½æãã¦ãµã¼ãã«1ã¤ç½®ãã¦ããã ãã§ãè¤æ°ã®ããã¸ã§ã¯ãã§ä½¿ãã¾ãããã¨ãã§ãã¾ãã
ã¯ã©ã¹ã®ä½ææé
ãã§ã¯ãã¯ã©ã¹ãããã¸ã§ã¯ãã«çµã¿è¾¼ãã«ã¯ã©ãããã°ããã®ãããã®æé ãç°¡åã«èª¬æãã¾ãã
- VBEditorãèµ·åããããã¸ã§ã¯ãã«ãã¯ã©ã¹ã¢ã¸ã¥ã¼ã«ããæ¿å ¥ãã¾ãã
- ã¯ã©ã¹ã®ãªãã¸ã§ã¯ãåãå¤æ´ããåãããããååã«ãã¾ãã
- ã¯ã©ã¹ã§ä½¿ç¨ããããããã£ã»ã¡ã½ãããä½æãã¾ãã
- æ¢ã«ããã¯ã©ã¹ã使ç¨ããå ´åã¯ãããã¸ã§ã¯ãã®ï¼»ãã¡ã¤ã«ã®ã¤ã³ãã¼ãï¼½ã¡ãã¥ã¼ã§çµã¿è¾¼ãã¯ã©ã¹ãã¤ã³ãã¼ããã¾ãã
- ããã¸ã§ã¯ãã«æ¨æºã¢ã¸ã¥ã¼ã«ã追å ããã¯ã©ã¹ã使ç¨ããããã·ã¼ã¸ã£ãä½æãã¾ãã
Dim
ã¹ãã¼ãã¡ã³ãã¨New
ãã¼ã¯ã¼ãã§ãä½æããã¯ã©ã¹ããªãã¸ã§ã¯ãåãã¾ãã- çµã¿è¾¼ãã ããããã£ã«å¤ãè¨å®ããã¡ã½ãããå®è¡ãã¾ãã
- æ°è¦ã«ä½æããã¯ã©ã¹ã¯ãããã¸ã§ã¯ãã®ï¼»ãã¡ã¤ã«ã®ã¨ã¯ã¹ãã¼ãï¼½ã¡ãã¥ã¼ã§ãåç¬ã®ãã¡ã¤ã«ã«ãã¦ä¿åããã°ãä»ã®ããã¸ã§ã¯ãã§ãå©ç¨ã§ãã¾ãã
ãåç¬ã®ãã¡ã¤ã«ã«ä¿åãããã¯ã©ã¹ã¯ãããã¹ããã¡ã¤ã«ã§ä¿åããã¦ãã¾ãã®ã§ãããã¹ãã¨ãã£ã¿ã§ç°¡åã«ã³ã¼ããã³ãã¼ï¼ãã¼ã¹ãã§ãã¾ãã
Excel VBAã§ã¯ã©ã¹ãä½ã
ãã§ã¯ãExcel VBAã§ãç°¡åãªExcelç¨ã®ã¯ã©ã¹ã1ã¤ä½æãã¦ã¿ã¾ãããããã®ã¯ã©ã¹ã¯ãå ±æåããã¦ããããã¯ã«ãç¾å¨ã¢ã¯ã»ã¹ãã¦ããã¦ã¼ã¶ã¼åã¨ãã°ã¤ã³æ¥æãããã°è¨é²ã¨ãã¦ããã¹ããã¡ã¤ã«ã«è½ã¨ãæ©è½ãå®è£ ãã¦ãã¾ãã
ããã°ãã¡ã¤ã«ãä¿åãããã©ã¤ãï¼ãã©ã«ãï¼ã®ãã¹ãè¨å®ãã1ã¤ã®ããããã£ã¨ããã°ãã¡ã¤ã«ä½æãå®è¡ãã1ã¤ã®ã¡ã½ãããæã¡ã¾ãã
æ°ããã¯ã©ã¹ãä½ã
ãä½ãæ¹ã¯ããã£ã¦ç°¡åã§ããæ°ããããã¯ãç¨æããVBEditorãèµ·åãã¦ãã ãããããã¸ã§ã¯ãã«ãã¯ã©ã¹ã¢ã¸ã¥ã¼ã«ãã1ã¤è¿½å ãã¾ãã
ãããããã£ã¦ã£ã³ãã¦ã§ããã®ã¯ã©ã¹ã®ãªãã¸ã§ã¯ãåãClass1
ããAccessLog
ã«å¤æ´ãã¾ãã
ãã¯ã©ã¹ã¢ã¸ã¥ã¼ã«ã®å®£è¨ã»ã¯ã·ã§ã³ã«ãPublic
ãã¼ã¯ã¼ãã§æåååå¤æ°PathName
ã宣è¨ãã¾ããããããã¯ã©ã¹ã®ããããã£ã«ãªãã¾ããå¤æ°åããã®ã¾ã¾ããããã£åã¨ãªããè¨å®ããå¤ã¯æåååã®ãã¼ã¿åã«ãªãã¾ãã
Public PathName As String
ãç°¡åãªããããã£ãã¯ã©ã¹ã«è¨å®ããã«ã¯ããã®å¤æ°å®£è¨ã使ãã¾ãã
ã¯ã©ã¹ã«ã¡ã½ãããä½ã
ã次ã«ãCreateLog
ã¨ããååã®ããã·ã¼ã¸ã£ã1ã¤ä½æãã¾ãããããããã®ã¯ã©ã¹ã®ã¡ã½ããã«ãªãã¾ããå¼æ°ãæããªãSub
ããã·ã¼ã¸ã£ã§å®£è¨ãã¾ãã
Sub CreateLog() End Sub
å¤æ°å®£è¨
ãããã·ã¼ã¸ã£å ã«å¤æ°ã5ã¤å®£è¨ãã¾ãã
Dim UserInfo As Variant '------ 1 Dim UserCnt As Integer '------ 2 Dim Fname As String '------ 3 Dim LogMsg As String '------ 4 Dim DateName As String '------ 5
- ã¦ã¼ã¶ã¼æ å ±ãæ ¼ç´
- ã¢ã¯ã»ã¹ãã¦ããã¦ã¼ã¶ã¼æ°ãæ ¼ç´
- ãã°ãä¿åãããã¡ã¤ã«åãæ ¼ç´
- ãã°ã«æ¸ãè¾¼ãæååãæ ¼ç´
- æ¥ä»ãæååã§æ ¼ç´ãã
ã¦ã¼ã¶ã¼æ å ±ã®åå¾
ãããã¯ã«ã¢ã¯ã»ã¹ãã¦ããã¦ã¼ã¶ã¼æ
å ±ã¯ãApplication
ãªãã¸ã§ã¯ãã®UserStatus
ããããã£ã«æ ¼ç´ããã¦ãã¾ãã
ããã®ããããã£ã¯ãã¦ã¼ã¶ã¼1人ã«ä»ããã¦ã¼ã¶ã¼åããã¢ã¯ã»ã¹ããæç¹ã®æ¥æããã¢ã¯ã»ã¹ã¢ã¼ããã®3ã¤ã®æ å ±ãé åã§æ ¼ç´ãã¦ãã¾ãã1人ã«ã¤ã3ã¤ã®æ å ±ãã¢ã¯ã»ã¹ãã¦ãã人æ°åæã¡ã¾ãã®ã§ã1ããå§ã¾ãnè¡3åã®2次å é åã¨ãªãã¾ãã1次å ã«ã¦ã¼ã¶ã¼ã®äººæ°ã2次å ã«ã¦ã¼ã¶ã¼æ å ±ãæ ¼ç´ããã¾ãã
ããã®ããããã£ã®å¤ãåãåºãå ´åã¯ãåãåãå¤æ°ã®ãã¼ã¿åãããªã¢ã³ãåã«ãã¦ããã¾ãã
UserInfo = ActiveWorkbook.UserStatus
ãããã§ããã¹ã¦ã®ã¦ã¼ã¶ã¼æ
å ±ãåãåããã¨ãã§ãã¾ããããã§ã¯ã3人ã®ã¦ã¼ã¶ã¼ãããã¯ã«ã¢ã¯ã»ã¹ãã¦ãã¾ããããåãåã£ãå¤æ°UserInfo
ã¯3è¡3åã®é
åã«ãªãã¾ãã
UserStatus
ããããã£ãè¿ãé
åã¯ãæ·»ãåãã1ãããå§ã¾ãé
åã«ãªãã¾ãããUBound
é¢æ°ã使ç¨ããã¨ãé
åã®æ大è¦ç´ æ°ãåå¾ã§ãã¾ãããããå¤æ°UserInfo
ã®1次å
é
åã«å¯¾ãã¦ä½¿ç¨ããã¨ãã¢ã¯ã»ã¹ãã¦ããã¦ã¼ã¶ã¼æ°ãææ¡ã§ãã¾ãã
ãé¢æ°ã®æ»ãå¤ã¯ãå¤æ°UserCnt
ã«æ ¼ç´ãã¦ããã¾ãã
UserCnt = UBound(UserInfo, 1)
ä¿åãããã°ãã¡ã¤ã«ã®ä½æ
ã次ã«ãä¿åãããã°ãã¡ã¤ã«ã«ã¯ä½æããæ¥ä»ããã¡ã¤ã«åã«ä»ããããã«ããã®ã§ããã®æ¥ä»ã®æååãä½æãã¾ããDate
é¢æ°ã使ãã¨ãã·ã¹ãã ããç¾å¨ã®æ¥ä»ãåå¾ã§ãã¾ãããDate
é¢æ°ã®æ»ãå¤ãã2000/04/20ãã¨ããæ¸å¼ã§è¿ã£ã¦ãã¾ãããã®æ¸å¼ã ã¨è¨å·ã/ããå«ã¾ãã¦ããããã®ã¾ã¾ã§ã¯ãã¡ã¤ã«åã«ä½¿ç¨ã§ãã¾ããã
ãããã§ãReplace
é¢æ°ã§ã/ãè¨å·ãã-ãã«å¤æããæ¥ä»ã®æååãã2000-04-20ãã«å¤æãã¾ãã
DateName = Replace(Date, "/", "-")
ãã¯ã©ã¹AccessLog
ã«è¨å®ããããããã£PathName
ã«ã¯ããã°ãã¡ã¤ã«ãä¿åãããã¡ã¤ã«ã®ãã©ã¤ãã»ãã©ã«ãåãè¨å®ãã¾ããããã§ãããã使ã£ã¦ãä¿åãããã¡ã¤ã«åããã«ãã¹ã§ä½æãã¾ãããã¡ã¤ã«åã¯ãæååAccessLog
ã¨ä½æããæ¥ä»ãããã¦æ¡å¼µåã.txtããçµåãã¦ä½æãã¾ãã
Fname = PathName & "\AccessLog" & DateName & ".txt"
ãä¾ãã°ã2006å¹´7æ27æ¥ã«ãã°ãã¡ã¤ã«ãä½æããã¨ããã¨ããã®ãã¡ã¤ã«åã¯ãAccessLog2006-07-27.txtãã¨ãããã¡ã¤ã«åã«ãªãã¾ããããããã£PathName
ã«ããC:ãã¨ãããã©ã¤ãåãè¨å®ããã°ããã«ãã¹ã®ãã¡ã¤ã«åã¯ãc:\AccessLog2006-07-27.txtãã¨ãªãããããå¤æ°Fname
ã«æ ¼ç´ããã¾ãã
FileSystemObjectã®ä½æ
ãExcelã§ããã¹ããã¡ã¤ã«ãä½æãã¦ä¿åããã«ã¯ãFileSystemObject
ãªãã¸ã§ã¯ãã使ç¨ãã¾ãããã®ãªãã¸ã§ã¯ãã¯ãExcelãä¿æãããªãã¸ã§ã¯ãã§ã¯ãªããVBAã«çµã¿è¾¼ã¾ãã¦ãããªãã¸ã§ã¯ãã§ãã³ã³ãã¥ã¼ã¿ã®ãã¡ã¤ã«ã·ã¹ãã ã¸ã®ã¢ã¯ã»ã¹ãæä¾ãããªãã¸ã§ã¯ãã§ãã
ãExcelã®ãªãã¸ã§ã¯ãã¨ã¯å°ãéãæé ã§ä½¿ç¨ãã¾ããã¾ããCreateObject
é¢æ°ã§ããã®ãªãã¸ã§ã¯ãã®ã¤ã³ã¹ã¿ã³ã¹ãä½æãã¾ããå¼æ°ã«ã¯ããã®ãªãã¸ã§ã¯ãã®ããã°ã©ã IDã§ããScripting.FileSystemObject
ãè¨è¿°ãã¾ããããã¦ãä½æãããªãã¸ã§ã¯ãã¸ã®åç
§ããªãã¸ã§ã¯ãå¤æ°Fsobj
ã«æ ¼ç´ãã¾ãã
Set Fsobj = CreateObject("Scripting.FileSystemObject")
ã次ã«ãFileSystemObject
ãªãã¸ã§ã¯ããä¿æããã¡ã½ããCreateTextFile
ãå®è¡ãã¾ãã
Set txt = Fsobj.CreateTextFile(Fname, True)
ããã®ã¡ã½ããã¯ã第1å¼æ°ã«æå®ãããã¡ã¤ã«åã®ããã¹ããã¡ã¤ã«ãä½æãããªã¼ãã³ãã¾ãã第2å¼æ°ã«ã¯ãæ¢ã«ååã®ãã¡ã¤ã«ããã£ãå ´åã«ä¸æ¸ããããã©ãããè«çå¤ã§è¨å®ãã¾ããTrue
ã§ä¸æ¸ãã許å¯ãã¾ãã
ããã¡ã¤ã«ãç¡äºä½æã§ããã¨ããããTextStream
ã¨ãããªãã¸ã§ã¯ãã§è¿ãã¦ãã¾ãã®ã§ãSet
ã¹ãã¼ãã¡ã³ãã§ãªãã¸ã§ã¯ãå¤æ°txt
ã«æ ¼ç´ãã¦ããã¾ãã
ãã¡ã¤ã«ä¿åã®å®è¡
ãããã¹ããã¡ã¤ã«ã®æºåãã§ãããããã®ãã¡ã¤ã«ã«æ¸ãè¾¼ãæååãä½æãã¾ããããã¯ãFor
...Next
ã¹ãã¼ãã¡ã³ãã§ãã¦ã¼ã¶ã¼æ
å ±ãæ ¼ç´ãã¦ããé
åUserInfo
ããã²ã¨ãã²ã¨ãã®ã¦ã¼ã¶ã¼åã¨ã¢ã¯ã»ã¹æ¥æãåãåºãã&
æ¼ç®åã§é£çµãã¦1ã¤ã®æååã«ãã¾ãã
For i = 1 To UserCnt LogMsg = LogMsg & UserInfo(i, 1) & "ï¼" & UserInfo(i, 2) & Chr(13) Next
ãããã¦ãTextStream
ãªãã¸ã§ã¯ãã®WriteLine
ã¡ã½ããã使ã£ã¦ãä½æãããã¡ã¤ã«ã«æååãæ¸ãè¾¼ã¿ã¾ãã
txt.WriteLine LogMsg
ãæå¾ã«ãä½æãããã¡ã¤ã«ããTextStream
ãªãã¸ã§ã¯ãã®Close
ã¡ã½ããã§éãã¦å®äºã§ãã
txt.Close
ã¯ã©ã¹ãå®è£ ãã
ãä½æããã¯ã©ã¹ã¯ãããã¾ã§ãå®ç¾©ãããã ãã§ãéå½¢ãä½ã£ããããªãã®ã§ãããã®ã¯ã©ã¹ãå ã«ãå®éã«å®è¡å¯è½ãªãªãã¸ã§ã¯ããä½æããå¿ è¦ãããã¾ãã
ãããã¸ã§ã¯ãã«æ¨æºã¢ã¸ã¥ã¼ã«ã1ã¤è¿½å ããããã·ã¼ã¸ã£ããã°ã®ä½æããä½ãã¾ãã
Sub ãã°ã®ä½æ() End Sub
ãããã·ã¼ã¸ã£ã®åé ã§ãDim
ã¹ãã¼ãã¡ã³ãã«New
ãã¼ã¯ã¼ããä»ãã¦ãã¯ã©ã¹AccessLog
ãå®ä½åãã¾ããDim
ã®ãã¨ã«ãªãã¸ã§ã¯ãåMyLog
ãè¨è¿°ããAs New
ã®ãã¨ã«ä½æããã¯ã©ã¹ã®ãªãã¸ã§ã¯ãåAccessLog
ãè¨è¿°ãã¾ãã
Dim Mylog As New AccessLog
ãããã§ãã¯ã©ã¹AccessLog
ãå
ã«ãæ°ãããªãã¸ã§ã¯ãMylog
ãçæããã¦ã¡ã¢ãªã«èªã¿è¾¼ã¾ãã¾ãã
ããªãã¸ã§ã¯ããä½æããã¨ãã¯ã©ã¹ã«å®è£
ããã¦ããããããã£ã¨ã¡ã½ãããå©ç¨å¯è½ã«ãªããããã¾ããä¿åãããã°ãã¡ã¤ã«ã®ãã©ã¤ãåããããããã£PathName
ã«è¨å®ãã¾ããã³ã¼ãã¦ã£ã³ãã¦ã«ããWith Mylogãã¨è¨è¿°ãã.ããå
¥åããæç¹ã§ãããããã£PathName
ã¨ã¡ã½ããCreateLog
ãé¸æåè£ã«è¡¨ç¤ºããã¾ãã
ãããããã£PathName
ãé¸ã³ãã=ãã®ãã¨ã«ã"c:"ãã¨å
¥åãã¾ããããã§ããã°ãã¡ã¤ã«ã¯Cãã©ã¤ãã«ä¿åããã¾ãã
With Mylog .PathName = "c:"
ããã¹ãè¨å®ããããã¡ã½ããCreateLog
ãå®è¡ãã¾ãã
.CreateLog End With
ãããã§ããã®ããã·ã¼ã¸ã£ããã°ã®ä½æããå®è¡ããã°ãããã¯ã«ã¢ã¯ã»ã¹ãã¦ãã人ã®ã¦ã¼ã¶ã¼åã¨ã¢ã¯ã»ã¹æ¥æãããã¹ããã¡ã¤ã«ã«è¨é²ã»ä¿åããã¾ãã