我想在IF语句中运行FOR,以使用&;连接;在vba excel中

我想在IF语句中运行FOR,以使用&;连接;在vba excel中,excel,vba,if-statement,Excel,Vba,If Statement,这是我第一次使用VBA。我以任意行数和固定列数在单元格网格中生成数据。请注意,行不是固定的,因此,例如,数据可能位于从B17到E20(4x4网格)的网格中,或者它也会更改为B17到E25(4x9网格) 现在,并非网格中的所有条目都将包含数据,有些条目将保持空白 所以我的问题是我想在excel的一个单元格中列出网格中的所有条目,用“;”分隔 我想我需要使用逻辑语句来省略空白单元格,但是我不知道如何用字符串来使用它们,也因为行的数量会改变,所以我需要把所有的都放在一个循环中。但是,一旦我这样做并尝

这是我第一次使用VBA。我以任意行数和固定列数在单元格网格中生成数据。请注意,行不是固定的,因此,例如,数据可能位于从B17到E20(4x4网格)的网格中,或者它也会更改为B17到E25(4x9网格)

现在,并非网格中的所有条目都将包含数据,有些条目将保持空白

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

我想我需要使用逻辑语句来省略空白单元格,但是我不知道如何用字符串来使用它们,也因为行的数量会改变,所以我需要把所有的都放在一个循环中。但是,一旦我这样做并尝试使用&操作符将内容生成到一个excel中,内容就会被覆盖

请参阅下面的代码,了解完整的宏和附加的数据屏幕截图,您需要在灰色单元格中输入这些数据才能使其正常工作

我的帖子是指“图片在网站画廊”部分

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
子组合()
'
'组合颜色大小
'
最大颜色=范围(“f3”)
最大尺寸=范围(“h3”)
行偏移=3
Col1=1
Col2=7
Col3=9
Col4=10
Col5=12
Col6=13
电流=4
'为所有颜色和尺寸生成简单图像
对于I=1到maxcolors
对于J=1至最大尺寸
单元格(CurRow,Col3).值=单元格(RowOffset+I,Col1).值
单元格(CurRow,Col4).值=单元格(RowOffset+J,Col2).值
单元格(CurRow,Col5)=范围(“f1”)和“-”单元格(CurRow,Col3)。值和“-”单元格(CurRow,Col4)。值
UPPERCOLOR=单元格(行偏移量+I,列1)
LowerColor=LCase(上色)
对于K=0到3
单元格(CurRow,Col6+K)=“/”&较低颜色和“c.jpg”
下一个K
电流=电流+1
下一个J
接下来我
单元格(CurRow,Col5)。值=范围(“f1”)
单元格(CurRow+1,Col5)。值=范围(“f1”)和“-M”
CurRowTwo=4
'生成可用图像
对于N=1至最大颜色
对于O=1到4
颜色=单元格(行偏移+N,Col1)
图像=单元格(行偏移量+N,列1+O)
如果图像为“”,则
单元格(CurRowTwo+N+12,Col1+O).Value=“/”&LCase(彩色)和LCase(图像)和.jpg
其他的
单元格(CurRowTwo+N+12,Col1+O)。Value=“”
如果结束
下一个O
下一个
'网站配置
'前3列中的图像
对于L=1到3
单元格(CurRow,Col5+L)=“/”和范围(“h17”)
下一个L
'网站媒体库中的图像
CurRowTwo=4
单元格(CurRow,Col5+4)。值=单元格(CurRowTwo+MaxColor+9,Col1+1)&“&Cells(CurRowTwo+MaxColor+9,Col1+2)&”&Cells(CurRowTwo+MaxColor+9,Col1+3)&“&Cells(CurRowTwo+MaxColor+9,Col1+4)&”&Cells(CurRowTwo+MaxColor+10,Col1+1)&“&Cells(CurRowTwo+MaxColor+10,Col1+2)&”&单元格(currowtow2+maxcolors+10,Col1+3)&“&单元格(currowtow2+maxcolors+10,Col1+4)&”&单元格(currowtow2+maxcolors+11,Col1+4)&“&单元格(currowtow2+maxcolors+11,Col1+4)&”&单元格(currowtow2+maxcolors+11,Col1+4)&“&单元格(currowtow2+maxcolors+11,Col1+4)
'市场配置
'前3列中的图像
对于M=1到3
单元格(CurRow+1,Col5+M)=“/”和范围(“h19”)
下一个M
端接头

我不太确定这是否是您试图实现的目标,但据我所知,这里有一个小片段可以实现这一目的

'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之间循环,并将单元格值(如果单元格不为空)添加到可供以后使用的串联字符串中。

是否有原因使
串联
函数无法执行此操作?您可以通过多种方式识别空格
cell.value=vbNullString
cell.value=”“
len(cell.value)=0
,等等。