Skip Row If Cell Is Empty

dirk1965

Board Regular
Joined
Feb 18, 2004
Messages
241
I have the following vba code which is normalizing some data from a pivot:

Code:
Dim LastRow As Long
    Dim LastCol As Long
    
    eRow = 1
    Do While Application.CountA(ws.Rows(eRow).EntireRow) > 0
        eRow = eRow + 1
    Loop
    
    eCol = 1
    Do While Application.CountA(ws.Columns(eCol).EntireColumn) > 0
        eCol = eCol + 1
    Loop
    
    Set Rng = Range(Cells(1, 1), Cells((eRow - 1), (eCol - 1)))

    If Rng Is Nothing Then
    Else
        PTOutput.Application.ScreenUpdating = False
       
        PTOutput.Range("A1").Offset(0, 0) = Rng.Range("A1").Value
        PTOutput.Range("A1").Offset(0, 1) = Rng.Range("B1").Value
        PTOutput.Range("A1").Offset(0, 2) = Rng.Range("C1").Value
        PTOutput.Range("A1").Offset(0, 3) = "Date"
        PTOutput.Range("A1").Offset(0, 4) = "Value"
        i = 1
        For r = 1 To Rng.Rows.Count - 1
            For c = 3 To Rng.Columns.Count - 1
                PTOutput.Range("A1").Offset(i, 0) = Rng.Offset(r, 0).Value
                PTOutput.Range("A1").Offset(i, 1) = Rng.Offset(r, 1).Value
                PTOutput.Range("A1").Offset(i, 2) = Rng.Offset(r, 2).Value
                PTOutput.Range("A1").Offset(i, 3) = Rng.Offset(0, c).Value
                PTOutput.Range("A1").Offset(i, 4) = Rng.Offset(r, c).Value
                i = i + 1
            Next c
        Next r
        
        Application.ScreenUpdating = True

End If

How do I skip the whole row if PTOutput.Range("A1").Offset(0, 4) = "Value" is empty/blank?

Thanks
 
What cells does the rng refer for?
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
What cells does the rng refer for?

The rng refers to the range of the data that is being unpivot'd. That is determined by where the first blank row and first blank column is.
The If clause should check for null/blank cell in column e which has a the header name 'Value'.

I hope one of those is what you're asking about. If not, let me know.

Thanks
 
Upvote 0
I have reworked you macro according to what I think you want to do. Hopefully, I have interpreted properly. See if this macro works for you.
Code:
Sub NormalizeData()
    Application.ScreenUpdating = False
    Dim Raw As Worksheet
    Dim PTOutput As Worksheet
    Dim Rng As Range
    Set PTOutput = Worksheets("Pivot")
    Set Raw = ActiveWorkbook.Sheets("Raw")
    Dim LastRow As Long
    LastRow = Raw.Columns(1).SpecialCells(xlCellTypeBlanks).Cells(1, 1).Row - 1
    Dim LastCol As Long
    LastCol = Raw.Cells(1, Columns.Count).End(xlToLeft).Column - 2
    Dim r As Long, c As Long
    PTOutput.Range("A1:E1") = Array("OffsetA", "OffsetB", "OffsetAPI", "Date", "Value")
    For r = 2 To LastRow
        For c = 4 To LastCol
            If Raw.Cells(r, c) <> "" Then
                PTOutput.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = Raw.Cells(r, 1)
                PTOutput.Cells(Rows.Count, "B").End(xlUp).Offset(1, 0) = Raw.Cells(r, 2)
                PTOutput.Cells(Rows.Count, "C").End(xlUp).Offset(1, 0) = Raw.Cells(r, 3)
                PTOutput.Cells(Rows.Count, "D").End(xlUp).Offset(1, 0) = Raw.Cells(1, c)
                PTOutput.Cells(Rows.Count, "E").End(xlUp).Offset(1, 0) = Raw.Cells(r, c)
            End If
        Next c
    Next r
    Set Rng = Range(Cells(1, 1), Cells(LastRow, LastCol))
    Rng.Name = "OffsetData"
    PTOutput.Activate
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Sorry for the delay. Been on a business trip. This worked out perfectly. Thanks for the assist and I've done the "Thank You".
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

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