excel - How to avoid use of .Select and .Activate in creating a table? -




i’ve been told use of .select , .workbooks.activate not way write vba. code below tends work , there doesn’t seem real issues. activeworkbook not problem because of workbooks("fua.xlsm").activate. question then, alternative/approach? i’m sorry if waste of time or it’s stupid question, have heard using these methods not way in long run. worried not work or create problems in future. should noted without workbooks("fua.xlsm").activate code tends create errors gets confused workbook should select. in short, question is, how able avoid using select , .activate mitigate potential errors in future?
code follows..

  dim wb1 excel.workbook     dim wb2 excel.workbook     set wb2 = workbooks.open("c:\users\ha.csv")     set wb1 = workbooks("fua")    dim sht1 worksheet    dim sht2 worksheet   dim copyrange range   set sht1 = wb1.sheets("sheet1")    set sht2 = wb2.sheets("ha") wb1.sheets("sheet1")    range("aa3").select     if application.worksheetfunction.counta(.cells) <> 0         lastrow = .cells.find(what:="*", _                       after:=.range("aa3"), _                       lookat:=xlpart, _                       lookin:=xlformulas, _                       searchorder:=xlbyrows, _                       searchdirection:=xlprevious, _                       matchcase:=false).row     else         lastrow = 1     end if end workbooks("fua.xlsm").activate    range("aa3").select     sht1.listobjects.add(xlsrcrange, , xlyes).name = _         "table1"     range("table1[#all]").select     sht1.listobjects("table1").range.autofilter field:=9, criteria1:= _         ">=-1000000000000", operator:=xland, criteria2:="<=1000000000000000"          application.displayalerts = false     selection.specialcells(xlcelltypevisible).copy     application.displayalerts = true     set wb2 = workbooks.open("c:\users\ha.csv")           application.displayalerts = false wb2.sheets("ha").paste  wb2.saveas filename:= _         "c:\users\ha.csv", fileformat:= _         xlcsv, createbackup:=false         workbooks("ha.csv").close end sub  

probably down voted saying if ain't broke don't fix it. code works fine already, no real reason change unless doesn't work or errors popping up. sounds you've tested already.

your code optimised , faster though here alternative sake of avoiding select. if error 1004 select methods similar last line sht2.range("a:i").copy columns(last_col + 1).pastespecial in code below continue anyway.

sub test()      dim wb1 excel.workbook     set wb1 = workbooks("xxx.xlsm")                ' here, use wb1 refer fua.xlsm      dim wb2 excel.workbook                      ' ditto wb2     set wb2 = workbooks.open("c:\users\ha.csv")      dim sht1 worksheet                          ' ditto sht1     set sht1 = wb1.sheets("sheet1")      dim sht2 worksheet      set sht2 = wb2.sheets("ha")      if application.worksheetfunction.counta(sht1.cells) <> 0          lastrow = sht1.cells.find( _                         what:="*", _                         after:=sht1.range("a1"), _                         lookat:=xlpart, _                         lookin:=xlformulas, _                         searchorder:=xlbyrows, _                         searchdirection:=xlprevious, _                         matchcase:=false).row     else         lastrow = 1     end if       sht1.listobjects.add(xlsrcrange, sht1.range("a:i"), xlyes).name = "table1"          sht1.listobjects("table1").range.autofilter _                 field:=9, _                 criteria1:=">=-1000000000000", _                 operator:=xland, _                 criteria2:="<=1000000000000000"      application.displayalerts = false          ' not sure if needed          sht1.range("a:i").specialcells(xlcelltypevisible).copy      on error resume next    sht2.range("a:i").copy columns(last_col + 1).pastespecial     on error resume next     application.displayalerts = true           ' not sure if needed      wb2.save                                   ' c:\users\ha.csv     wb2.close  end sub 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -