vba 具有多个变量的for循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27261331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:23:51  来源:igfitidea点击:

For loop with multiple variables

excelvbaexcel-vba

提问by phillipsK

Here is a excel vba sub procedure example. I have two columns of data, range vand range c- How could I concatenate each cell rows' value with the parallel row call value.

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

Ideally, what I am trying to do would be this

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

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

Pleas let me further explain: cell G2 value is only related to J2, and G3 with J3

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

G2 value = Blue
J2 value = Spaghetti 

I am trying to return "Blue Spaghetti" with one for loop?

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

G2 value = Red
J2 value = Noodles

I am trying to return "Red Noodles" with one for loop?

我想用一个 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

回答by peege

Looking at your original post, I'm going to say I'm confused with all the extra stuff. Look at what goes on here, and comment with questions. I think you are over complicating what you are attempting.

看看你原来的帖子,我会说我对所有额外的东西感到困惑。看看这里发生了什么,并提出问题发表评论。我认为你把你正在尝试的事情复杂化了。

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