{"id":3437,"date":"2023-08-05T23:34:32","date_gmt":"2023-08-05T15:34:32","guid":{"rendered":"https:\/\/www.bytetape.com\/?p=3437"},"modified":"2023-09-01T22:55:03","modified_gmt":"2023-09-01T14:55:03","slug":"vba-%e8%ae%a9%e5%9b%be%e7%89%87%e5%85%85%e6%bb%a1%e6%89%80%e5%9c%a8%e5%8d%95%e5%85%83%e6%a0%bc","status":"publish","type":"post","link":"https:\/\/www.bytetape.com\/?p=3437","title":{"rendered":"\u4f7f\u7528VBA\u8ba9\u56fe\u7247\u5b8c\u7f8e\u5145\u6ee1\u5355\u5143\u683c\uff0c\u753b\u9f99\u70b9\u775b\u4e4b\u6280\uff01"},"content":{"rendered":"\n<p>\u5927\u5bb6\u597d\uff0c\u6b22\u8fce\u6765\u5230\u6211\u7684\u5c0f\u5929\u5730\uff01\u4eca\u5929\uff0c\u6211\u8981\u4e0e\u5927\u5bb6\u5206\u4eab\u4e00\u4e2aVBA\u5c0f\u6280\u5de7\uff0c\u8ba9\u4f60\u7684Excel\u56fe\u7247\u5728\u5355\u5143\u683c\u4e2d\u7115\u53d1\u51fa\u5168\u65b0\u7684\u6d3b\u529b\u3002\u5982\u679c\u4f60\u662f\u4e00\u4e2a\u7231\u597d\u7f8e\u5b66\u7684\u6570\u636e\u8fbe\u4eba\uff0c\u6216\u8005\u662f\u60f3\u8981\u4e3a\u4f60\u7684\u62a5\u544a\u589e\u8272\u6dfb\u5f69\uff0c\u90a3\u4e48\u8fd9\u7bc7\u6587\u7ae0\u7edd\u5bf9\u4e0d\u5bb9\u9519\u8fc7\uff01\u5e9f\u8bdd\u4e0d\u591a\u8bf4\uff0c\u8ba9\u6211\u4eec\u4e00\u8d77\u63ed\u5f00\u8fd9\u4e2a\u795e\u79d8\u7684VBA\u6280\u5de7\u9762\u7eb1\u5427\uff01<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u77ac\u95f4\u8ba9\u56fe\u7247\u5145\u6ee1\u5355\u5143\u683c\u7684\u9b54\u6cd5<\/h3>\n\n\n\n<p>\u6bcf\u4e2a\u4eba\u90fd\u77e5\u9053\uff0cExcel\u4e2d\u7684\u56fe\u7247\u867d\u7136\u65b9\u4fbf\u7f8e\u89c2\uff0c\u4f46\u5e38\u5e38\u96be\u4ee5\u4f7f\u5176\u4e0e\u5355\u5143\u683c\u5b8c\u7f8e\u878d\u5408\u3002\u522b\u62c5\u5fc3\uff0cVBA\u5c31\u662f\u4f60\u7684\u5c0f\u52a9\u624b\uff01\u4e0b\u9762\u662f\u4e00\u4e2a\u795e\u5947\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u8ba9\u4f60\u7684\u56fe\u7247\u77ac\u95f4\u5145\u6ee1\u6240\u5728\u5355\u5143\u683c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bytetape.com\/wp-content\/uploads\/2023\/08\/image-1.png\" alt=\"\" class=\"wp-image-3442\" style=\"width:399px;height:309px\" width=\"399\" height=\"309\" srcset=\"https:\/\/www.bytetape.com\/wp-content\/uploads\/2023\/08\/image-1.png 797w, https:\/\/www.bytetape.com\/wp-content\/uploads\/2023\/08\/image-1-300x232.png 300w, https:\/\/www.bytetape.com\/wp-content\/uploads\/2023\/08\/image-1-768x595.png 768w\" sizes=\"auto, (max-width: 399px) 100vw, 399px\" \/><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"visualbasic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Sub PicInCell()\n  \n  Dim Cell As Range           '\u56fe\u7247\u6240\u5728\u5355\u5143\u683c\n  Dim picShape As Shape       '\u56fe\u7247shape\u5bf9\u8c61\n  Dim scaleValue As Double\n  \n  Application.ScreenUpdating = False\n  '\u4fdd\u5b58\u5f53\u524d\u7684\u663e\u793a\u6bd4\u4f8b\n  Dim nZoom As Integer\n  nZoom = ActiveWindow.Zoom\n  '\u663e\u793a\u6bd4\u4f8b\u8c03\u6574\u4e3a100%\n  ActiveWindow.Zoom = 100\n  \n  '\u904d\u5386\u6240\u6709\u56fe\u7247\n  For Each picShape In ActiveSheet.Shapes\n    If picShape.Type = msoPicture Then '\u5982\u679c\u662f\u56fe\u7247\n      Set Cell = picShape.TopLeftCell '\u8bbe\u7f6e\u63d2\u5165\u6279\u6ce8\u7684\u5355\u5143\u683c\n      \n      '\u6062\u590d\u56fe\u7247\u539f\u59cb\u5c3a\u5bf8\uff0c\u4e3a\u4e86\u4fdd\u6301\u539f\u6bd4\u4f8b\n      picShape.ScaleHeight 1, msoTrue\n      picShape.ScaleWidth 1, msoTrue\n      \n      '\u91cd\u65b0\u8bbe\u7f6e\u56fe\u7247\u7684\u5c3a\u5bf8\u53ca\u4f4d\u7f6e\n      scaleValue = Application.Min(1, Application.Min(Cell.Height \/ picShape.Height, Cell.Width \/ picShape.Width))\n      picShape.ScaleHeight scaleValue, msoTrue\n      picShape.ScaleWidth scaleValue, msoTrue\n      \n      picShape.Height = picShape.Height - 1\n      picShape.Width = picShape.Width - 1\n      picShape.Top = WorksheetFunction.RoundUp(Cell.Top + (Cell.Height - picShape.Height) \/ 2, 0)\n      picShape.Left = WorksheetFunction.RoundUp(Cell.Left + (Cell.Width - picShape.Width) \/ 2, 0)\n    End If\n  Next picShape\n  \n  '\u8fd8\u539f\u663e\u793a\u6bd4\u4f8b\n  ActiveWindow.Zoom = nZoom\n  Application.ScreenUpdating = True\n  \nEnd Sub<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u9b54\u6cd5\u89e3\u6790<\/h3>\n\n\n\n<p>\u8ba9\u6211\u4eec\u4e00\u6b65\u6b65\u89e3\u6790\u8fd9\u6bb5\u795e\u5947\u4ee3\u7801\u7684\u9b54\u529b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u4ee3\u7801\u9996\u5148\u5173\u95ed\u4e86\u5c4f\u5e55\u66f4\u65b0\uff0c\u4ee5\u786e\u4fdd\u9b54\u6cd5\u64cd\u4f5c\u4e0d\u4f1a\u5f71\u54cd\u7528\u6237\u7684\u89c6\u89c9\u4f53\u9a8c\u3002<\/li>\n\n\n\n<li>\u6211\u4eec\u4fdd\u5b58\u4e86\u5f53\u524d\u7684\u663e\u793a\u6bd4\u4f8b\uff0c\u4e3a\u540e\u7eed\u8fd8\u539f\u505a\u51c6\u5907\u3002\u7136\u540e\uff0c\u5c06\u663e\u793a\u6bd4\u4f8b\u8bbe\u7f6e\u4e3a100%\uff0c\u786e\u4fdd\u56fe\u7247\u7684\u64cd\u4f5c\u4e0d\u53d7\u7f29\u653e\u5f71\u54cd\u3002<\/li>\n\n\n\n<li>\u63a5\u4e0b\u6765\uff0c\u4ee3\u7801\u5f00\u59cb\u904d\u5386\u6240\u6709\u7684\u56fe\u7247\u5f62\u72b6\u3002\u5982\u679c\u5f62\u72b6\u7c7b\u578b\u4e3a\u56fe\u7247\uff08<code>msoPicture<\/code>\uff09\uff0c\u5219\u5f00\u59cb\u4e0b\u4e00\u6b65\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li>\u4ee3\u7801\u6839\u636e\u56fe\u7247\u7684\u6240\u5728\u5355\u5143\u683c\uff0c\u91cd\u65b0\u8c03\u6574\u56fe\u7247\u7684\u5c3a\u5bf8\u548c\u4f4d\u7f6e\uff0c\u4f7f\u4e4b\u5145\u6ee1\u6574\u4e2a\u5355\u5143\u683c\u3002\u8fd9\u8ba9\u4f60\u7684\u56fe\u7247\u4e0d\u518d\u53d7\u9650\u4e8e\u5355\u5143\u683c\u5927\u5c0f\uff0c\u7115\u53d1\u51fa\u5168\u65b0\u7684\u751f\u547d\u529b\uff01<\/li>\n\n\n\n<li>\u6700\u540e\uff0c\u4ee3\u7801\u8fd8\u539f\u4e86\u4e4b\u524d\u4fdd\u5b58\u7684\u663e\u793a\u6bd4\u4f8b\uff0c\u5e76\u6253\u5f00\u5c4f\u5e55\u66f4\u65b0\uff0c\u8ba9\u4f60\u6b23\u8d4f\u5230\u8fd9\u4e2a\u7f8e\u5999\u7684\u9b54\u6cd5\u6548\u679c\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u5145\u5b9e\u4f60\u7684Excel\u4e16\u754c<\/h3>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u4e2a\u7b80\u5355\u800c\u53c8\u5f3a\u5927\u7684VBA\u9b54\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728Excel\u4e2d\u5b9e\u73b0\u8ba9\u56fe\u7247\u5145\u6ee1\u5355\u5143\u683c\u7684\u6548\u679c\uff0c\u4e3a\u4f60\u7684\u62a5\u544a\u3001\u8868\u683c\u6dfb\u52a0\u4e00\u62b9\u89c6\u89c9\u7684\u9b45\u529b\u3002\u8bd5\u7740\u5c06\u8fd9\u4e2a\u6280\u5de7\u5e94\u7528\u5230\u4f60\u7684\u9879\u76ee\u4e2d\uff0c\u770b\u770b\u5b83\u4f1a\u4e3a\u4f60\u7684\u6570\u636e\u4e16\u754c\u5e26\u6765\u600e\u6837\u7684\u53d8\u5316\u5427\uff01\u5982\u679c\u4f60\u5bf9\u8fd9\u4e2a\u6280\u5de7\u6709\u4efb\u4f55\u7591\u95ee\u6216\u60f3\u6cd5\uff0c\u6b22\u8fce\u5728\u4e0b\u65b9\u7559\u8a00\uff0c\u8ba9\u6211\u4eec\u4e00\u8d77\u5206\u4eabVBA\u7684\u5947\u5999\u4e4b\u65c5\u5427\uff01\u611f\u8c22\u4f60\u7684\u9605\u8bfb\uff0c\u6211\u4eec\u4e0b\u6b21\u518d\u89c1\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u6b22\u8fce\u6765\u5230\u6211\u7684\u5c0f\u5929\u5730\uff01\u4eca\u5929\uff0c\u6211\u8981\u4e0e&#8230; <\/p>\n","protected":false},"author":1,"featured_media":3438,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[40],"tags":[61,36,69,70,49],"class_list":["post-3437","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vba","tag-excel","tag-vba","tag-69","tag-70","tag-49"],"_links":{"self":[{"href":"https:\/\/www.bytetape.com\/index.php?rest_route=\/wp\/v2\/posts\/3437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bytetape.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bytetape.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bytetape.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bytetape.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3437"}],"version-history":[{"count":6,"href":"https:\/\/www.bytetape.com\/index.php?rest_route=\/wp\/v2\/posts\/3437\/revisions"}],"predecessor-version":[{"id":3599,"href":"https:\/\/www.bytetape.com\/index.php?rest_route=\/wp\/v2\/posts\/3437\/revisions\/3599"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bytetape.com\/index.php?rest_route=\/wp\/v2\/media\/3438"}],"wp:attachment":[{"href":"https:\/\/www.bytetape.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bytetape.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bytetape.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}