split the statement in a excel file column using a specific keyword -
i have excel file data in multiple columns. need select 1 column file , separate data in each cell of column using specific keyword in cell. keyword same each cell
now want is, want separate each alphabet cell , give them title 1 in image below
the following vba code should trick:
option explicit public sub mainsub() dim arr variant dim srcrow long, destrow long dim wssrc worksheet, wsdest worksheet set wssrc = thisworkbook.worksheets("sheet1") set wsdest = thisworkbook.worksheets.add(before:=wssrc) wsdest.cells(1, 1).value2 = "letter" wsdest.cells(1, 2).value2 = "value" destrow = 2 srcrow = 1 wssrc.cells(wssrc.rows.count, "a").end(xlup).row if instr(1, wssrc.cells(srcrow, 1).value2, "half:", vbtextcompare) arr = split(wssrc.cells(srcrow, 1).value2, "half:")(0) arr = split(arr, "/") destrow = writetodest(arr, "full", destrow, wsdest) arr = split(wssrc.cells(srcrow, 1).value2, "half:")(1) arr = split(arr, "/") destrow = writetodest(arr, "half", destrow, wsdest) else arr = split(wssrc.cells(srcrow, 1).value2, "/") destrow = writetodest(arr, "full", destrow, wsdest) end if next srcrow end sub private function writetodest(arr variant, halforfull string, destrow long, wsdest worksheet) long dim element long element = lbound(arr) ubound(arr) if trim(arr(element)) <> vbnullstring wsdest.cells(destrow, 1).value2 = ucase(trim(arr(element))) wsdest.cells(destrow, 2).value2 = "full" destrow = destrow + 1 end if next element writetodest = destrow end function
just copy of empty module , adjust name of sheet in set wssrc = thisworkbook.worksheets("sheet1")
actual name of sheet data resides.
the above code insert new sheet , process / output data described in post above.
Comments
Post a Comment