Vbasic4now
Vbasic4now

Reputation: 589

Looping through a range to copy cells in corresponding columns when a number is found

I'm trying to create a user interface for making timing charts.

I have a list of processes and how long each takes.

The user will enter numbers starting at 1 in the cells to the right

order of operations chart

I'm trying to do the following:
For any row with a number copy the info from column B and F then paste it into row 39 + whatever number it finds.
This should create a list that is in the correct order in a chart.

output chart

I attempted to put together a program that loops through every row of every column in that number entry area.

Sub TimingChart()

Dim rng As Range
Dim cel As Range
Dim col As Range
'x is the variable for row adjustment to determine what cell to paste into
Dim x As Long
'a is the variable for column adjustments
Dim a As Long
Set rng = ThisWorkbook.Worksheets("Sheet1").Range("M4:AG33")

a = -11

'loop columns
For Each col In rng.Columns

'loop rows of that column
    For Each cel In col.Cells
'skip cells with nothing in them
        If cel.Value = "" Then

'any cell with a number copy the data in row B and row F and
'paste in the chart below the entry field starting in row 40
'the data should copy into row 39 + x putting the operations in order
        Else

            x = cel.Value
            cel.Offset(a, 0).Copy cel.Offset(a, 39 + x)
            cel.Offset(a + 4, 0).Copy cel.Offset(a + 4, 39 + x)
            cel.Value.Copy cel.Offset(a - 1, 39 + x)

        End If
    Next
    a = a - 1
Next col

End Sub

Edited to reflect removal of "range("

Upvotes: 0

Views: 71

Answers (1)

Scott Craner
Scott Craner

Reputation: 152450

to get the numbers put this in A39 and copy down:

=IFERROR(SMALL($M$4:$AG$33,ROW(A1)),"")

To get the Actions, put this in B29 and copy down:

=IF(A39<>"",INDEX(B:B,AGGREGATE(15,7,ROW($M$4:$AG$33)/($M$4:$AG$33=A39),COUNTIF($A$39:A39,A39))),"")

To get the time, put this in F39 and copy down:

=IF(A39<>"",INDEX(F:F,MATCH(B39,$B:$B,0)),"")

So with data like:

enter image description here

using those formula we get:

enter image description here

If you really want vba then forget using offset and just refer to the column:

Sub TimingChart()
    With ThisWorkbook.Worksheets("Sheet1")

        Dim rng As Range
        Set rng = .Range("M4:AG33")

        'loop columns
        Dim col As Range
        For Each col In rng.Columns

        'loop rows of that column
            Dim cel As Range
            For Each cel In col.Cells
        'skip cells with nothing in them
                If cel.Value = "" Then

        'any cell with a number copy the data in row B and row F and
        'paste in the chart below the entry field starting in row 40
        'the data should copy into row 39 + x putting the operations in order
                Else
                    'x is the variable for row adjustment to determine what cell to paste into
                    Dim x As Long
                    x = cel.Value
                    .Cells(cel.Row, 2).Copy .Cells(39 + x, 2)
                    .Cells(cel.Row, 6).Copy .Cells(39 + x, 6)
                    cel.Copy .Cells(39 + x, 1)

                End If
            Next cel
        Next col
    End With
End Sub

Upvotes: 2

Related Questions