excel vba - VBA- Sum values in Column B if name in column A is the same -
i try edit macro summing values in column b, doesnt work correctly:
here is, have:
option explicit sub main() collectarray "a", "d" dosum "d", "e", "a", "b" end sub ' collect array specific column , print new 1 without duplicates ' params: ' fromcolumn - column need remove duplicates ' tocolumn - reprint array without duplicates sub collectarray(fromcolumn string, tocolumn string) redim arr(0) string dim long = 1 range(fromcolumn & rows.count).end(xlup).row arr(ubound(arr)) = range(fromcolumn & i) redim preserve arr(ubound(arr) + 1) next redim preserve arr(ubound(arr) - 1) removeduplicate arr range(tocolumn & "1:" & tocolumn & range(tocolumn & rows.count).end(xlup).row).clearcontents = lbound(arr) ubound(arr) range(tocolumn & + 1) = arr(i) next end sub ' sums values 1 column against other column ' params: ' fromcolumn - column string match against ' tocolumn - sum printed ' originalcolumn - original column including duplicate ' valuecolumn - column values sum private sub dosum(fromcolumn string, tocolumn string, originalcolumn string, valuecolumn string) range(tocolumn & "1:" & tocolumn & range(tocolumn & rows.count).end(xlup).row).clearcontents dim long = 1 range(fromcolumn & rows.count).end(xlup).row range(tocolumn & i) = worksheetfunction.sumif(range(originalcolumn & ":" & originalcolumn), range(fromcolumn & i), range(valuecolumn & ":" & valuecolumn)) next end sub private sub removeduplicate(byref stringarray() string) dim lowbound$, upbound&, a&, b&, cur&, temparray() string if (not stringarray) = true exit sub lowbound = lbound(stringarray): upbound = ubound(stringarray) redim temparray(lowbound upbound) cur = lowbound: temparray(cur) = stringarray(lowbound) = lowbound + 1 upbound b = lowbound cur if lenb(temparray(b)) = lenb(stringarray(a)) if instrb(1, stringarray(a), temparray(b), vbbinarycompare) = 1 exit end if next b if b > cur cur = b temparray(cur) = stringarray(a) next redim preserve temparray(lowbound cur): stringarray = temparray end sub
macro copy names column (removes duplicates) column d, , values column b should sum according names column part removeduplicates doesnt work properly. can tell me/help me, can problem ?
sub createsummary() dim x long dim dict object set dict = createobject("scripting.dictionary") x = 1 range("a" & rows.count).end(xlup).row dict(cells(x, 1).value) = dict(cells(x, 1).value) + cells(x, 2).value next range("d1").resize(dict.count).value = application.transpose(dict.keys) range("e1").resize(dict.count).value = application.transpose(dict.items) end sub
Comments
Post a Comment