循环将一列中单元格的每个值循环为空,对于每个值,请使用不同列中的所有值直至为空

时间:2019-06-11 08:45:25

标签: excel vba loops

我要完成一项任务,目前已经完成了一半。 我需要一个循环来迭代每个值,比如说“ A”(A1,A2,A3等)列,然后从第一个“ B”(B1,B2,B3等)列中获取所有单元格值“ A”上的第二个单元格,然后移动以从“ C”列中获取所有值,以此类推。 我设法完成将“ A”上的每个单元格循环到空白,并在“ A”列中获取从“ B”到空白的所有值,但是当“ A”上的单元格更改为第二(A2)。

Dim BlankFound As Boolean
Dim BlankFound_1 As Boolean
Dim x As Long
Dim y As Long

Do While BlankFound = False
    x = x + 1
    If Worksheets("shee1").Cells(x, "A").Value = "" Then
        BlankFound = True
    ElseIf Worksheets("shee1").Cells(x, "A").Value = "title" Then

    Else
        value_1 = Worksheets("shee1").Cells(x, "A").Value
        BlankFound_1 = False
        y = 0
        Do While BlankFound_1 = False
            y = y + 1

            If Worksheets("sheet2").Cells(y, "A").Value = "" Then
                BlankFound_1 = True
            ElseIf Worksheets("sheet2").Cells(y, "A").Value = "values" Then

            Else
                value_2 = Worksheets("sheet2").Cells(y, "A").Value
            End If
       Loop
    End If

    'End If 'this End If doesn't have an If
Loop

1 个答案:

答案 0 :(得分:0)

如果我了解您的意图,那么我认为这将会做到:

Option Explicit
Sub consolidate()

    Dim x As Long
    Dim i As Long
    Dim Col As Long

    With ThisWorkbook.Sheets("MySheet") 'Change MySheet for your working sheet name
        Col = .Cells(1, .Columns.Count).End(xlToLeft).Column 'calculate the last column
        'instead looping for every cell, just use the Sum function for the range in every column
        For i = 2 To Col
            .Cells(i, 1) = Application.Sum(.Columns(i)) 'the i will increase both the column to calculate and the row for the output
        Next i
    End With

End Sub