excel vba sum dynamic last column -


macro purpose:

  • macro loop through folder of csv files
  • sum last 2 columns , place result in next 2 adjacent cells on row 1

example: file1.csv last 2 columns k , l sum column l placed in cell m1 , sum column k placed in cell n1. file2.csv may have different amount of columns have been trying setup variables these 4 requirements.

closest example find is: use last column range(f:lastcolumn)

issue having have set last column in file1.csv variable syntax not allowed when summing variable:

range("m1") = application.worksheetfunction.sum(columns("lastcolletter:lastcolletter")) 

not sure how fix above syntax. full code below:

    sub macro1_measure1_2() ' ' dim my_filename     string dim lastcol         long dim lastcolletter   string '--------------------------------------------  workbooks.open filename:="file1.csv" '  ' variable1: store last column ' https://stackoverflow.com/questions/16941083/use-last-column-for-a-rangeflastcolumn 'lastcol = 12 'manual test lastcol = cells(1, columns.count).end(xltoleft).column 'option-2 debug.print lastcol lastcolletter = getcolumnletter(lastcol)  ' check range we've set debug.print lastcolletter  ' variable2: store 2nd last column ' logic = lastcol -1 col  'range("m1") = application.worksheetfunction.sum(columns("l:l")) 'option-1 works needs dynamic range("m1") = application.worksheetfunction.sum(columns("lastcolletter:lastcolletter")) 'option-2  end sub  function getcolumnletter(colnum long) string     dim varr     varr = split(cells(1, colnum).address(true, false), "$")     getcolumnletter = varr(0) end function   ''''''''''' ''' end ''' ''''''''''' 


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -