Search code examples
excelvbaloopsrow

Cut columns whenever threshold is reached


I have about 24000 rows in Excel that I want to split to create graphs using VBA. A small part of my data looks like this:

enter image description here

Column A contains the time (4 samples per second).
Column B contains values that tell me whenever a new cycle should start.
Column C contains the most valuable information; temperature, which I finally want to include in my graphs.

The number of rows is different for every file. Now, what I want is code that detects whenever the value for column B is high (+-27000). Everytime the "high peak" is over (purple 500 value in column B), column C should be cut and pasted in the following column, until the rows are empty. I only need the data of column C for my graphs, so the other information does not have to be cut.

The data where the B-value is high, should be part of the previous column, like so, where green and orange are two separate columns:

enter image description here


Solution

  • Here the VBA subroutine which resolves your task:

    Option Explicit
    
    Sub WrapRows()
      Dim i As Long, j As Long, k As Long, b As Boolean
      Dim Limit As Double
      Limit = 20000
      j = 4: k = 1
      For i = 1 To [B1].End(xlDown).Row
        If Cells(i, 2) >= Limit Then b = True
        If Cells(i, 2) < Limit And b Then
          Cells(k, 3).Resize(i - k, 1).Copy Cells(1, j)
          j = j + 1: k = i: b = False
        End If
      Next
      If i > k Then Cells(k, 3).Resize(i - k, 1).Copy Cells(1, j)
    End Sub
    

    enter image description here