VBA Transpose Rows to Columns and Fill in Black Cells for Days Without Data

lstod

New Member
Joined
Mar 4, 2017
Messages
1
I would really appreciate assistance with reviewing my code. I am fairly new to VBA and can't seem to figure out the correct procedure to clean up/reformat the data on a spreadsheet.

I have a spreadsheet with sheets for each year from last year to 10 years in the future. Within each sheet the data is broken down by day horizontally; however the dates are not hard coded they merely a number between 1-31.

I need to get the Total Count by Day rows for each month beginning January of the current year +1 year out transposed vertically onto a different sheet "Compiled" that lists the date in column "A" and the Total Count in Column "B" and fill in zeros for any dates with no data.

Problems I've noticed with my code
1. Need a way to determine what the current year is in order to select the correct sheet and start at the correct date on the "Compiled" sheet.
2. Number of days varies month to month and year over year (leap year), which causes the data when pasted vertically to be offset into the wrong date on the "Compiled" sheet.
3. December typically has no data during the last few days of the month, which causes another offset issue when the loop starts to pull next years data.

Excel 2010
BCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAIAJAK
161November 2018ThuFriSatSunMonTueWedThuFriSatSunMonTueWedThuFriSatSunMonTueWedThuFriSatSunMonTueWedThuFri
162123456789101112131415161718192021222324252627282930
163
1641100440095001000010000920071001900400100
165
16645270139532854500450038251305
167
168245018395617501750175060030
169
170138700682513125
171TOTAL COUNT BY DAY11004400950010000100009200714521701795340945504683478130551750175060030138700682513125
172100706
173
174December 2018SatSunMonTueWedThuFriSatSunMonTueWedThuFriSatSunMonTueWedThuFriSatSunMonTueWedThuFriSatSunMon
17512345678910111213141516171819202122232425262728293031
176
1771750013300113751925350
178
17930602101530291030003000261060060
180TOTAL COUNT BY DAY175001330011375195541021015302910300030002610600600
18158460

<tbody>
</tbody>
2018




Code:
Sub CleanRUR()
Dim x As Long
    Dim i As Long
    Dim z As Range
    Dim y As Long
    Dim LastRow As Long
    Dim LRow As Long
    Dim StartDate As Date
    Dim EndDate As Date
    Dim NoDays As Integer
    
    'Add sheet and fill date down for two years
    Sheets.Add.Name = "Compiled"
    Sheets("Compiled").Range("a2") = (#1/1/2017#)
    Range("A2:A732").DataSeries Rowcol:=xlColumns, Type:=xlChronological, Date:=xlDay, Step:=1, Trend:=False
    Cells(1, 1).Value = [{"Date"}]
    Cells(1, 2) = [{"Product"}]
    Columns("A:B").EntireColumn.AutoFit
    
    'Loop through first two sheets in workbook to consolidate monthly totals
    For y = 3 To 4
        If y = 3 Then
            Sheets(y).Activate
            Cells(1, 1).Select
            LastRow = Cells(Rows.Count, 2).End(xlUp).Row
            For x = LastRow To 1 Step -1
                If Cells(x, 2).Value <> "TOTAL COUNT BY DAY" Then
                Rows(x).Delete
                End If
            Next x
    
            Columns("A:E").Delete
            Cells(1, 1).Select
    
            LRow = Cells(Rows.Count, 1).End(xlUp).Row
        
            'Copy rows and transpose vertically onto Compile ws
            For i = 1 To LRow Step 1
                Rows(i).Copy
                Sheets("Compiled").Range("B" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues, Transpose:=True
            Next i
            
            'Fills in zero for blank cells
            With Sheets("Compiled")
                .Range("B2:B367").SpecialCells(xlCellTypeBlanks).Value = "0"
            End With
        Else
            Sheets(y).Activate
            Cells(1, 1).Select
            LastRow = Cells(Rows.Count, 2).End(xlUp).Row
            For x = LastRow To 1 Step -1
                If Cells(x, 2).Value <> "TOTAL COUNT BY DAY" Then
                Rows(x).Delete
                End If
            Next x
    
            Columns("A:E").Delete
            Cells(1, 1).Select
    
            LRow = Cells(Rows.Count, 1).End(xlUp).Row
        
            'Copy rows and transpose vertically onto Compile ws
            For i = 1 To LRow Step 1
                Rows(i).Copy
                Sheets("Compiled").Range("B" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues, Transpose:=True
            Next i
            
            'Fills in zero for blank cells
            With Sheets("Compiled")
                .Range("B368:B732").SpecialCells(xlCellTypeBlanks).Value = "0"
            End With
        End If
    Next y
    
    Sheets("Compiled").Activate

End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Forum statistics

Threads
1,214,819
Messages
6,121,739
Members
449,050
Latest member
excelknuckles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top