VBA Cell Iteration

I am looking for a way to iterate through cells in a worksheet to insert values.

The code below will not recognize cell locations. Is there a way to encode a cell to gradually insert information in the right places? I want the code to first insert the value into A1: C1, and then move to 4 spaces and send to E1: G1 and so on.

Not sure how to iterate over letters to move a cell.

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
+4
source share
5 answers

, "A1 + 4 * y: C1 + 4 * y", , , . , ( Cstr, , VBA , , . , , ) , , , .

, , vba/coding, , , , .

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

"" .

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
+2

.

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

, . , .

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
0

, .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. ,

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

A2, B2, C2 1,2,3

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

A2, A3, A4 1,2,3


Microsoft , .Resize(), .Offset() .Cells().

0

Source: https://habr.com/ru/post/1665277/


All Articles