{"id":2241,"date":"2024-07-08T12:44:29","date_gmt":"2024-07-08T19:44:29","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=2241"},"modified":"2024-07-08T12:44:29","modified_gmt":"2024-07-08T19:44:29","slug":"433-run-a-vba-macro-when-certain-cells-change-in-excel","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/433-run-a-vba-macro-when-certain-cells-change-in-excel\/","title":{"rendered":"#433 Run a VBA macro when certain cells change in Excel"},"content":{"rendered":"<p>When you type an entry in cells A1:C10 on Sheet1, a message box is displayed.<br \/>\nIn the module sheet behind Sheet1 add this script<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nPrivate Sub Worksheet_Change(ByVal Target As Range)\r\n    Dim KeyCells As Range\r\n\r\n&#039; The variable KeyCells contains the cells that will\r\n    &#039; cause an alert when they are changed.\r\n    Set KeyCells = Range(&quot;A1:C10&quot;)\r\n\r\nIf Not Application.Intersect(KeyCells, Range(Target.Address)) _\r\n           Is Nothing Then\r\n\r\n&#039; Display a message when one of the designated cells has been \r\n        &#039; changed.\r\n        &#039; Place your code here.\r\n        MsgBox &quot;Cell &quot; &amp; Target.Address &amp; &quot; has changed.&quot;\r\n\r\nEnd If\r\nEnd Sub\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When you type an entry in cells A1:C10 on Sheet1, a message box is displayed. In the module sheet behind Sheet1 add this script Private Sub Worksheet_Change(ByVal Target As Range) Dim KeyCells As Range &#039; The variable KeyCells contains the cells that will &#039; cause an alert when they are changed. Set KeyCells = Range(&quot;A1:C10&quot;) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[107,15],"tags":[108,16],"class_list":["post-2241","post","type-post","status-publish","format-standard","hentry","category-excel","category-vba","tag-excel","tag-vba"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-A9","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":3351,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/08\/444-how-to-suppress-the-message-this-workbook-contains-links-and-automatically-select-dont-update-in-vba\/","url_meta":{"origin":2241,"position":0},"title":"#444 How to suppress the message &#8220;This workbook contains links&#8221; and automatically select &#8220;Don&#8217;t Update&#8221; in VBA","author":"gantovnik","date":"2024-08-02","format":false,"excerpt":"Sub OpenWorkbookWithoutUpdatingLinks() Dim wb As Workbook Application.AskToUpdateLinks = False Set wb = Workbooks.Open(\"C:\\Path\\To\\Your\\Workbook.xlsm\", UpdateLinks:=0) Application.AskToUpdateLinks = True End Sub","rel":"","context":"In &quot;vba&quot;","block_context":{"text":"vba","link":"https:\/\/gantovnik.com\/bio-tips\/category\/vba\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":407,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/02\/updating-all-fields-automatically-in-ms-word\/","url_meta":{"origin":2241,"position":1},"title":"#61 Updating All Fields Automatically in MS Word","author":"gantovnik","date":"2019-02-20","format":false,"excerpt":"[code language=\"python\"] Public Sub UpdateAllFields() 'Programmer: Vladimir Gantovnik 'Date: 01\/15\/2014 Dim rngStory As Word.Range Dim lngJunk As Long Dim oShp As Shape lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType For Each rngStory In ActiveDocument.StoryRanges 'Iterate through all linked stories Do On Error Resume Next rngStory.Fields.Update Select Case rngStory.StoryType Case 6, 7, 8, 9, 10,\u2026","rel":"","context":"In &quot;vba&quot;","block_context":{"text":"vba","link":"https:\/\/gantovnik.com\/bio-tips\/category\/vba\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2201,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/05\/428-python-script-to-open-excel-files-from-the-list-of-files-and-get-values-of-specific-cells-in-the-selected-sheet\/","url_meta":{"origin":2241,"position":2},"title":"#428 Python script to open Excel files from the list of files and get values of specific cells in the selected sheet","author":"gantovnik","date":"2024-05-26","format":false,"excerpt":"[code language=\"python\"] import xlwings as xw path = r\"D:\\projects\\wordpress\\ex428\\list_of_files.txt\" with open(path, 'r') as fp: filepaths = fp.readlines() sheet='Flight' for filepath in filepaths: filepath=filepath.strip() file = filepath wb=xw.Book(file) ws = wb.sheets[sheet] v1 = ws.range(\"F6\").value v2 = ws.range(\"F7\").value v3 = ws.range(\"F8\").value v4 = ws.range(\"F9\").value wb.app.quit() vmax=max(v1,v2,v3,v4) print(file) print(\"vmax\", vmax) [\/code] Output: [code\u2026","rel":"","context":"In &quot;excel&quot;","block_context":{"text":"excel","link":"https:\/\/gantovnik.com\/bio-tips\/category\/excel\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10459,"url":"https:\/\/gantovnik.com\/bio-tips\/2025\/09\/463-count-unique-values-in-excel-with-one-powerful-excel-formula\/","url_meta":{"origin":2241,"position":3},"title":"#463 Count Unique Values in Excel with One Powerful Excel Formula","author":"gantovnik","date":"2025-09-22","format":false,"excerpt":"Ever wonder how to count the number of unique numbers in a column without using PivotTables or helper columns? Here\u2019s a classic Excel array formula that does the job beautifully: =SUM(IF(FREQUENCY(A:A, A:A) > 0, 1)) How It Works FREQUENCY(A:A, A:A)Returns an array where only the first occurrence of each number\u2026","rel":"","context":"In &quot;excel&quot;","block_context":{"text":"excel","link":"https:\/\/gantovnik.com\/bio-tips\/category\/excel\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1690,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/11\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-3-2-2-3-2-2-4-2-2-2-2-2-3-2\/","url_meta":{"origin":2241,"position":4},"title":"#320 Game of Life using C#","author":"gantovnik","date":"2022-11-30","format":false,"excerpt":"Zip file: game-of-life Conway\u2019s\u00a0Game of Life\u00a0is a zero-player video game that uses a few basic rules to determine if cells live or die based on the density of their neighbors.","rel":"","context":"In &quot;C#&quot;","block_context":{"text":"C#","link":"https:\/\/gantovnik.com\/bio-tips\/category\/c\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/11\/ex320.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/11\/ex320.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/11\/ex320.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/11\/ex320.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/11\/ex320.png?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":1494,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/06\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-2-2-2-2-2-2-2-2-2-3-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":2241,"position":5},"title":"#285 Find min and max lengths of lines in HyperMesh database","author":"gantovnik","date":"2022-06-15","format":false,"excerpt":"#285 Find min and max lengths of lines in HyperMesh database [code language=\"python\"] proc myRand {min max} { set range [expr {$max - $min}] return [expr {$min + rand() * $range}] } proc create_random_lines {} { set n 30 for {set i 0} {$i < $n} {incr i} { set\u2026","rel":"","context":"In &quot;HyperMesh&quot;","block_context":{"text":"HyperMesh","link":"https:\/\/gantovnik.com\/bio-tips\/category\/hypermesh\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/06\/2022-06-15_143821.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/06\/2022-06-15_143821.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/06\/2022-06-15_143821.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/06\/2022-06-15_143821.jpg?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/06\/2022-06-15_143821.jpg?resize=1050%2C600&ssl=1 3x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=2241"}],"version-history":[{"count":1,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2241\/revisions"}],"predecessor-version":[{"id":2243,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/2241\/revisions\/2243"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=2241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=2241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=2241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}