简体   繁体   English

VBA迭代细胞

[英]VBA Iterate through cells

I'm looking for a way to iterate through the cells in the worksheet to paste in values. 我正在寻找一种方法来迭代工作表中的单元格以粘贴值。

The code posted below will not recognize the cell locations. 下面发布的代码无法识别单元格位置。 Is there a way to loop the cell location to progressively paste the information into the desired spaces? 有没有办法循环单元格位置以逐步将信息粘贴到所需的空间? I want the code to paste the value into A1:C1 first and then move over 4 spaces and post to E1:G1 and so on. 我希望代码将值粘贴到A1:C1中,然后移动4个空格并发布到E1:G1,依此类推。

Unsure how to iterate the letters to move the cell over. 不确定如何迭代字母以移动单元格。

Sub test()

Dim x As Integer
Dim y As Integer
Dim InputSheet As Worksheet
Dim myBook As Workbook

Set myBook = Excel.ActiveWorkbook
Set InputSheet = myBook.Sheets("InputSheet")

For y = 0 To 5
    x = 4
    InputSheet.Range("A1 + 4*y : C1 + 4*y").Value = x
Next y

End Sub

I'd try using "Cells" to do this. 我尝试使用“Cells”来做到这一点。

Sub test()

Dim x As Integer
Dim y As Integer
Dim InputSheet As Worksheet
Dim myBook As Workbook

Set myBook = Excel.ActiveWorkbook
Set InputSheet = myBook.Sheets("InputSheet")

For y = 0 To 5
    x = 4
    InputSheet.Range(InputSheet.Cells(1, 4 * y + 1), InputSheet.Cells(1, 4 * y + 3)).Value = x
Next y

End Sub

You could use the Offset function. 您可以使用偏移功能。

For i = 0 to 5
    x = 4
    InputSheet.Range("A1:C1").Offset(0, i * 4) = x
Next i

You are telling the workbook to literally look for range "A1 + 4*y : C1 + 4*y" which obviously isn't a valid address. 你告诉工作簿从字面上寻找范围“A1 + 4 * y:C1 + 4 * y”,这显然不是一个有效的地址。 You need to evaluate the numerical expression outside the string, convert back to a string (either explicitly using Cstr or you can put the expression in parentheses and let the compiler do it for you as VBA is dynamically typed which is an important concept you might want to look up. Basically it's able to figure out from the context that it's dealing with a string type variable) and finally tack it back onto your address for this to work. 您需要评估字符串外部的数字表达式,转换回字符串(显式地使用Cstr或者您可以将表达式放在括号中并让编译器为您执行此操作,因为VBA是动态类型的,这是您可能需要的重要概念查找。基本上它能够从上下文中找出它正在处理字符串类型变量)并最终将其恢复到您的地址以使其工作。

Since you seem to be new to vba/coding I would advise you to figure out how to use breakpoints and watches to see how your machine actually evaluates your variables. 由于您似乎是vba /编码的新手,我建议您弄清楚如何使用断点和监视来查看您的机器如何实际评估您的变量。

InputSheet.Range("A" & (1+4*y) & ":C" & (1+4*y)).Value = x

I did the following code because I was having some runtime errors as well. 我做了以下代码,因为我也遇到了一些运行时错误。 Hope it helps. 希望能帮助到你。

Dim wb As Workbook

Dim ws As Object

Set wb = Workbooks("Libro2")

Set ws = wb.Sheets("Hoja1")

For i = 1 To 10000

        Dim strrangoa As String
        Dim strrangob As String
            'Creating a string variable replaces selecting the cell
            strrangoa = "A" & i
            strrangob = "B" & i
            'If the active cell does not have data, it exits the loop
            If ws.Range(strrangoa).Value = "" Then
                GoTo Salir
            Else
            'The data from cells in Column A are passed to Column B
                ws.Range(strrangob).Value = ws.Range(strrangoa).Value
            End If

Next

Salir:

End Sub

Ok, every other answer here has failed to use the .Resize() function to select a range of cells which is the recommended way. 好的,这里的每个其他答案都没有使用.Resize()函数来选择推荐方式的单元格范围。

Option Explicit

Sub Test()

    Dim x As Integer
    Dim y As Integer
    Dim InputSheet As Worksheet
    Dim myBook As Workbook

    Set myBook = Excel.ActiveWorkbook
    Set InputSheet = myBook.Sheets("InputSheet")

    For y = 0 To 5
        x = 4
        ' Start for "A1", move down 4*y rows, and select
        ' one row and 3 columns to set the value
        InputSheet.Range("A1").Offset(4*y, 0).Resize(1, 3).Value = x

        ' Similar to using the `.Cells()` method
        ' InputSheet.Range("A1").Cells(4*y+1, 1).Resize(1, 3).Value = x
    Next y       

End Sub

PS. PS。 You can also do things like 你也可以这样做

InputSheet.Range("A2").Resize(1,3).Value = Array(1,2,3)

To set cells A2,B2,C2 to 1,2,3 将单元格A2,B2,C2设置为1,2,3

or 要么

InputSheet.Range("A2").Resize(3,1).Value = _ 
          WorksheetFunction.Transpose(Array(1,2,3))

To set cells A2,A3,A4 to 1,2,3 将单元格A2,A3,A4设置为1,2,3


Read here from Microsoft on how to use .Resize() , .Offset() and .Cells() . 阅读在这里从微软如何使用.Resize() .Offset().Cells()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM