{"id":1941,"date":"2023-11-20T21:38:25","date_gmt":"2023-11-20T12:38:25","guid":{"rendered":"https:\/\/euc-access-excel-db.com\/tips\/?p=1941"},"modified":"2023-12-04T21:30:05","modified_gmt":"2023-12-04T12:30:05","slug":"excelvba-auto-close01","status":"publish","type":"post","link":"https:\/\/euc-access-excel-db.com\/tips\/ct07_se\/ct075012_xls2k_vba_tips\/excelvba-auto-close01","title":{"rendered":"ExcelVBA \uff5e \u30d5\u30a1\u30a4\u30eb\u3092\u9589\u3058\u308b\u3068\u304d\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\uff08\u500b\u4eba\u7528\u30de\u30af\u30ed\u30d6\u30c3\u30af\u304c\u6025\u306b\u751f\u6210\u3055\u308c\u3066\u3057\u307e\u3063\u305f\u308a\u3001\u4ed6\u306b\u958b\u3044\u3066\u3044\u308b\u30d5\u30a1\u30a4\u30eb\u304c\u3042\u3063\u3066\u305d\u3061\u3089\u306f\u9589\u3058\u305f\u304f\u306a\u3044\u5834\u5408\uff09"},"content":{"rendered":"<p>ExcelVBA \uff5e \u30d5\u30a1\u30a4\u30eb\u3092\u9589\u3058\u308b\u3068\u304d\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\uff08\u500b\u4eba\u7528\u30de\u30af\u30ed\u30d6\u30c3\u30af\u304c\u6025\u306b\u751f\u6210\u3055\u308c\u3066\u3057\u307e\u3063\u305f\u308a\u3001\u4ed6\u306b\u958b\u3044\u3066\u3044\u308b\u30d5\u30a1\u30a4\u30eb\u304c\u3042\u3063\u3066\u305d\u3061\u3089\u306f\u9589\u3058\u305f\u304f\u306a\u3044\u5834\u5408\uff09<\/p>\n<p><SPAN>\u3000\u3000<\/SPAN><br \/>\nExcel\u30d5\u30a1\u30a4\u30eb\u3092\u9589\u3058\u308b\u30b3\u30de\u30f3\u30c9\u30dc\u30bf\u30f3\u3092\u4f5c\u308a\u305f\u3044\u6642\u3001<\/p>\n<p>\u30fb\u77e5\u3089\u306a\u3044\u3042\u3044\u3060\u306b\u8ab0\u304b\u304c\u300c\u500b\u4eba\u7528\u30de\u30af\u30ed\u30d6\u30c3\u30af(Personal.xlsb)\u300d\u3092<br \/>\n\u3000\u3000\u751f\u6210\u3057\u3066\u3057\u307e\u3063\u3066\u3044\u305f\u308a\u3001<\/p>\n<p>\u30fb\u7121\u95a2\u4fc2\u306e\u8907\u6570\u306e\u30d5\u30a1\u30a4\u30eb\u304c\u958b\u304b\u308c\u3066\u3057\u307e\u3063\u3066\u3044\u305f\u308a\u3067\u3001<\/p>\n<p>\u300cApplication.Quit\u300d\u3060\u3051\u3060\u3068\u6b63\u5e38\u306b\u30d5\u30a1\u30a4\u30eb\u304c\u9589\u3058\u308b\u3053\u3068\u304c\u3067\u304d\u306a\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u306a\u306e\u3067\u3001\u305d\u306e\u3088\u3046\u306a\u5834\u5408\u3001\u4ee5\u964d\u306b\u6319\u3052\u305f\u3088\u3046\u306a\u30b3\u30fc\u30c9\u3092<br \/>\n\u6a19\u6e96\u30e2\u30b8\u30e5\u30fc\u30eb\u306b\u300c\u305d\u306e\u307e\u307e\u30b3\u30d4\u30da\u300d\u3057\u3066\u304a\u304f\u3068\u3001<br \/>\n\u30d5\u30a1\u30a4\u30eb\u3092\u9589\u3058\u308b\u3068\u304d\u306b\u5909\u306a\u52d5\u304d\u306b\u306a\u308b\u3053\u3068\u3092\u56de\u907f\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u300cAuto_Close()\u300d\u3068\u3044\u3046\u300cSub\u300d\u306e\u30d7\u30ed\u30b7\u30fc\u30b8\u30e3\u306b\u66f8\u304f\u306e\u3067\u3001\u30b3\u30de\u30f3\u30c9\u30dc\u30bf\u30f3\u304c<br \/>\n\u300c\u30d5\u30a9\u30fc\u30e0\u30b3\u30f3\u30c8\u30ed\u30fc\u30eb\u300d\u3067\u3082\u300cActiveX\u30b3\u30f3\u30c8\u30ed\u30fc\u30eb\u300d\u3067\u3082\u3001<br \/>\n\u3069\u3061\u3089\u3067\u3082OK\u3067\u3059\u3002<br \/>\n\u307e\u305f\u3001\u300cAuto_Close()\u300d\u306f\u3001\u300c\u00d7\u300d\u3067\u9589\u3058\u305f\u3068\u304d\u3082\u305d\u306e\u5185\u5bb9\u304c\u5b9f\u884c\u3055\u308c\u308b\u306e\u3067\u3001<br \/>\n\u305d\u306e\u610f\u5473\u3067\u3082\u30c0\u30a4\u30b8\u30e7\u30d6\u3067\u3059\u3002\u81ea\u52d5\u4e0a\u66f8\u304d\u3057\u3066\u9589\u3058\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<p><SPAN>\u3000\u3000<\/SPAN><\/p>\n<pre class=\"lang:default decode:true \">\r\n'\r\n'\r\nOption Explicit\r\n\r\nSub Auto_Close()\r\n\r\n    Dim i_WBcnt As Integer\r\n    \r\n    Let i_WBcnt = Workbooks.Count\r\n    \r\n    ThisWorkbook.Save\r\n    '\u4e0a\u66f8\u304d\u3057\u3066\u3001\r\n    \r\n    If i_WBcnt = 1 Then Application.Quit\r\n    '\u2191\u958b\u3044\u3066\u3044\u308b\u30d5\u30a1\u30a4\u30eb\u304c1\u3064\u306e\u5834\u5408\u3001Personal.xlsb\u3082\u4f55\u3082\u306a\u3044\u306e\u3067\u3001\r\n    '\u3000\u307e\u3093\u307e\u3067\u9589\u3058\u308b\u3002ThisWorkbook.Close\u3067\u9589\u3058\u308b\u3068\u30ac\u30ef\u3060\u3051\u304c\r\n\u3000\u3000'\u3000\u306e\u3063\u307a\u3089\u307c\u3046\u3067\u6b8b\u3063\u3066\u3057\u307e\u3046\u305f\u3081\u3001Application.Quit\u3067\u9589\u3058\u308b\u3002\r\n    \r\n    \r\n    If i_WBcnt = 2 Then\r\n    \r\n        If WBExistChk(\"Personal.xlsb\") = True Then\r\n            Application.DisplayAlerts = False\r\n            Workbooks.Item(\"Personal.xlsb\").Save\r\n            Application.Quit\r\n        Else\r\n            Application.DisplayAlerts = False\r\n            ThisWorkbook.Save\r\n            ThisWorkbook.Close\r\n        End If\r\n        \r\n    Else\r\n        Application.DisplayAlerts = False\r\n        ThisWorkbook.Save\r\n        ThisWorkbook.Close\r\n    End If\r\n    '\u2191\u958b\u3044\u3066\u3044\u308b\u30d5\u30a1\u30a4\u30eb\u304c\uff12\u3064\u306e\u5834\u5408\u3001Personal.xlsb\u304c\u3042\u308b\u306e\u3067\u3001\r\n    '\u3000Personal.xlsb\u304c\u3042\u308b\u5834\u5408\u306f\u305d\u3063\u3061\u3082\u4e00\u5fdc\u4e0a\u66f8\u304d\u3057\u3066\u304b\u3089\u9589\u3058\u308b\u3002\r\n    \r\n    \r\n    If i_WBcnt >= 3 Then ThisWorkbook.Close\r\n    '\u2191\u958b\u3044\u3066\u3044\u308b\u30d5\u30a1\u30a4\u30eb\u304c\uff13\u3064\u4ee5\u4e0a\u306e\u5834\u5408\u3001Personal.xlsb\u304c\u3042\u308d\u3046\u304c\r\n    '\u3000\u306a\u304b\u308d\u3046\u304c\u95a2\u4fc2\u306a\u3044\u306e\u3067\u3001ThisWorkbook.Close\u3067\u9589\u3058\u308b\u3002\r\n\u3000\u3000'\u3000Application.Close\u3067\u9589\u3058\u308b\u3068\u3001\u4ed6\u306e\u7121\u95a2\u4fc2\u306e\u30d5\u30a1\u30a4\u30eb\u307e\u3067\r\n\u3000\u3000'\u3000\u9589\u3058\u3088\u3046\u3068\u3055\u308c\u3066\u3057\u307e\u3046\u306e\u3067ThisWorkbook.Close\u3067\u9589\u3058\u308b\u3002\r\n\u3000\u3000'\u3000\uff11\u3064\u3060\u3051\u306e\u30d5\u30a1\u30a4\u30eb\u306e\u5834\u5408\u3001ThisWorkbook.Close\u3067\u9589\u3058\u308b\u3068\r\n\u3000\u3000'\u3000\u30ac\u30ef\u3060\u3051\u304c\u306e\u307a\u3089\u307c\u3046\u306e\u307e\u307e\u6b8b\u3063\u3066\u3057\u307e\u3046\u304c\u3001\r\n\u3000\u3000'\u3000\uff13\u3064\u4ee5\u4e0a\u306e\u5834\u5408\u306f\u305d\u3046\u306a\u3089\u306a\u3044\u306e\u3067\u3001ThisWorkbook.Close\u3067OK\u3002\r\n    \r\nEnd Sub\r\n\r\n\r\n'######################################################################\r\n'\u6307\u5b9a\u3057\u305f\u540d\u524d\u306e\u30ef\u30fc\u30af\u30d6\u30c3\u30af\u304c\u5b58\u5728\u3059\u308b\u304b\u3069\u3046\u304b\u306e\u30c1\u30a7\u30c3\u30af\u306e\u95a2\u6570\r\n'\u5b58\u5728\u3059\u308c\u3070True\u3092\u8fd4\u3057\u3001\u5b58\u5728\u3057\u306a\u3051\u308c\u3070False\u3092\u8fd4\u3057\u307e\u3059\u3002\r\n'\u30eb\u30fc\u30d7\u3092\u4f7f\u308f\u306a\u3044\u65b9\u6cd5\uff1d\u30a8\u30e9\u30fc\u3067\u5224\u5225\u3059\u308b\u65b9\u6cd5\u3001\u3067\u3084\u3063\u3066\u3044\u307e\u3059\u3002\r\n'\u3061\u3087\u3063\u3068\u624b\u629c\u304d\u7248\r\n'######################################################################\r\n \r\nFunction WBExistChk(s_WbNm02 As String) As Boolean\r\n \r\n    On Error GoTo error1:\r\n    \r\n    If 1 <= Len(Workbooks.Item(s_WbNm02).Name) Then\r\n        WBExistChk = True\r\n    Else\r\n    \r\n    End If\r\n    \r\nExit Function\r\n \r\nerror1:\r\n \r\n    WBExistChk = False\r\n \r\nEnd Function\r\n \r\n\r\n'\r\n'\r\n<\/pre>\n<p><SPAN>\u3000\u3000<\/SPAN><br \/>\n<SPAN>\u3000\u3000<\/SPAN><br \/>\n<SPAN>\u3000\u3000<\/SPAN><br \/>\n<SPAN>\u3000\u3000<\/SPAN><br \/>\n<SPAN>\u3000\u3000<\/SPAN><\/p>\n","protected":false},"excerpt":{"rendered":"ExcelVBA \uff5e \u30d5\u30a1\u30a4\u30eb\u3092\u9589\u3058\u308b\u3068\u304d\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\uff08\u500b\u4eba\u7528\u30de\u30af\u30ed\u30d6\u30c3\u30af\u304c\u6025\u306b\u751f\u6210\u3055\u308c\u3066\u3057\u307e\u3063\u305f\u308a\u3001\u4ed6\u306b\u958b\u3044\u3066\u3044\u308b\u30d5\u30a1\u30a4\u30eb\u304c\u3042\u3063\u3066\u305d\u3061\u3089\u306f\u9589\u3058\u305f\u304f\u306a\u3044\u5834\u5408\uff09 \u3000\u3000 Excel\u30d5\u30a1\u30a4\u30eb\u3092\u9589\u3058\u308b\u30b3\u30de\u30f3\u30c9\u30dc\u30bf\u30f3\u3092\u4f5c\u308a\u305f\u3044\u6642\u3001 ...","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[234],"_links":{"self":[{"href":"https:\/\/euc-access-excel-db.com\/tips\/wp-json\/wp\/v2\/posts\/1941"}],"collection":[{"href":"https:\/\/euc-access-excel-db.com\/tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/euc-access-excel-db.com\/tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/euc-access-excel-db.com\/tips\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/euc-access-excel-db.com\/tips\/wp-json\/wp\/v2\/comments?post=1941"}],"version-history":[{"count":0,"href":"https:\/\/euc-access-excel-db.com\/tips\/wp-json\/wp\/v2\/posts\/1941\/revisions"}],"wp:attachment":[{"href":"https:\/\/euc-access-excel-db.com\/tips\/wp-json\/wp\/v2\/media?parent=1941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/euc-access-excel-db.com\/tips\/wp-json\/wp\/v2\/categories?post=1941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}