excel - VBA code for deleting columns that contain a specific string with the function Cells -


i writing code rid of columns contain specific string in cell. code follows:

dim integer dim j integer dim state string dim num variant num = inputbox("enter state", "what stx it?", "enter x here") 'the & combine them state = "st" & num activesheet.usedrange 'uses columns in use due previous line  j = 2 .columns.count     if .cells(2, j).formula = state     'do nothing     else     range(.cells(1, j), .cells(.rows.count, j)).delete shift:=xltoleft     end if  next j end end sub 

i start @ j=2 because not want erase first column. here snippet of data trying modify. however, doesn't erase columns contain specific cell. puzzles me if replace

 range(.cells(1, j), .cells(.rows.count, j)).delete shift:=xltoleft 

with

range(.cells(1, j), .cells(.rows.count, j)).interior.colorindex = 6 

it correctly highlights cells want delete.

when deleting rows loop should run backwards, when delete row, j increases 1 , means skip row.

replace

for j = 2 .columns.count 

for

for j = .columns.count 2 step -1 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -