{"id":2187,"date":"2024-12-26T15:17:11","date_gmt":"2024-12-26T07:17:11","guid":{"rendered":"https:\/\/blog.quantoyo.com\/?p=2187"},"modified":"2024-12-26T15:17:11","modified_gmt":"2024-12-26T07:17:11","slug":"excel-vba-evaluate","status":"publish","type":"post","link":"https:\/\/blog.quantoyo.com\/?p=2187","title":{"rendered":"Excel VBA-Evaluate"},"content":{"rendered":"\n<p>Evaluate (\u4e5f\u5c31\u662f js \u88e1\u9762\u7684 eval ) \u53ef\u4ee5\u5728 VBA \u4e2d\u5c07\u5b57\u4e32\u8f49\u578b\u5225<\/p>\n\n\n\n<p>ex.  a = Evaluate(&#8220;1&#8221;) \u76f8\u7576\u65bc a = 1<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>\u4f46\u662fEvaluate \u7684\u4f5c\u7528\u7bc4\u570d(scope) \u6709\u53ef\u80fd\u8ddf\u60f3\u50cf\u4e2d\u4e0d\u4e00\u6a23<\/p>\n\n\n\n<p>\u6211\u5728\u958b\u767c\u4e00\u500b\u8a08\u7b97\u5de5\u5177\u6642\uff0c\u539f\u672c\u4f7f\u7528\u4e86\u4e0d\u5c11 Evaluate \u4f86\u505a\u70ba\u5b57\u4e32\u8f49\u578b\u6210Double \u7684\u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>\u7a0b\u5f0f\u7247\u6bb5\uff1a\n\nDo Until lM = ubM - 1 Or (Evaluate(m) >= Evaluate(arM(lM)) And Evaluate(m) &lt; Evaluate(arM(lM + 1)))\n<\/code><\/pre>\n\n\n\n<p>\u958b\u767c\u968e\u6bb5\u6e2c\u8a66\u90fd\u53ef\u4ee5\u6b63\u5e38\u904b\u4f5c\uff0c\u76f4\u5230\u70ba\u4e86\u65b9\u4fbf\u4f7f\u7528\uff0c\u6dfb\u52a0\u4e86\u4e00\u500b\u6309\u9215\u5728\u9801\u9762\u4e0a\u89f8\u767c\uff0c\u958b\u59cb\u6703\u986f\u793a \u201c\u7269\u4ef6\u4e0d\u652f\u63f4\u6b64\u5c6c\u6027\u6216\u65b9\u6cd5 (\u932f\u8aa4 438)\u201d<\/p>\n\n\n\n<p>\u932f\u8aa4\u767c\u751f\u5728\u4e0a\u9762\u5beb\u9019\u884c<\/p>\n\n\n\n<p>\u7d93\u904e\u7814\u7a76\u767c\u73fe\u554f\u984c\u51fa\u5728Evaluate \u4e26\u4e0d\u662f\u55ae\u7d14\u7684\u5c07\u5b57\u4e32\u8f49\u578b\uff0c\u9700\u8981\u8003\u616e\u4e0a\u4e0b\u6587\u95dc\u4fc2<\/p>\n\n\n\n<p><code>Evaluate(m)<\/code> \u6703\u5617\u8a66\u5c07 <code>m<\/code> \u89e3\u91cb\u70ba\u4e00\u500b Excel \u8868\u9054\u5f0f\uff0c\u5982\u679c <code>m<\/code> \u662f\u7d14\u6578\u503c\u6216\u5b57\u4e32\uff0c\u53ef\u80fd\u7121\u6cd5\u6b63\u78ba\u57f7\u884c\u3002<\/p>\n\n\n\n<p>\u53e6\u5916\uff0c\u7576\u6309\u9215\u89f8\u767c\u5de8\u96c6\u6642\uff0c\u5de8\u96c6\u57f7\u884c\u7684\u4e0a\u4e0b\u6587\u53ef\u80fd\u8207\u76f4\u63a5\u57f7\u884c\u4e0d\u540c\uff0c<code>Evaluate<\/code> \u4f9d\u8cf4\u7684\u5de5\u4f5c\u8868\u53ef\u80fd\u8207\u7576\u524d\u5de5\u4f5c\u8868\u4e0d\u540c\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u4e0d\u61c9\u8a72\u4f7f\u7528<code>Evaluate<\/code>\uff0c<code>Evaluate<\/code>\u66f4\u504f\u5411\u65bc\u5c07\u5132\u5b58\u683c\u4e2d\u7684\u6587\u5b57\u8f49\u6210\u7a0b\u5f0f\u78bc\u7528\u7684\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u89e3\u6c7a\u65b9\u6cd5\uff1a\u5c07 <code>Evaluate<\/code> \u6539\u6210 CDbl (Change Double) <\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>\u6539\u6210\u9019\u6a23\uff1a\n\nDo Until lM = ubM - 1 Or (CDbl(m) >= CDbl(arM(lM)) And CDbl(m) &lt; CDbl(arM(lM + 1)))\n<\/code><\/pre>\n\n\n\n<p>\u5373\u53ef\u89e3\u6c7a\u6b64\u554f\u984c<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u76f8\u95dc\u529f\u80fd\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u51fd\u6578<\/th><th>\u529f\u80fd<\/th><th>\u7bc4\u4f8b<\/th><\/tr><\/thead><tbody><tr><td><code>CInt<\/code><\/td><td>\u5c07\u503c\u8f49\u63db\u70ba\u6574\u6578\uff08\u56db\u6368\u4e94\u5165\u5230\u6700\u63a5\u8fd1\u7684\u6574\u6578\uff09<\/td><td><code>CInt(5.7)<\/code> \u2192 <code>6<\/code><\/td><\/tr><tr><td><code>CLng<\/code><\/td><td>\u5c07\u503c\u8f49\u63db\u70ba\u9577\u6574\u6578\uff08Long\uff09<\/td><td><code>CLng(123456789.5)<\/code> \u2192 <code>123456790<\/code><\/td><\/tr><tr><td><code>CDbl<\/code><\/td><td>\u5c07\u503c\u8f49\u63db\u70ba\u96d9\u7cbe\u5ea6\u6d6e\u9ede\u6578\uff08Double\uff09<\/td><td><code>CDbl(\"3.14159\")<\/code> \u2192 <code>3.14159<\/code><\/td><\/tr><tr><td><code>CSng<\/code><\/td><td>\u5c07\u503c\u8f49\u63db\u70ba\u55ae\u7cbe\u5ea6\u6d6e\u9ede\u6578\uff08Single\uff09<\/td><td><code>CSng(3.14159)<\/code> \u2192 <code>3.14159<\/code><\/td><\/tr><tr><td><code>CDec<\/code><\/td><td>\u5c07\u503c\u8f49\u63db\u70ba\u5341\u9032\u5236\u6578\u5b57\uff08Decimal\uff0c\u7f55\u7528\uff09<\/td><td><code>CDec(12345.6789)<\/code> \u2192 <code>12345.6789<\/code><\/td><\/tr><tr><td><code>CStr<\/code><\/td><td>\u5c07\u503c\u8f49\u63db\u70ba\u5b57\u4e32<\/td><td><code>CStr(123.45)<\/code> \u2192 <code>\"123.45\"<\/code><\/td><\/tr><tr><td><code>Str<\/code><\/td><td>\u5c07\u6578\u5b57\u8f49\u63db\u70ba\u5b57\u4e32\uff08\u6578\u5b57\u524d\u6709\u7a7a\u767d\u8868\u793a\u6b63\u8ca0\u865f\uff09#\u6bd4\u8f03\u4e0d\u597d\u7528<\/td><td><code>Str(123.45)<\/code> \u2192 <code>\" 123.45\"<\/code><\/td><\/tr><tr><td><code>CDate<\/code><\/td><td>\u5c07\u503c\u8f49\u63db\u70ba\u65e5\u671f<\/td><td><code>CDate(\"2024-12-26\")<\/code> \u2192 <code>#2024-12-26#<\/code><\/td><\/tr><tr><td><code>DateValue<\/code><\/td><td>\u5c07\u5b57\u4e32\u8f49\u63db\u70ba\u65e5\u671f\uff08\u50c5\u65e5\u671f\u90e8\u5206\uff09<\/td><td><code>DateValue(\"2024-12-26\")<\/code> \u2192 <code>#2024-12-26#<\/code><\/td><\/tr><tr><td><code>TimeValue<\/code><\/td><td>\u5c07\u5b57\u4e32\u8f49\u63db\u70ba\u6642\u9593\uff08\u50c5\u6642\u9593\u90e8\u5206\uff09<\/td><td><code>TimeValue(\"14:30:00\")<\/code> \u2192 <code>#14:30:00#<\/code><\/td><\/tr><tr><td><code>CBool<\/code><\/td><td>\u5c07\u503c\u8f49\u63db\u70ba\u5e03\u6797\u503c\uff08True \u6216 False\uff09<\/td><td><code>CBool(1)<\/code> \u2192 <code>True<\/code><br><code>CBool(0)<\/code> \u2192 <code>False<\/code><\/td><\/tr><tr><td><code>VarType<\/code><\/td><td>\u7372\u53d6\u8b8a\u6578\u7684\u985e\u578b\uff08\u8fd4\u56de\u4e00\u500b\u6574\u6578\u578b\u4ee3\u78bc\uff09<\/td><td><code>VarType(123.45)<\/code> \u2192 <code>5<\/code> (Double)<\/td><\/tr><tr><td><code>CVErr<\/code><\/td><td>\u5c07\u6578\u503c\u8f49\u63db\u70ba\u81ea\u5b9a\u7fa9\u932f\u8aa4\u985e\u578b\uff08Variant\uff09<\/td><td><code>CVErr(513)<\/code> \u2192 <code>#N\/A<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5feb\u901f\u5c0d\u7167\u8868<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u985e\u578b<\/th><th>\u63a8\u85a6\u51fd\u6578<\/th><\/tr><\/thead><tbody><tr><td><strong>\u6574\u6578<\/strong><\/td><td><code>CInt<\/code>\u3001<code>CLng<\/code><\/td><\/tr><tr><td><strong>\u6d6e\u9ede\u6578<\/strong><\/td><td><code>CDbl<\/code>\u3001<code>CSng<\/code><\/td><\/tr><tr><td><strong>\u6587\u5b57<\/strong><\/td><td><code>CStr<\/code><\/td><\/tr><tr><td><strong>\u65e5\u671f\u6642\u9593<\/strong><\/td><td><code>CDate<\/code>\u3001<code>DateValue<\/code>\u3001<code>TimeValue<\/code><\/td><\/tr><tr><td><strong>\u5e03\u6797\u503c<\/strong><\/td><td><code>CBool<\/code><\/td><\/tr><tr><td><strong>\u81ea\u8a02\u932f\u8aa4<\/strong><\/td><td><code>CVErr<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Evaluate (\u4e5f\u5c31\u662f js \u88e1\u9762\u7684 eval ) \u53ef\u4ee5\u5728 VBA \u4e2d\u5c07\u5b57\u4e32\u8f49\u578b\u5225 ex. a = Eva &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.quantoyo.com\/?p=2187\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Excel VBA-Evaluate\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[377,374,370,372,368,369,371,373,379,375,365,363,376,378,380,364,367,366],"class_list":["post-2187","post","type-post","status-publish","format-standard","hentry","category-technology","tag-cbool","tag-cdate","tag-cdbl","tag-cdec","tag-cint","tag-clng","tag-csng","tag-cstr","tag-cverr","tag-datevalue","tag-evaluate","tag-excel","tag-timevalue","tag-vartype","tag-vb","tag-vba","tag-367","tag-366"],"_links":{"self":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/2187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2187"}],"version-history":[{"count":1,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/2187\/revisions"}],"predecessor-version":[{"id":2188,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=\/wp\/v2\/posts\/2187\/revisions\/2188"}],"wp:attachment":[{"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.quantoyo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}