EXCEL VBA - Delete if a column exists -


i have requirement wherein, need delete particular column if exists.

i trying locate particular column through column header.

this code,

if scolumnname = (worksheetfunction.match("rsd", sheets("rs_report").rows(1), 0)) , scolumnname = true  ddc= worksheetfunction.match("rsd", sheets("rs_report").rows(1), 0)  dfc= getcolumnletter(ddc)  range(dfc& 1 & ":" & dfc& lastrow).select  selection.delete shift:=xlup 

the getcolumnletter , lastrow user defined functions , return correct values. not sure how check if column exists or not. kindly me this. share thoughts.

there 3 ways this.

1) loop looks extents of header row specific string.
pro: easy con: string has exact

something like

dim string yourstring dim lcolumn long lcolumn = ws.usedrange.columns.count yourstring = whatever x = 1 lcolumn   if range(cells(1, 1), cells(1, x)).value = yourstring     columns(x).entirecolumn.delete   end if next 

2) use range.find method can learn here https://msdn.microsoft.com/en-us/library/office/ff839746.aspx

here short rough example can use reference:

sub header_format() dim rlastcell range set rlastcell = updatesheet.cells.find(what:="*", after:=updatesheet.cells(1, 1), lookin:=xlformulas, lookat:= _ xlpart, searchorder:=xlbycolumns, searchdirection:=xlprevious, matchcase:=false)     updatesheet.range(cells(4, 1), cells(4, rlastcell.column))         .copy mastersheet.cells(1, 1)         .copy removalsheet.cells(1, 1)     end end sub 

3) there using match method, spoke on already.

https://msdn.microsoft.com/en-us/library/office/ff835873.aspx


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -