vba - Copying a discontinuous range from one sheet to another -


vba rookie here (and first-time poster) pretty basic question. however, haven't found answer anywhere on internet (or in reference books have) i'm pretty stumped.

how can take bunch of spaced-out columns in 1 sheet , stuff them sheet, without gaps?

for example, want copy cells marked x's sheet this:

x . . . x x . . x . . x x . . . x x . . x . . x x . . . x x . . x . . x x . . . x x . . x . . x x . . . x x . . x . . x x . . . x x . . x . . x x . . . x x . . x . . x x . . . x x . . x . . x x . . . x x . . x . . x 

to different sheet this:

x x x x x . . . . .  x x x x x . . . . .  x x x x x . . . . .  x x x x x . . . . .  x x x x x . . . . .  x x x x x . . . . .  x x x x x . . . . .  x x x x x . . . . .  x x x x x . . . . .  x x x x x . . . . .  

design constraints:

  • source range disjointed columns. destination continuous block
    • e.g. source "a3:b440, g3:g440, i3:i440" -> destination "a3:d440"
  • only values. destination has conditional formatting needs preserved
  • destination part of databodyrange of listobject
  • the source range columns arbitrary. they're found header indexing function.
  • the row-count arbitrary, same both source , destination.
  • there 400 rows , 10-15 columns i'm trying copy. loops are... annoying.

this snippets gets job done, bounces things , forth much, , takes way long. feel wrong way it.

for each hdrfield in exportfields      rawdatacol = s_rawdata.headercolumnpositions(hdrfield)      s_rawdata.activate     s_rawdata.range(s_rawdata.cells(3, rawdatacol), s_rawdata.cells(lastrow, rawdatacol)).copy (s_console.range(s_console.cells(3, i), s_console.cells(lastrow, i)))     s_console.activate     s_console.range(s_console.cells(3, i), s_console.cells(lastrow, i)).select     s_console.paste      = + 1  next hdrfield 

this approach works. it's faster, , it's reliable. it's i've been doing, hard-coding source positions isn't going work anymore.

'transfer important columns raw data sheet report line sheet s_console.range("a3:a" & upperlimit).value = s_rawdata.range("a3:a" & upperlimit).value 'timestamp s_console.range("b3:b" & upperlimit).value = s_rawdata.range("i3:i" & upperlimit).value 'h2.ppm s_console.range("c3:c" & upperlimit).value = s_rawdata.range("j3:j" & upperlimit).value 'h2_dg.ppm s_console.range("d3:d" & upperlimit).value = s_rawdata.range("k3:k" & upperlimit).value 'oiltemp or gastemp s_console.range("e3:e" & upperlimit).value = s_rawdata.range("l3:l" & upperlimit).value 'h2_g.ppm s_console.range("f3:f" & upperlimit).value = s_rawdata.range("q3:q" & upperlimit).value 'h2_mt s_console.range("g3:g" & upperlimit).value = s_rawdata.range("r3:r" & upperlimit).value 'h2_oo s_console.range("h3:h" & upperlimit).value = s_rawdata.range("s3:s" & upperlimit).value 'h2_lg s_console.range("i3:i" & upperlimit).value = s_rawdata.range("t3:t" & upperlimit).value 'r1 s_console.range("j3:j" & upperlimit).value = s_rawdata.range("u3:u" & upperlimit).value 'r2 s_console.range("k3:k" & upperlimit).value = s_rawdata.range("ab3:ab" & upperlimit).value 't1 s_console.range("l3:l" & upperlimit).value = s_rawdata.range("ac3:ac" & upperlimit).value 't2 s_console.range("m3:m" & upperlimit).value = s_rawdata.range("ah3:ah" & upperlimit).value 'cycle type 

why can't have hybrid of two? why won't code work?

 s_console.range("a3:m" & lastrow).value = s_rawdata.exportrange 

(i've got custom "exportrange" property written, can select + copy range want... can't set values of range because it's discontinuous)

thanks help! seems fundamental piece of learning vba can't find information about.

-matt

the key thing aware of can copy whole discontinuous range @ once, this:

sheet1.range("a3:b440, g3:g440, i3:i440").copy sheet2.range("a3").pastespecial xlvalues 

note in above sheet1 , sheet2 codenames, you'll use thisworkbook.worksheets("mysheet").

i couldn't sure else you're trying do, wrote code. finds columns copy using find , findnext, searching columns "copy" in row 2:

sub copydiscontiguouscolumns() dim wsfrom excel.worksheet dim wsto excel.worksheet dim rangetocopy excel.range dim headerrange excel.range dim headertext string dim firstfoundheader excel.range dim nextfoundheader excel.range dim lastrow long  set wsfrom = thisworkbook.worksheets(1) set wsto = thisworkbook.worksheets(2) 'headers in row 2 set headerrange = wsfrom.rows(2) 'this text identifies columns copies headertext = "copy" wsfrom     'look first instance of "copy" in header row     set firstfoundheader = headerrange.find(headertext)     'if "copy" found, we're off , running     if not firstfoundheader nothing         lastrow = .cells(.rows.count, firstfoundheader.column).end(xlup).row         set nextfoundheader = firstfoundheader         'start build range columns copy         set rangetocopy = .range(.cells(3, nextfoundheader.column), .cells(.rows.count, nextfoundheader.column))         'and keep doing same thing in loop until start                 set nextfoundheader = headerrange.findnext(nextfoundheader)             if not nextfoundheader nothing                 set rangetocopy = union(rangetocopy, .range(.cells(3, nextfoundheader.column), .cells(.rows.count, nextfoundheader.column)))             end if         loop while not nextfoundheader nothing , nextfoundheader.address <> firstfoundheader.address     end if end rangetocopy.copy sheet2.range("a3").pastespecial xlvalues end sub 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -