vba - Excel VBS do Muliple this 1 after the other on same sheet -


i have work book, need create button click for, run evey month. need multiple procedures on same sheet in set order.

so far below code have function. need in ordereing in correct way runs following list, , maybe tidying if , little if know of beeter way code it.

  1. increase cell l3 value 1
  2. add 1 month dates in cells c7 & l7
  3. copy cell n40 , paste value n42
  4. copy cell g36 , paste value g37
  5. copy cell g49 , paste value g50
  6. copy range n12:n27 , paste values j12:j27
  7. saveas "payment l3 l7.xlsm"

here code have far.

    private sub commandbutton3_click()          dim mpay range          dim mtarget range          dim ciscurrent range          dim cistarget range          dim taxcurrent range          dim taxtarget range          dim ttdcurrent range          dim ttdtarget range          dim ttd string     'define ranges          set ttdcurrent = ws.range("n40")           set ttdtarget = ws.range("n42")          set mpay = ws.range("n12:n27")           set mtarget = ws.range("j12:j27")          set ciscurrent = ws.range("g36")           set cistarget = ws.range("g37")          set taxcurrent = ws.range("g49")           set taxtarget = ws.range("g50")     'copy , paste special-add          ttdcurrent.copy           ttdtarget.pastespecial xlvalues          ttdcurrent.copy           ttdtarget.pastespecial xlvalues          mpay.copy           mtarget.pastespecial xlvalues          ciscurrent.copy           cistarget.pastespecial xlvalues          taxcurrent.copy           taxtarget.pastespecial xlvalues end sub 

i have managed come code cowrk sof rchanging date, increaseing l3 1 , saving document.

increase l3, , add 1 month dates:

private sub commandbutton4_click() dim rdate range dim rdate2 range  set rdate = activesheet.range("c7") set rdate2 = activesheet.range("l7")  activesheet     range("l3").value = range("l3").value + 1     rdate.value = dateadd("m", 1, rdate.value)         rdate.value = dateserial(year(rdate), month(rdate) + 1, 0)     rdate2.value = dateadd("m", 1, rdate2.value)         rdate2.value = dateserial(year(rdate), month(rdate) + 1, 0)  end end sub 

this code saving as:

private sub commandbutton6_click() dim mypath string, myrange range, mydate range  mypath = activeworkbook.path set mydate = sheets("sub con payment form").range("l7") set myrange = sheets("sub con payment form").range("l3") 'with name of cell thisworkbook.saveas filename:=mypath & "\" & "payment" & " " & myrange.value & " " & mydate.text & ".xlsm" end sub 

any can provide appreciated!!!!

i thinking asking more professional , efficient way code code? if case, perhaps got few advises you:

  1. use option explicit in code . make sure declare object before using it.
  2. add application.screenupdating =false enhance macro performance.
  3. the copy macro substitute writing formula in cell if going copy value same cells. e.g. can code cell(n42) formula =n40 copy value automatically whe cell(n40) updated. indirectly, reduce declaration of variables in code. therefore performance increased.
  4. normally, set "pattern" : workbook.worksheet variable. e.g.

    dim myworkbook workbook dim myworksheet worksheet set myworkbook = workbooks("book1.xlsm") set myworksheet = workbooktemplate.sheets("worksheetname") 

    than use them in performing transaction/tasks

  5. lastly, please use meaningful variables ease future enhancement. if interesting know more, can read book call "clean code"


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -