对于具有多个变量的循环

时间:2014-12-03 00:03:38

标签: excel vba excel-vba

这是一个excel vba子程序示例。我有两列数据,范围v和范围c - 我如何将每个单元格行的值与并行行调用值连接。

理想情况下,我想做的就是这个

For Each c,b In v,bb
 ...
next c,b

请允许我进一步解释:单元格G2值仅与J2相关,而G3与J3相关

G2 value = Blue
J2 value = Spaghetti 

我试图用一个for循环返回“Blue Spaghetti”?

G2 value = Red
J2 value = Noodles

我试图用一个for循环返回“Red Noodles”?

Dim c As Variant
Dim b As Variant
Dim v As Range
Dim bb As Range
Dim brow As Long
Dim vrow as long

Set v = ActiveSheet.Range("G:G")
vrow = v(v.Cells.Count).End(xlUp).Row
Set v = Range(v(2), v(brow))

Set bb = ActiveSheet.Range("J:J")
brow = bb(bb.Cells.Count).End(xlUp).Row
Set bb = Range(bb(2), bb(brow))    

For Each c In v
    c = Mid(c, 1, 4)
    msgbox c
Next c

For each b in bb   
    msgbox b    
next b

1 个答案:

答案 0 :(得分:5)

看看你的原帖,我会说我对所有额外的东西感到困惑。看看这里发生了什么,并回答问题。我认为你已经使你正在尝试的事情变得复杂。

Sub ConcatCols()    
    Dim lastRow As Long
    Dim tempValue As String

    lastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).row

    For iRow = 2 to lastRow
        tempValue = Sheets("Sheet1").Cells(iRow, "G").Text & " " & _
            Sheets("Sheet1").Cells(iRow, "J").Text
        MsgBox tempValue   
    Next iRow    
End Sub