vba - Importing table from pdf to excel - Need to uncomment and comment a piece of code for it to run -
i have figured out , works great, except run following errors anytime open workbook , have go through process of uncommenting , commenting section of code.
below code:
private sub activateexcel() appactivate "microsoft excel" 'import_pdf receives table pdf file. sheets("importpdf").select 'clear previous data (at moment causes error way) thisworkbook.sheets("importpdf").cells.clearcontents range("a1").select activesheet.paste 'important note__________________________________________________________________ ' 'the section requires tinkering: next code below separate pdf 'table excel columns, needs done first time, , 'just pasting works! doing 2nd time makes error. 2nd run need 'sort a-z new importpdf sheet (put 3.5 after 3 instead of @ end! application.cutcopymode = false ' selection.texttocolumns destination:=range("a2"), datatype:=xldelimited, _ ' textqualifier:=xldoublequote, consecutivedelimiter:=true, tab:=true, _ ' semicolon:=false, comma:=false, space:=true, other:=false, fieldinfo _ ' :=array(array(1, 1), array(2, 1), array(3, 1), array(4, 1), array(5, 1), array(6, 1), _ ' array(7, 1), array(8, 1), array(9, 1), array(10, 1), array(11, 1), array(12, 1), array(13, 1 _ ' ), array(14, 1)), trailingminusnumbers:=true activeworkbook.worksheets("importpdf").sort.sortfields.clear activeworkbook.worksheets("importpdf").sort.sortfields.add key:=range("a3"), _ sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal activeworkbook.worksheets("importpdf").sort .setrange range("a3:n26") .header = xlno .matchcase = false .orientation = xltoptobottom .sortmethod = xlpinyin .apply end 'copy column import_pdf "dev" volute sheet active on ctrl-b ' range("b3:b26").select selection.copy 'activewindow.scrollworkbooktabs position:=xlfirst 'here select target active sheet when ctrl-b started macro: 'whichever sheet open during ctrl-b pdf "dev" col now... 'msgbox activesheet.name & " activating sheet now..." sheets(activesheetimport).select 'activewindow.smallscroll down:=24 'need time delay right here! 'msgbox "currently active = " & activesheet.name & " active on ctrl-b = " & activesheetimport 'and here can paste active cell when ctrl-b started macro: 'the range active cell! that's dev col pastes next line: activesheet.paste range("a1").select 'range("a1").activate 'sendkeys ("^v") 'shell "pskill " & startadobe 'call shell("taskkill /f /pid " & cstr(vpid), vbhide) end sub so when first run macro import data "importpdf" worksheet data looks ths:[error 1][1]
so solve this: section of code written:
application.cutcopymode = false selection.texttocolumns destination:=range("a2"), datatype:=xldelimited, _ textqualifier:=xldoublequote, consecutivedelimiter:=true, tab:=true, _ semicolon:=false, comma:=false, space:=true, other:=false, fieldinfo _ :=array(array(1, 1), array(2, 1), array(3, 1), array(4, 1), array(5, 1), array(6, 1), _ array(7, 1), array(8, 1), array(9, 1), array(10, 1), array(11, 1), array(12, 1), array(13, 1 _ ), array(14, 1)), trailingminusnumbers:=true so uncomment line of code , re run macro. have run macro twice. second time run macro error: error 2
once error, if comment code again, , run macro. data transfer perfect , works charm. if close workbook, have redo entire process. know why is? or how can fix issue?
once you've run texttocolumns once, excel "remembers" settings , automatically applies subsequent paste operation.
one way around might check selection.columns.count after pasting, run text columns if count 1
application.cutcopymode = false selection if .columns.count = 1 'edited .texttocolumns destination:= .cells(1).offset(0,1), datatype:=xldelimited, _ textqualifier:=xldoublequote, consecutivedelimiter:=true, tab:=true, _ semicolon:=false, comma:=false, space:=true, other:=false, _ fieldinfo :=array(array(1, 1), array(2, 1), array(3, 1), array(4, 1), _ array(5, 1), array(6, 1), array(7, 1), array(8, 1), array(9, 1), array(10, 1), _ array(11, 1), array(12, 1), array(13, 1), array(14, 1)), trailingminusnumbers:=true end if end
Comments
Post a Comment