{"id":2075,"date":"2017-11-15T13:03:05","date_gmt":"2017-11-15T04:03:05","guid":{"rendered":"http:\/\/okamurax.com\/?p=2075"},"modified":"2017-11-15T13:03:37","modified_gmt":"2017-11-15T04:03:37","slug":"2075","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=2075","title":{"rendered":"VBA \u5f25\u751fCSV\u64cd\u4f5c"},"content":{"rendered":"<p>DB\u306b\u767b\u9332\u3002<\/p>\n<pre class=\"lang:vb decode:true\">Application.StatusBar = \"\u30c7\u30fc\u30bf\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u4e2d...\"\r\n\r\nOn Error GoTo try\r\n\r\nSet con = CreateObject(\"adodb.connection\")\r\n\r\ncon.Open \"provider=microsoft.jet.oledb.4.0;data source=\" &amp; ActiveWorkbook.Path &amp; \"\\yayoi.mdb\"\r\ncon.Execute \"delete from tbl\"\r\n\r\nSet w = Sheets(\"\u8cbc\u4ed8\")\r\n\r\nFor r = 9 To w.UsedRange.Rows.Count\r\n\r\n  If w.Cells(r, 1).Value = \"[\u660e\u7d30\u884c]\" Then\r\n\r\n    w.Cells(r, 2).NumberFormatLocal = \"yyyy\/mm\/dd\"\r\n\r\n    If w.Cells(r, 15).Text = \"\" Then w.Cells(r, 15).Value = 0\r\n    If w.Cells(r, 16).Text = \"\" Then w.Cells(r, 16).Value = 0\r\n    If w.Cells(r, 22).Text = \"\" Then w.Cells(r, 22).Value = 0\r\n    If w.Cells(r, 23).Text = \"\" Then w.Cells(r, 23).Value = 0\r\n\r\n    q = \"insert into tbl (\" &amp; _\r\n    \"\u65e5\u4ed8,\u501f\u65b9\u52d8\u5b9a\u79d1\u76ee,\u501f\u65b9\u88dc\u52a9\u79d1\u76ee,\u501f\u65b9\u90e8\u9580,\u501f\u65b9\u91d1\u984d,\u501f\u65b9\u7a0e\u984d,\u8cb8\u65b9\u52d8\u5b9a\u79d1\u76ee,\u8cb8\u65b9\u88dc\u52a9\u79d1\u76ee,\u8cb8\u65b9\u90e8\u9580,\u8cb8\u65b9\u91d1\u984d,\u8cb8\u65b9\u7a0e\u984d,\u6458\u8981\" &amp; _\r\n    \") values (\" &amp; _\r\n    \"'\" &amp; w.Cells(r, 2).Text &amp; \"',\" &amp; _\r\n    \"'\" &amp; w.Cells(r, 10).Text &amp; \"',\" &amp; _\r\n    \"'\" &amp; w.Cells(r, 11).Text &amp; \"',\" &amp; _\r\n    \"'\" &amp; w.Cells(r, 12).Text &amp; \"',\" &amp; _\r\n    \" \" &amp; w.Cells(r, 15).Text &amp; \" ,\" &amp; _\r\n    \" \" &amp; w.Cells(r, 16).Text &amp; \" ,\" &amp; _\r\n    \"'\" &amp; w.Cells(r, 17).Text &amp; \"',\" &amp; _\r\n    \"'\" &amp; w.Cells(r, 18).Text &amp; \"',\" &amp; _\r\n    \"'\" &amp; w.Cells(r, 19).Text &amp; \"',\" &amp; _\r\n    \" \" &amp; w.Cells(r, 22).Text &amp; \" ,\" &amp; _\r\n    \" \" &amp; w.Cells(r, 23).Text &amp; \" ,\" &amp; _\r\n    \"'\" &amp; w.Cells(r, 24).Text &amp; \"');\"\r\n\r\n    con.Execute q: Debug.Print q\r\n\r\n  End If\r\n\r\nNext r\r\n\r\nIf con.State = 1 Then con.Close\r\n\r\nApplication.StatusBar = False\r\n\r\nInsertDb = True\r\n\r\nExit Function\r\n\r\ntry:\r\n\r\n  If con.State = 1 Then con.Close\r\n  \r\n  Application.StatusBar = False\r\n  \r\n  MsgBox Err.Description\r\n  \r\n  InsertDb = False\r\n<\/pre>\n<p>DB\u304b\u3089\u53d6\u308a\u51fa\u3059\u3002<\/p>\n<pre class=\"lang:vb decode:true\">Application.StatusBar = \"\u30c7\u30fc\u30bf\u53d6\u5f97\u4e2d...\"\r\n\r\nOn Error GoTo try\r\n\r\nSet con = CreateObject(\"adodb.connection\")\r\nSet rec = CreateObject(\"adodb.recordset\")\r\n\r\ncon.Open \"provider=microsoft.jet.oledb.4.0;data source=\" &amp; ActiveWorkbook.Path &amp; \"\\yayoi.mdb\"\r\n\r\nSet w = Sheets(\"\u96c6\u8a08\")\r\nw.Activate\r\nw.Cells.Delete\r\n\r\ni = 1\r\n\r\nw.Cells(i, 1).Value = \"\u58f2\u4e0a\"\r\n\r\ni = w.UsedRange.Rows.Count + 1\r\nrec.Open \"select '\u751f\u7523\u8005\u58f2\u4e0a\u9ad8','-',sum(\u8cb8\u65b9\u91d1\u984d)-sum(\u8cb8\u65b9\u7a0e\u984d) from tbl where \u8cb8\u65b9\u52d8\u5b9a\u79d1\u76ee = '\u751f\u7523\u8005\u58f2\u4e0a\u9ad8'\", con\r\nw.Cells(i, 2).CopyFromRecordset rec\r\nrec.Close\r\n\r\ni = w.UsedRange.Rows.Count + 1\r\nw.Cells(i, 1).Value = \"\u58f2\u4e0a\/\u88dc\u52a9\"\r\n\r\ni = w.UsedRange.Rows.Count + 1\r\nrec.Open \"select '\u751f\u7523\u8005\u58f2\u4e0a\u9ad8',\u8cb8\u65b9\u88dc\u52a9\u79d1\u76ee,sum(\u8cb8\u65b9\u91d1\u984d)-sum(\u8cb8\u65b9\u7a0e\u984d) from tbl where \u8cb8\u65b9\u52d8\u5b9a\u79d1\u76ee = '\u751f\u7523\u8005\u58f2\u4e0a\u9ad8' group by \u8cb8\u65b9\u88dc\u52a9\u79d1\u76ee\"\r\nw.Cells(i, 2).CopyFromRecordset rec\r\nrec.Close\r\n\r\nw.Columns.AutoFit\r\n\r\nIf con.State = 1 Then con.Close\r\n\r\nApplication.StatusBar = False\r\n\r\nSelectDb = True\r\n\r\nExit Function\r\n\r\ntry:\r\n\r\n  If con.State = 1 Then con.Close\r\n  \r\n  Application.StatusBar = False\r\n  \r\n  MsgBox Err.Description\r\n  \r\n  SelectDb = False\r\n<\/pre>\n<p>\u8a72\u5f53\u30a2\u30c9\u30ec\u30b9\u3092\u53d6\u308a\u51fa\u3059\u3002<\/p>\n<pre class=\"lang:vb decode:true \">Set w = Sheets(\"\u96c6\u8a08\")\r\n  \r\nFor r = 1 To w.UsedRange.Rows.Count\r\n\r\n  If w.Cells(r, 2).Text = op1 And w.Cells(r, 3).Text = op2 Then\r\n  \r\n    SearchText = w.Cells(r, 4).Address(rowabsolute:=False, columnabsolute:=False)\r\n    Exit Function\r\n  \r\n  End If\r\n  \r\nNext r\r\n\r\nSearchText = \"A2\"<\/pre>\n<p>\u95a2\u6570\u767b\u9332<\/p>\n<pre class=\"lang:vb decode:true \">w.Cells(13, 5).Value = \"=\u96c6\u8a08!\" &amp; SearchText(\"\u696d\u8005\u4ed5\u5165\u9ad8\", \"\u52a0\u5de5\u4ed6\uff08\u8cb7\u53d6\uff09\")\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>DB\u306b\u767b\u9332\u3002 Application.StatusBar = &#8220;\u30c7\u30fc\u30bf\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u4e2d&#8230;&#8221; On Error GoTo try Set con = CreateObject(&#8220;adodb.connection&#8221;) co &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=2075\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;VBA \u5f25\u751fCSV\u64cd\u4f5c&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[5],"class_list":["post-2075","post","type-post","status-publish","format-standard","hentry","category-1","tag-vba"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2075","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2075"}],"version-history":[{"count":2,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2075\/revisions"}],"predecessor-version":[{"id":2077,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2075\/revisions\/2077"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}