Consulting

Results 1 to 10 of 10

Thread: Removing blank lines from report calculated by macro

  1. #1
    VBAX Regular
    Joined
    Feb 2016
    Posts
    41
    Location

    Removing blank lines from report calculated by macro

    Hi - I'm sure my subject line is super confusing, but I can explain.
    I have the below code (thanks to help I got here!) that makes wonderful reports and exports them to a new location.
    However, they are being exported with several blank lines where there was once a formula. I don't think Excel is seeing them as blank lines though, which might be a problem. Is there a way to alter the below code to remove those blank lines?

    Previous forum - http://www.vbaexpress.com/forum/show...412#post338412

    Code
    Option Explicit
     
    Sub DiscrepancyReport()
        Dim Wb As Workbook
        Dim xWs As Worksheet
        Dim DateBox As String
          Dim xPath As String
        xPath = ThisWorkbook.Path
         
        DateBox = InputBox("DISCREPANCY REPORTS: Please enter the date YYYYMM")
         
            'Application.ScreenUpdating = False
         'Application.DisplayAlerts = False
         
        For Each xWs In ThisWorkbook.Sheets
            If xWs.Name <> "MACROS" And xWs.Name <> "Flat File" And xWs.Name <> "DisInput" And xWs.Name <> "DisReport" Then
                xWs.Cells.Copy Sheets("DisInput").Range("A1")
                Application.Calculate
                Sheets("DisReport").Copy
                With ActiveSheet.Cells
                    .Copy
                    .PasteSpecial Paste:=xlPasteValues
                End With
                  Application.ActiveWorkbook.SaveAs Filename:=xPath & "\" _
                & DateBox & "_" _
                & Sheets("DisReport").Range("AK2") _
                & "_Discrepancy Report" & ".xlsx"
                Application.ActiveWorkbook.Close False
            End If
        Next
         
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
       ThisWorkbook.Activate
         End Sub

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I don't think Excel is seeing them as blank lines though, which might be a problem.
    Why do you think that?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Regular
    Joined
    Feb 2016
    Posts
    41
    Location
    Quote Originally Posted by SamT View Post
    Why do you think that?
    Don't laugh at my terminology here, but when I do a ctrl-shift-down, it goes down to where my formula ended whether there is data populated in the cells or not.

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    What is in the Formula Bar when you stop there?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    VBAX Regular
    Joined
    Feb 2016
    Location
    Bangalore,India
    Posts
    11
    Location
    do you want to remove the entire row of blanks then use this code

    select a column i assume here column as a:a

    Range("a:a").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

    thats it

  6. #6
    VBAX Regular
    Joined
    Feb 2016
    Posts
    41
    Location
    Quote Originally Posted by SamT View Post
    What is in the Formula Bar when you stop there?
    Nothin'. That's why I don't think Excel is seeing these lines as "blank." I could be wrong.
    VBA is completely new to me, but it's cut a 2 hour project each month down to about 30 seconds, so I'm pleased so far.

  7. #7
    VBAX Regular
    Joined
    Feb 2016
    Posts
    41
    Location
    Quote Originally Posted by shailendranr View Post
    do you want to remove the entire row of blanks then use this code

    select a column i assume here column as a:a

    Range("a:a").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

    thats it
    How does this fit into the code I posted?

  8. #8
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I would live with it, unless it becomes an issue.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  9. #9
    VBAX Regular
    Joined
    Feb 2016
    Posts
    41
    Location
    Quote Originally Posted by SamT View Post
    I would live with it, unless it becomes an issue.
    The only issue I have is that it makes the file size so large.

  10. #10
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Sub ExcelDiet()
    'http://www.vbaexpress.com/kb/getarticle.php?kb_id=83
    'By Jacob Hilderbrand
    Dim j As Long
    Dim k As Long
    Dim LastRow As Long
    Dim LastCol As Long
    Dim ColFormula As Range
    Dim RowFormula As Range
    Dim ColValue As Range
    Dim RowValue As Range
    Dim Shp As Shape
    Dim ws As Worksheet

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    On Error Resume Next

    For Each ws In Worksheets
    With ws
    'Find the last used cell with a formula and value
    'Search by Columns and Rows
    On Error Resume Next
    Set ColFormula = .Cells.Find(What:="*", After:=Range("A1"), LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)
    Set ColValue = .Cells.Find(What:="*", After:=Range("A1"), LookIn:=xlValues, _
    LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)
    Set RowFormula = .Cells.Find(What:="*", After:=Range("A1"), LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
    Set RowValue = .Cells.Find(What:="*", After:=Range("A1"), LookIn:=xlValues, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
    On Error GoTo 0

    'Determine the last column
    If ColFormula Is Nothing Then
    LastCol = 0
    Else
    LastCol = ColFormula.Column
    End If
    If Not ColValue Is Nothing Then
    LastCol = Application.WorksheetFunction.Max(LastCol, ColValue.Column)
    End If

    'Determine the last row
    If RowFormula Is Nothing Then
    LastRow = 0
    Else
    LastRow = RowFormula.Row
    End If
    If Not RowValue Is Nothing Then
    LastRow = Application.WorksheetFunction.Max(LastRow, RowValue.Row)
    End If

    'Determine if any shapes are beyond the last row and last column
    For Each Shp In .Shapes
    j = 0
    k = 0
    On Error Resume Next
    j = Shp.TopLeftCell.Row
    k = Shp.TopLeftCell.Column
    On Error GoTo 0
    If j > 0 And k > 0 Then
    Do Until .Cells(j, k).Top > Shp.Top + Shp.Height
    j = j + 1
    Loop
    If j > LastRow Then
    LastRow = j
    End If
    Do Until .Cells(j, k).Left > Shp.Left + Shp.Width
    k = k + 1
    Loop
    If k > LastCol Then
    LastCol = k
    End If
    End If
    Next

    .Range(Cells(1, LastCol + 1).Address & ":IV65536").Delete
    .Range(Cells(LastRow + 1, 1).Address & ":IV65536").Delete
    End With
    Next

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True

    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •