excel vba - execute Worksheet_Change when cell changed by a macro -
i have edited question initial posting since realized no macro activate worksheet_change function.
i using userform create macro edit cell. want worksheet take value 1 cell , create values in other cells. works manually, not in via macro!
from userform:
sub writeoperatingfunds(dates, description, money) dim ws2 worksheet set ws2 = thisworkbook.worksheets("operatingfunds") 'find first empty row in database irow = ws2.cells.find(what:="*", searchorder:=xlrows, _ searchdirection:=xlprevious, lookin:=xlvalues).row + 1 ws2.cells(irow, 1).value = dates ws2.cells(irow, 2).value = description ws2.cells(irow, 3).value = money end sub
and worksheet:
private sub worksheet_change(byval target range) dim change string dim chngrow long dim incrow long dim exprow long dim totrow long dim income long dim expense long dim total long set ws = thisworkbook.worksheets("operatingfunds") totrow = ws.cells(ws.rows.count, 5).end(xlup).row 'finds bottom of 'total' column 'looks target in range if not application.intersect(target, ws.range("c3", ws.cells(totrow + 1, 4))) nothing change = target.address(false, false) chngrow = ws.range(change).row 'get last rows of range columns incrow = ws.range("c" & ws.rows.count).end(xlup).row exprow = ws.range("d" & ws.rows.count).end(xlup).row application.enableevents = false 'to prevent endless loop & not record changes macro 'if total column empty if ws.cells(chngrow, 5) = "" income = application.worksheetfunction.sum(ws.range("c3", ws.cells(totrow + 1, 3))) expense = application.worksheetfunction.sum(ws.range("d3", ws.cells(totrow + 1, 4))) total = income + expense ws.cells(chngrow, 5) = total 'if total column not empty (i.e. needs rewritten) elseif ws.cells(chngrow, 5) <> "" income = application.worksheetfunction.sum(ws.range("c3", ws.cells(chngrow, 3))) expense = application.worksheetfunction.sum(ws.range("d3", ws.cells(chngrow, 4))) total = income + expense ws.cells(chngrow, 5) = total end if else msgbox "else thrown." exit sub end if application.enableevents = true 'so future changes can read end sub
i don't want pretentious answer own question, friend helped me find solution, , maybe others similar problem.
the key factor i'm using button on userform write data cell. it's not "changing" cell when write "cells(#,#) = value". in code, needed turn sub public , then
call thisworkbook.worksheets("worksheetname").worksheet_change(target.address)
this made work! example me: https://bytes.com/topic/access/answers/767919-trigger-click-event-button-another-form
Comments
Post a Comment