我想在IF语句中运行一个FOR来连接使用&in vba excel

这是我第一次使用VBA。 我正在由任意数量的行和固定的4列在单元格网格中生成数据。 请注意,行不是固定的,所以例如数据可能在从B17到E20(4×4网格)的网格中,或者也可以变成B17到E25(4×9网格)。

截图

现在,并不是网格中的所有条目都包含数据,有些则保持空白。

所以我的问题是我想列出在Excel中的一个单元格中的网格中的所有条目分隔“;”

我想我将需要使用逻辑语句来省略空白单元格,但是我不知道如何在string中使用它们,也因为行数会改变,所以我需要把它放在一个循环中。 然而,只要我这样做,并尝试使用&运算符生成一个Excel的内容,内容被覆盖。

请参阅下面的代码完整的macros和附加的屏幕截图的数据,你将需要input灰色单元格,使其工作。

我的文章是指“网站库中的图片”部分…

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 for all Colours and Sizes For I = 1 To MaxColours For J = 1 To 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) For K = 0 To 3 Cells(CurRow, Col6 + K) = "/" & LowerColour & "c.jpg" Next K CurRow = CurRow + 1 Next J Next I Cells(CurRow, Col5).Value = Range("f1") Cells(CurRow + 1, Col5).Value = Range("f1") & "-M" CurRowTwo = 4 ' Generate Available Images For N = 1 To MaxColours For O = 1 To 4 Colour = Cells(RowOffset + N, Col1) Image = Cells(RowOffset + N, Col1 + O) If Image <> "" Then 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 For L = 1 To 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 For M = 1 To 3 Cells(CurRow + 1, Col5 + M) = "/" & Range("h19") Next M End Sub 

我不太确定这是不是你想要达到的目标,但是从我的理解来看,这是一个应该做的诀窍。

 'Define your grid area firstRowOfGrid = 17 lastRowOfGrid = 29 numberOfCols = 4 firstColLetter = "B" cellValue = "" 'This one will hold the cell value concatenatedString = "" 'This will be the concatenated string firstColLetter = Asc(firstColLetter) 'Convert the letter to a number For j = firstColLetter To firstColLetter + numberOfCols 'Loop through the columns For i = firstRowOfGrid To lastRowOfGrid 'Loop through the rows cellValue = Range(Chr(j) & i).Value 'Get the current cell value If (cellValue) Then 'Check if current cell has a value 'The new value of the concatenated string is the concatenated string itself followed by a semi-colon and the current 'cell value concatenatedString = concatenatedString & ";" & cellValue End If Next i Next j 'Remove the first semi-colon concatenatedString = Mid(concatenatedString, 2, Len(concatenatedString)) 'Do something with your string (currently displaying in a msgbox) MsgBox concatenatedString 

此代码将遍历字段B17到E29,并将单元格值(如果单元格不为空)添加到稍后可以使用的连接string中。