How to use a Macro in one workbook to read and analyze data in multiple others- Excel VBA -
i have looked @ bunch of articles try figure out, nothing seems working. have built macro in excel workbook practice collecting data , making tables want, , want apply many other workbooks in different folder, not original 1 using practice. kept in same location wrote lines thought apply other locations. oddly, code have make changes new file, of changes reading data in workbook have been practicing with. example, add sheets new file, fills each workbook same information instead of information book changing.
here have:
sub loopfiles() dim myfilename, filenamenew, mypath string dim mybook workbook mypath = "c:\excelt2\" myfilename = dir(mypath & "*.csv") until myfilename = "" set wb1 = workbooks.open(mypath & myfilename) set mybook = activeworkbook activeworkbook.sheets.add after:=worksheets(worksheets.count) activeworkbook.sheets.add after:=worksheets(worksheets.count) activeworkbook.sheets.add after:=worksheets(worksheets.count) wb1 wb1.worksheets(1).select . . .
after reads data first sheet line line, isn't reading first sheet of workbook opened. don't know going wrong. here sample line 1 place read in data:
ra1 = cells(i, 7) . . . worksheets("sheet2").cells(prevval, 3) = ra1
this pulls data wrong workbook puts in right sheet in newly opened workbook. please help!
***update
this line gives me application or object defined error now..
elseif wb1.worksheets(1).cells(i, 3) <> wb1.worksheets(1).cells((i - 1), 3)
see changes below made. these changes made explicitly qualify objects working with. otherwise, there default behaviors in way vba reads lines throw code in different direction (like referring unintended workbook or worksheet.
do until myfilename = "" set wb1 = workbooks.open(mypath & myfilename) 'set mybook = activeworkbook --> wb1 set newly opened file wb1 '--> work directly wb1 .sheets.add after:= .worksheets(.worksheets.count) '--> add "." in front of object qualify parent (wb1 in case) '..... ra1 = .workhseets(1).cells(i,7) .worksheets(2).cells(prevval, 3) = ra1 '.... end
Comments
Post a Comment