excel - VBA: Copying data to a worksheet that checks if record exists -
i have 2 worksheets, pretty input worksheet end user input data, , data sheet store data after enter in. input sheet has names in first 2 columns (first , last) , has 2 or 3 columns after of data person. right code takes 5 columns, reads dynamic array , writes datasheet worksheet. amount of names in input sheet can added or subtracted @ anytime. want know way excel check if names in data sheet, , if overwrite data person. if person doesn't exist create new line , copy data on person.
right code works copying entire array, finding next available row in datasheet, , writing entire array there. problem if have 5 people, , run code twice, 2 different instances of same person. can't change range activecell , overwrite latest iteration of input page because there different grouping variables people. when group1 selected on different page, input page populated group1 members, , group2 etc. need save them same sheet.
this code looks now:
dim behvdata() variant sheets("i_behavior").activate behvdata = range("a8", range("a8").end(xldown).offset(0, 4)) worksheets("behvdatasheet").select range("a3").select range(activecell.end(xldown), activecell.end(xldown).offset(ubound(behvdata, 1) - 1, 4)).value = behvdata erase behvdata i'm new vba , appreciate here.
could perhaps work you?
sub test() dim long dim nextavailablerow long dim existingdatarow long thisworkbook.sheets("i_behavior") = 8 .range("a8").end(xldown).row if worksheetfunction.isnumber(application.match(.range("a" & i).value, thisworkbook.sheets("behvdatasheet").range("a:a"), 0)) 'edit: update column e existing value in column existingdatarow = application.match(.range("a" & i).value, thisworkbook.sheets("behvdatasheet").range("a:a"), 0) thisworkbook.sheets("behvdatasheet").range("e" & existingdatarow) = .range("e" & i).value else nextavailablerow = lastrow("behvdatasheet", "a") + 1 thisworkbook.sheets("behvdatasheet").range("a" & nextavailablerow & ":e" & nextavailablerow) = .range("a" & & ":e" & i).value end if next end end sub public function lastrow(sheetname variant, col variant) long application.volatile true thisworkbook.sheets(sheetname) if .cells(.rows.count, col).value <> "" lastrow = .rows.count exit function end if lastrow = .cells(.rows.count, col).end(xlup).row end end function
Comments
Post a Comment