excel - Altering code to look through multiple workbooks -
i have below code , alter instead of going through current spreadsheet, looks through multiple workbooks stored in folder on c drive.
sub test() dim wb1 workbook, wb2 workbook dim ws1 worksheet, ws2 worksheet dim copyfrom range dim lrow long '<~~ not integer. might give error in higher versions of excel dim strsearch string set wb1 = thisworkbook ' application.workbooks.open("c:\sample.xlsx") set ws1 = wb1.worksheets("fca") strsearch = activesheet.inputname.text ws1 '~~> remove filters .autofiltermode = false lrow = .range("h" & .rows.count).end(xlup).row .range("h1:h" & lrow) .autofilter field:=1, criteria1:="=*" & strsearch & "*" set copyfrom = .offset(1, 0).specialcells(xlcelltypevisible).entirerow end '~~> remove filters .autofiltermode = false end '~~> destination file set ws2 = wb1.worksheets("output") ws2 if application.worksheetfunction.counta(.cells) <> 0 lrow = .cells.find(what:="*", _ after:=.range("h1"), _ lookat:=xlpart, _ lookin:=xlformulas, _ searchorder:=xlbyrows, _ searchdirection:=xlprevious, _ matchcase:=false).row + 1 else lrow = 1 end if copyfrom.copy .rows(lrow) end end sub
sub loopthroughfiles() dim path string dim filename string dim wb workbook path = "" 'your folder path here filename = dir(path & "*.xls") while (filename <> "") set wb = workbooks.open(path & filename) 'your code goes here wb.close filename = dir wend end sub
Comments
Post a Comment