{"id":3880,"date":"2020-11-10T23:03:46","date_gmt":"2020-11-10T14:03:46","guid":{"rendered":"https:\/\/okamurax.com\/?p=3880"},"modified":"2020-11-10T23:03:46","modified_gmt":"2020-11-10T14:03:46","slug":"vba-listbox%e5%8f%82%e8%80%83","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=3880","title":{"rendered":"VBA ListBox\u53c2\u8003"},"content":{"rendered":"<p>ListBox\u306e\u4f7f\u3044\u65b9\u3092\u5fd8\u308c\u3066\u3057\u307e\u3046\u306e\u3067\u3001\u3055\u3063\u304d\u4f5c\u3063\u305f\u3082\u306e\u3092\u30e1\u30e2\u4ee3\u308f\u308a\u3002<\/p>\n<pre class=\"lang:vb decode:true \">Private Sub CommandButton1_Click()\r\n\r\n' \u691c\u7d22\r\n\r\nTextBox1.Enabled = False\r\nCommandButton1.Enabled = False\r\n\r\nSet w = Sheets(\"dat\")\r\n\r\nr = 2\r\nDo While w.Cells(r, 1).Value &lt;&gt; \"\"\r\n\r\n  If TextBox1.Text = w.Cells(r, 1).Value Then\r\n\r\n    TextBox2.Text = w.Cells(r, 3).Value\r\n    TextBox3.Text = w.Cells(r, 10).Value\r\n    TextBox4.Text = w.Cells(r, 11).Value\r\n    TextBox5.Text = w.Cells(r, 17).Value\r\n    Label7.caption = r\r\n    CommandButton2.Enabled = True\r\n    \r\n    Call CheckDate\r\n    Exit Sub\r\n  \r\n  End If\r\n\r\nr = r + 1\r\nLoop\r\n\r\nTextBox1.Enabled = True\r\nCommandButton1.Enabled = True\r\n\r\nEnd Sub\r\n\r\nSub CheckDate()\r\n\r\nOn Error GoTo exception\r\n\r\ns = CDate(TextBox3.Text)\r\ne = CDate(TextBox4.Text)\r\ne = DateAdd(\"d\", 1, e)\r\n\r\n' \u4eca\u65e5\u304c\u958b\u59cb\u65e5\u3088\u308a\u5f8c\u3067\u7d42\u4e86\u65e5\u3088\u308a\u524d\r\n\r\nIf s &lt; Now And e &gt; Now Then\r\n  Label8.caption = \"OK\"\r\nElse\r\n  Label8.caption = \"NG\"\r\nEnd If\r\n    \r\nExit Sub\r\n\r\nexception:\r\n\r\nLabel8.caption = \"-\"\r\n\r\nEnd Sub\r\n\r\nPrivate Sub CommandButton2_Click()\r\n\r\n' \u4eca\u65e5\u306e\u65e5\u4ed8\u3092\u767b\u9332\r\n\r\nSet w = Sheets(\"dat\")\r\n\r\nIf TextBox5.Text &lt;&gt; \"\" Then\r\n  result = TextBox5.Text &amp; \", \" &amp; Month(Now) &amp; \"\/\" &amp; Day(Now)\r\nElse\r\n  result = Month(Now) &amp; \"\/\" &amp; Day(Now)\r\nEnd If\r\n\r\nIf TextBox6.Text &lt;&gt; \"\" Then\r\n  result = result &amp; \"(\" &amp; TextBox6.Text &amp; \")\"\r\nEnd If\r\n\r\nr = Label7.caption\r\nw.Cells(r, 17).Value = result\r\nTextBox5.Text = result\r\n\r\n' CommandButton2.Enabled = False\r\n\r\nw.Activate\r\nw.Rows(r).Select\r\n\r\nMsgBox \"\u767b\u9332\u3057\u307e\u3057\u305f\u3002\"\r\n\r\nEnd Sub\r\n\r\nPrivate Sub CommandButton3_Click()\r\n\r\n' \u30ad\u30e3\u30f3\u30bb\u30eb\r\n\r\nTextBox1.Enabled = True\r\nCommandButton1.Enabled = True\r\n\r\nTextBox1.Text = \"\"\r\nTextBox2.Text = \"\"\r\nTextBox3.Text = \"\"\r\nTextBox4.Text = \"\"\r\nTextBox5.Text = \"\"\r\nTextBox6.Text = \"\"\r\nLabel7.caption = \"\"\r\nLabel8.caption = \"\"\r\n\r\nCommandButton2.Enabled = False\r\nTextBox1.SetFocus\r\n    \r\nEnd Sub\r\n\r\nPrivate Sub CommandButton5_Click()\r\n\r\n' \u30b7\u30fc\u30c8\u66f8\u304d\u8fbc\u307f\r\n\r\nIf ListBox2.ListCount = 0 Then Exit Sub\r\n\r\nyesNo = vbYes\r\nIf TextBox2.Text = \"\" Then yesNo = MsgBox(\"\u6c0f\u540d\u304c\u3042\u308a\u307e\u305b\u3093\u3002\u767b\u9332\u3057\u307e\u3059\u304b\uff1f\", vbYesNo)\r\nIf yesNo = vbNo Then Exit Sub\r\n\r\nSet w = Sheets(\"\u58f2\u4e0a\")\r\n\r\nr = 1\r\nDo While w.Cells(r, 4).Value &lt;&gt; \"\" ' \u5546\u54c1\u540d\u3067\u6700\u7d42\u884c\u3092\u7b97\u51fa\r\n  r = r + 1\r\nLoop\r\ntargetRow = r\r\n\r\nmemberCode = TextBox1.Text\r\nmemberName = TextBox2.Text\r\n\r\nFor r = 0 To ListBox2.ListCount - 1\r\n\r\n  productName = ListBox2.List(r, 0)\r\n  unitPrice = ListBox2.List(r, 1)\r\n  amount = ListBox2.List(r, 2)\r\n  \r\n  w.Cells(targetRow, 1).Value = DateTime.Now\r\n  w.Cells(targetRow, 2).Value = memberCode\r\n  w.Cells(targetRow, 3).Value = memberName\r\n  w.Cells(targetRow, 4).Value = productName\r\n  w.Cells(targetRow, 5).Value = unitPrice\r\n  w.Cells(targetRow, 6).Value = amount\r\n  \r\n  targetRow = targetRow + 1\r\n  \r\nNext r\r\n\r\nw.Activate\r\nw.Rows(targetRow - 1).Select\r\n\r\nMsgBox \"\u767b\u9332\u3057\u307e\u3057\u305f\u3002\"\r\n\r\nEnd Sub\r\n\r\nPrivate Sub CommandButton6_Click()\r\n\r\n' \u30af\u30ea\u30a2\r\n\r\nComboBox1.Text = \"\"\r\nListBox2.Clear\r\nLabel12.caption = \"\"\r\n\r\nEnd Sub\r\n\r\nPrivate Sub UserForm_Initialize()\r\n\r\nTextBox1.SetFocus\r\nMasterSetup\r\n\r\nEnd Sub\r\n\r\nSub MasterSetup()\r\n\r\n' \u5546\u54c1\u30ea\u30b9\u30c8\r\n\r\nListBox1.ColumnCount = 2\r\n\r\nSet w = Sheets(\"master\")\r\n\r\nr = 2\r\nDo While w.Cells(r, 1).Value &lt;&gt; \"\"\r\n\r\n  productName = w.Cells(r, 1).Value\r\n  unitPrice = w.Cells(r, 2).Value\r\n  ListBox1.AddItem \"\"\r\n  ListBox1.List(ListBox1.ListCount - 1, 0) = productName\r\n  ListBox1.List(ListBox1.ListCount - 1, 1) = unitPrice\r\n\r\nr = r + 1\r\nLoop\r\n\r\n' \u58f2\u4e0a\u30ea\u30b9\u30c8\r\n\r\nListBox2.ColumnCount = 4\r\n\r\n\r\n' \u6570\u91cf\r\nFor i = 1 To 10\r\n  ComboBox1.AddItem i\r\nNext\r\n\r\nEnd Sub\r\n\r\nPrivate Sub CommandButton4_Click()\r\n\r\n' \u8ee2\u9001\r\n\r\nIf ListBox1.Text = \"\" Then\r\n  MsgBox \"\u5546\u54c1\u3092\u9078\u629e\u3057\u3066\u304f\u3060\u3055\u3044\u3002\"\r\n  Exit Sub\r\nEnd If\r\n\r\nIf ComboBox1.Text = \"\" Then\r\n  MsgBox \"\u6570\u91cf\u3092\u9078\u629e\u3057\u3066\u304f\u3060\u3055\u3044\u3002\"\r\n  Exit Sub\r\nEnd If\r\n\r\nproductName = ListBox1.List(ListBox1.ListIndex, 0)\r\nunitPrice = ListBox1.List(ListBox1.ListIndex, 1)\r\namount = ComboBox1.Text\r\n\r\nListBox2.AddItem \"\"\r\nListBox2.List(ListBox2.ListCount - 1, 0) = productName\r\nListBox2.List(ListBox2.ListCount - 1, 1) = unitPrice\r\nListBox2.List(ListBox2.ListCount - 1, 2) = amount\r\nListBox2.List(ListBox2.ListCount - 1, 3) = CDec(unitPrice) * CDec(amount)\r\n\r\nTotalPrice\r\n\r\nEnd Sub\r\n\r\nSub TotalPrice()\r\n\r\nresult = 0\r\nFor r = 0 To ListBox2.ListCount - 1\r\n  result = CDec(result) + CDec(ListBox2.List(r, 3))\r\nNext r\r\n\r\nLabel12.caption = \"\\ \" &amp; result\r\n\r\nEnd Sub\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ListBox\u306e\u4f7f\u3044\u65b9\u3092\u5fd8\u308c\u3066\u3057\u307e\u3046\u306e\u3067\u3001\u3055\u3063\u304d\u4f5c\u3063\u305f\u3082\u306e\u3092\u30e1\u30e2\u4ee3\u308f\u308a\u3002 Private Sub CommandButton1_Click() &#8216; \u691c\u7d22 TextBox1.Enabled = False CommandB &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=3880\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;VBA ListBox\u53c2\u8003&#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-3880","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\/3880","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=3880"}],"version-history":[{"count":1,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/3880\/revisions"}],"predecessor-version":[{"id":3881,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/3880\/revisions\/3881"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}