excel - VBA check for value in a range -
i trying loop through column , if cells = "what i'm lookng for" something. have far, i'm off in if statement check "name":
option explicit sub test() dim wksdest worksheet dim wkssource worksheet dim rngsource range dim name string dim lastrow long dim lastcol long dim c long application.screenupdating = false set wkssource = worksheets("sheet1") wkssource lastcol = .cells(1, .columns.count).end(xltoleft).column c = 16 20 lastrow = .cells(.rows.count, c).end(xlup).row set rngsource = .range(.cells(5, 16), .cells(lastrow, 16)) name = rngsource.value if name = "mark" end if next c end application.screenupdating = true 'msgbox "done!", vbexclamation end sub
ok chris maybe bit of simplification required few assumptions. doesn't seem lastcol being used - let's assume column want loop through. loop has fixed start , end values yet determining lastrow - let's assume want start row 5 (in code) , loop lastrow in lastcol. in order determine lastcol must have data in row using - let's assume there values in row 1 in columns column want loop 16 (in code). if want (if) test single (string) value in case must arrange rngsource single cell value. don't need assign variable unless need use again. finally, if want check other values may want consider using select case structure in place of if structure. have @ following , change assumptions meet requirement - luck.
sub test() dim wksdest worksheet dim wkssource worksheet dim rngsource range dim name string dim lastrow long dim lastcol long dim c long application.screenupdating = false set wkssource = worksheets("sheet1") wkssource lastcol = .cells(1, .columns.count).end(xltoleft).column lastrow = .cells(rows.count, lastcol).end(xlup).row firstrow = 5 c = firstrow lastrow if .range(.cells(c, lastcol), .cells(c, lastcol)).value = "mark" msgbox ("do something") end if next c end end sub
Comments
Post a Comment