Ignore/Delete blank rows in Unpivot Code VBA Excel

Philsomedeth

New Member
Joined
Jun 5, 2024
Messages
1
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
I got this VBA code on the internet and I have adapted it to my data, but I don't know how to ignore/delete blank rows in the code I have tried various ways but didn't get the results I want
I hope there is someone who is able to change/add some code that ignore/delete blank rows in the code i hope this make sense

Thank u :)


VBA Code:
Public Sub unpivotData()
   
    Dim staticColumnCount As Integer
    Dim oldRow As Long, theDate As Long, newRow As Long
    Dim inputSheet As Worksheet, outputSheet As Worksheet
    Dim inputSheetName As String, outputSheetName As String

    staticColumnCount = 4
    outputSheetName = "Output"
    inputSheetName = ActiveSheet.Name
    
    
    Set inputSheet = ActiveWorkbook.Worksheets("Sheet3")
    Set outputSheet = Worksheets.Add
            outputSheet.Select
                outputSheet.Name = outputSheetName
    

        For i = 1 To staticColumnCount
            outputSheet.Cells(1, i).Value = inputSheet.Cells(1, i).Value
        Next i

            outputSheet.Cells(1, staticColumnCount + 1).Value = "RTA"
            outputSheet.Cells(1, staticColumnCount + 2).Value = "QTY"
    
            inputRow = 2
            outputRow = 2
    
                Do While inputSheet.Cells(inputRow, 1).Value <> ""
                    inputColumn = staticColumnCount + 1
                        Do While inputSheet.Cells(1, inputColumn).Value <> ""
                            For j = 1 To staticColumnCount
                                outputSheet.Cells(outputRow, j).Value = inputSheet.Cells(inputRow, j)
                            Next j
                        outputSheet.Cells(outputRow, staticColumnCount + 1).Value = inputSheet.Cells(1, inputColumn)
                            outputSheet.Cells(outputRow, staticColumnCount + 2).Value = inputSheet.Cells(inputRow, inputColumn)
                            inputColumn = inputColumn + 1
                        outputRow = outputRow + 1
                        Loop
                    inputRow = inputRow + 1
                Loop
    
    outputSheet.Columns("E:E").NumberFormat = "m/d/yyyy"
        outputSheet.Cells(1, 1).Select
    
    MsgBox "Data has been transposed"


End Sub


Gambar sebelum Unpivot
1717638166244.png


Result like this, but ignore/delete blank rows
1717638181544.png
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
After the static column section, try something like this (please test on a copy first)
VBA Code:
If (Len(inputSheet.Cells(1, inputColumn) > 0) Then
     ' this is the original code
    outputSheet.Cells(outputRow, staticColumnCount + 1).Value = inputSheet.Cells(1, inputColumn)
    outputSheet.Cells(outputRow, staticColumnCount + 2).Value = inputSheet.Cells(inputRow, inputColumn)
    outputRow = outputRow + 1
Else
    outputsheet.cells(outputrow, 1).EntireRow.Delete
End If
' move this outside of the if statement
inputColumn = inputColumn + 1

You may want to turn off screen updating / automatic calculations depending on your expected data size (I would do it regardless).
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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