I want to run a FOR inside an IF statment to concatenate using & in vba excel -


this first time using vba. generating data in grid of cells number of rows , fixed 4 columns. please note rows not fixed, example data may in grid b17 e20 (4x4 grid) or change b17 e25 (4x9 grid).

screenshot

now, not entries in grid contain data, remain blank.

so problem want list of entries in grid in 1 cell on excel separated ";"

i think need use logic statements omit blank cells, not how use them strings , because number of rows change, need put of inside loop. yet , try generate content 1 excel using & operator, content gets overwritten.

please see code below full macro , attached screenshot of data need enter in grey cells make work.

my post referring "images in website gallery" section...

sub combo() ' ' combo colours sizes '      maxcolours = range("f3")     maxsizes = range("h3")     rowoffset = 3     col1 = 1     col2 = 7     col3 = 9     col4 = 10     col5 = 12     col6 = 13     currow = 4  '   generate simple images colours , sizes      = 1 maxcolours         j = 1 maxsizes              cells(currow, col3).value = cells(rowoffset + i, col1).value             cells(currow, col4).value = cells(rowoffset + j, col2).value             cells(currow, col5) = range("f1") & "-" & cells(currow, col3).value & "-" & cells(currow, col4).value              uppercolour = cells(rowoffset + i, col1)             lowercolour = lcase(uppercolour)                  k = 0 3                     cells(currow, col6 + k) = "/" & lowercolour & "c.jpg"                 next k              currow = currow + 1         next j     next      cells(currow, col5).value = range("f1")     cells(currow + 1, col5).value = range("f1") & "-m"      currowtwo = 4  '   generate available images      n = 1 maxcolours         o = 1 4              colour = cells(rowoffset + n, col1)             image = cells(rowoffset + n, col1 + o)             if image <> ""             cells(currowtwo + n + 12, col1 + o).value = "/" & lcase(colour) & lcase(image) & ".jpg"             else             cells(currowtwo + n + 12, col1 + o).value = ""             end if          next o     next n  '   website config '   images in first 3 columns      l = 1 3         cells(currow, col5 + l) = "/" & range("h17")     next l  '   images in website media gallery      currowtwo = 4              cells(currow, col5 + 4).value = cells(currowtwo + maxcolours + 9, col1 + 1) & "; " & cells(currowtwo + maxcolours + 9, col1 + 2) & "; " & cells(currowtwo + maxcolours + 9, col1 + 3) & "; " & cells(currowtwo + maxcolours + 9, col1 + 4) & "; " & cells(currowtwo + maxcolours + 10, col1 + 1) & "; " & cells(currowtwo + maxcolours + 10, col1 + 2) & "; " & cells(currowtwo + maxcolours + 10, col1 + 3) & "; " & cells(currowtwo + maxcolours + 10, col1 + 4) & "; " & cells(currowtwo + maxcolours + 11, col1 + 4) & "; " & cells(currowtwo + maxcolours + 11, col1 + 4) & "; " & cells(currowtwo + maxcolours + 11, col1 + 4) & "; " & cells(currowtwo + maxcolours + 11, col1 + 4)   '   marketplace config '   images in first 3 columns      m = 1 3         cells(currow + 1, col5 + m) = "/" & range("h19")     next m  end sub 

i'm not quite sure if trying achieve but, understood, here small snippet should trick.

'define grid area firstrowofgrid = 17 lastrowofgrid = 29 numberofcols = 4 firstcolletter = "b"  cellvalue = ""                  'this 1 hold cell value concatenatedstring = ""         'this concatenated string  firstcolletter = asc(firstcolletter)    'convert letter number  j = firstcolletter firstcolletter + numberofcols     'loop through columns     = firstrowofgrid lastrowofgrid                 'loop through rows         cellvalue = range(chr(j) & i).value                 'get current cell value         if (cellvalue)                                 'check if current cell has value             'the new value of concatenated string concatenated string followed semi-colon , current             'cell value             concatenatedstring = concatenatedstring & ";" & cellvalue         end if     next next j  'remove first semi-colon concatenatedstring = mid(concatenatedstring, 2, len(concatenatedstring)) 'do string (currently displaying in msgbox) msgbox concatenatedstring 

this code loop through fields b17 e29 , add cell value (if cell not empty) concatenated string can use later.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -