1
votes

Is there a way to find the last 2nd row of alternating column in a worksheet in this case the last 2nd row of Time1, Time2 etc and after that copy and paste it on another worksheet as shown in the picture? Often time, when each column has the same number of rows, using LastRow = .Range("E" & .Rows.Count).End(xlUp).Row will do the job but this time round, every alternating columns have different number of rows so i cant used that LastRow definition. Below is my attempted code where i am stuck on how to manipulate to get the last 2nd row for every alternating column.

Sub main()

Dim i As Long

Dim j As Long

Dim lastrowv1 As Long

Dim LastColumn As Long


i = 1 'i for column
j = 2  'j for row



Set sht = ActiveWorkbook.Sheets("current")
LastColumn = sht.Cells(1, sht.Columns.count).End(xlToLeft).Column

For i = 1 To LastColumn Step 2
    With sht

'How do i find the last 2nd row for every alternate column(in this case time1,time2,time3...timex) in VBA?

'
'
'

End Sub

enter image description here

When tried to store the values on another worksheet eg:

     For m = 2 To i - 2 Step 2
                Sheets("Breakdown").Cells(m / 2 + 1, 3) = timecoll.Add.Cells(.Rows.count, i).End(xlUp).Offset(-1, 0).Value

'^argument not optional error above
              Next m

If there are empty data and i want to give that columns with empty data a name called "short", how do i go about doing it?

enter image description here

3

3 Answers

5
votes

Using the same kind of method you used to get the last column we can get the last row of each column from inside the loop and offset by -1 row.

Then store the values in a collection (or do whatever you want with them).

    Dim i As Long
    Dim lastcolumn As Long

    Dim sht As Worksheet

    Set sht = ThisWorkbook.Sheets("current")

    Dim timecoll As Collection
    Set timecoll = New Collection
    With sht
        lastcolumn = .Cells(1, .Columns.Count).End(xlToLeft).Column

        For i = 1 To lastcolumn Step 2
            timecoll.Add .Cells(.Rows.Count, i).End(xlUp).Offset(-1, 0).Value
        Next i
    End With

For your edit, I am unsure what that loop is so I'm going to dump to column A in Sheets("Breakdown").

dim k as variant

set sht = thisworkbook.sheets("Breakdown")
i = 1
with sht
    for each k in timecoll
        .cells(i, 1).value = k
        i = i + 1
    next k
end with

Okay here's the whole thing after the edit:

Option Explicit

Sub test()
    Dim i As Long
    Dim lastcolumn As Long

    Dim sht As Worksheet

    Set sht = ThisWorkbook.Sheets("current")

    Dim timecoll As Collection
    Set timecoll = New Collection
    With sht
        lastcolumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
        On Error GoTo short
        For i = 1 To lastcolumn Step 2
            timecoll.Add .Cells(.Rows.Count, i).End(xlUp).Offset(-1, 0).Value
        Next i
        On Error GoTo 0
    End With

    Dim k As Variant

    Set sht = ThisWorkbook.Sheets("Breakdown")
    i = 1
    With sht
        For Each k In timecoll
            .Cells(i, 1).Value = k
            i = i + 1
        Next k
    End With
    Exit Sub
short:
    timecoll.Add "Short"
    Resume Next
End Sub

2
votes

If you mean you want the result something like this : enter image description here

I do the code like this :

Sub test()
tRow = ActiveSheet.Columns(1).Rows.Count
Set DestSh = Sheets("test") 'change as needed
DestCol = "J" 'change as needed
Set oStart = ActiveSheet.Range("A1")
oCol = Left(oStart.Columns.Address(0, 0), 1)

Do
DestSh.Range(DestCol & tRow).End(xlUp).Offset(1, 0).Value _
= Range(oCol & tRow).End(xlUp).Offset(-1, 0)
Set oStart = oStart.Offset(0, 2)
oCol = Left(oStart.Columns.Address(0, 0), 1)
Loop Until oStart.Value = ""

End Sub

Edit: if there is no data :

Sub test5()
tRow = ActiveSheet.Columns(1).Rows.Count
Set DestSh = Sheets("test") 'change as needed
DestCol = "P" 'change as needed
Set oStart = Sheets("test").Range("A1") 'change as needed
oCol = Left(oStart.Columns.Address(0, 0), 1)

Do
If Range(oCol & 2).Value = "" Then
DestSh.Range(DestCol & tRow).End(xlUp).Offset(1, 0).Value = "short"
Else
DestSh.Range(DestCol & tRow).End(xlUp).Offset(1, 0).Value _
= Range(oCol & tRow).End(xlUp).Offset(-1, 0)
End If
Set oStart = oStart.Offset(0, 2)
oCol = Left(oStart.Columns.Address(0, 0), 1)
Loop Until oStart.Value = ""

End Sub

enter image description here

The condition which I don't "calculate" is
IF it only has one row data THEN... (what ?) :)

1
votes

If all the data has headings in the first row find the last column number and then loop though all those columns as below.

Sub test()
Set sht = ActiveWorkbook.Sheets("current")
Dim myCells As Range
Lcol = sht.Cells(1, sht.Columns.Count).End(xlToLeft).Column
i = 1
Do While i <= Lcol
    PrevofLRow = sht.Cells(sht.Rows.Count, i).End(xlUp).Row - 1
    'union of those cells can be made as below
    If myCells Is Nothing Then
      Set myCells = Cells(PrevofLRow, i)
      Else
      Set myCells = Union(myCells, Cells(PrevofLRow, i))
    End If
i = i + 2
Loop

End Sub