+ Reply to Thread
Results 1 to 13 of 13

Removing blanks where blanks show blank but are code to show blank.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-25-2011
    Location
    London, England
    MS-Off Ver
    office 2021
    Posts
    318

    Removing blanks where blanks show blank but are code to show blank.

    Hi Guys
    I have the following code which was kindly given to me by a colleague at ExcelForum. It was designed to remove one row if that row was empty which it does. I have tried to use it in another location where there are several hundred blank rows which it was not designed for and takes a very long time removing some. Eventually having moved some it gives up.
    Firstly I can’t work out why it is removing some as they appear identical to the ones not removed
    Secondly it is very slow.
    So is there any code that would remove these blanks.
    Here is the code I have

    'This will remove the blank row only if there is one
    'Sub checkTwoCols()
    'Option Explicit
    
        Dim cel As Range, rng As Range
         Set rng = Range("A2", Range("A65536").End(xlUp))
          For Each cel In rng
            If cel.Value = "" Then
                If cel.Offset(, 2).Value = "" Then
                   cel.EntireRow.Delete
                End If
           End If
        Next cel
    End Sub

    The problem is that I have now run a macro to pull from another sheet the data I need. The macro shows “ “ in the cells I do not need but behind that there is the code. I do not know how Excel recognises this. If anyone has a suggestion I would be thrilled.
    I am uploading the workbook which puts my comments into context. The sheet I am working in is Petty cash and the data is being pulled from BDC2. The macro (4) has just been run. There are entries in the petty cash sheet. Some of the columns are hidden because there are not needed.

    Thanks in advance
    Mark
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Removing blanks where blanks show blank but are code to show blank.

    Hi mark

    Try this link

    http://www.ozgrid.com/VBA/VBACode.htm

  3. #3
    Forum Contributor
    Join Date
    03-25-2011
    Location
    London, England
    MS-Off Ver
    office 2021
    Posts
    318

    Re: Removing blanks where blanks show blank but are code to show blank.

    I had gone through all the macros on this sheet before posting my question. Still do not have the solution. I am sure it is somewhere but where? Thanks.

  4. #4
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Removing blanks where blanks show blank but are code to show blank.

    This worked for me:-

    
    Sub Macro3()
    
    Range("A1:" & Selection.SpecialCells(xlCellTypeLastCell).Address).Select
    
    Dim i As Long
            'We turn off calculation and screenupdating to speed up the macro.
    
            With Application
                    .Calculation = xlCalculationManual
                    .ScreenUpdating = False
    
            'We work backwards because we are deleting rows.
    
            For i = Selection.Rows.Count To 1 Step -1
    
                    If WorksheetFunction.CountA(Selection.Rows(i)) = 0 Then
                            Selection.Rows(i).EntireRow.Delete
                    End If
            Next i
    
                    .Calculation = xlCalculationAutomatic
    
                    .ScreenUpdating = True
            End With
            
            Range("A1").Select
            
    End Sub

  5. #5
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Removing blanks where blanks show blank but are code to show blank.

    Well in your workbook code, the bit that you have put in the post that deletes the empty rows has been commented out? I'd suggest you need to step through backwards though otherwise it will skip rows:
    for count = Range("A" & rows.count).end(xlup).row to 1 step -1
       If Range("A" & count) = "" then rows(count).delete
    next

  6. #6
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: Removing blanks where blanks show blank but are code to show blank.

    You can also try this code as well

    Sub checkTwoCols()
    Dim i As Integer, lr As Integer
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Petty cash")
    ws.Activate
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    For i = lr To 2 Step -1
        Range("A" & i).Select 'you will be able to view the active cell during debugging otherwise you can delete this line
        If Range("A" & i).Value = "" And Range("A" & i).Formula <> "" Then
            If Range("A" & i).Offset(0, 4).Value = "" Then
            Range("A" & i).EntireRow.Delete
            End If
        End If
    Next i
    End Sub
    Hope this helps.
    Last edited by sktneer; 11-07-2013 at 11:12 AM.
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  7. #7
    Forum Contributor
    Join Date
    03-25-2011
    Location
    London, England
    MS-Off Ver
    office 2021
    Posts
    318

    Re: Removing blanks where blanks show blank but are code to show blank.

    Hi Yudlugar and Sktneer

    Youlugar I am not sure where to enter this code. I have played about with it to no avail.

    And Sktneer I copied you code into my VBA and ran it. Nothing happened.

    Still trying to see how to use either one.

    Mark

  8. #8
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: Removing blanks where blanks show blank but are code to show blank.

    Quote Originally Posted by markDuffy View Post

    And Sktneer I copied you code into my VBA and ran it. Nothing happened.

    Mark
    I don't understand that what do you mean by nothing happened.
    When I ran the code, Only 73 rows were left on petty cash sheet in comparison to 873 rows initially. The code deleted all the rows based on your criteria.
    Please see the attachment. The code is on Module3. You can compare the attached sheet with the sheet you uploaded in post#1 to see if you get the desired result.
    Attached Files Attached Files

  9. #9
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Removing blanks where blanks show blank but are code to show blank.

    Mark, your "Macro4" should be:
    Sub Macro4()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Sheets("Petty Cash").Range("A2:Q" & Sheets("BDC2").Range("C" & Rows.Count).End(xlUp).Row).FormulaR1C1 = "=IF('BDC2'!RC16= ""petty cash"",'BDC2'!RC, """")"
    Application.Calculate
    For Count = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
       If Range("A" & Count) = "" And Range("C" & Count) = "" Then Rows(Count).Delete
    Next
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    End Sub

  10. #10
    Forum Contributor
    Join Date
    03-25-2011
    Location
    London, England
    MS-Off Ver
    office 2021
    Posts
    318

    Re: Removing blanks where blanks show blank but are code to show blank.

    Hi Yudlugar

    Thanks again for a delightful piece of code. Don't know how long it is going to take me till I am able to write code which does what it should. I will keep studying what you have given me and see how much of it I can understand. I may have committed a most cardinal sin in that I had not tested correctly the code given to me by Sktneer which actually worked. By studying that also there is a hope I will improve.

    All the Best Mark

  11. #11
    Forum Contributor
    Join Date
    03-25-2011
    Location
    London, England
    MS-Off Ver
    office 2021
    Posts
    318

    Re: Removing blanks where blanks show blank but are code to show blank.

    Hi Sktneer
    I must have copied something incorrectly or other. I have run yours as above and its great. I don't know how to apologise enough as the time and effort given by you is so appreciated. I will now go back and see where I went wrong. I am learning every time I do something but it is not recognisable to me yet to spot where i have done something incorrect.

    But thank you very very much

    All the Best
    Mark

  12. #12
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: Removing blanks where blanks show blank but are code to show blank.

    Never mind. Glad to help you. Thanks for the feedback and the rep.

  13. #13
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Removing blanks where blanks show blank but are code to show blank.

    Mark,

    Not a problem - few pointers:

    -I think one of the reasons your original code was running very slowly was that everytime you deleted a line of code the entire sheet recalculated itself. This takes a bit of time when you have so many formulas in the sheet.

    -Your original code assigned the same r1c1 formula to several columns individually, this was unneccessary, you could do it all in one go.

    -When deleting rows, you need to loop backwards otherwise you will skip some.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA Code to Copy Non Blanks/Cells with Values until Formula returns as blank
    By FLani in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-16-2013, 10:18 PM
  2. [SOLVED] Find first non-blank cell after a group of blanks copy offset cells, find next blank, loop
    By gwsampso in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-21-2012, 06:11 AM
  3. Replies: 4
    Last Post: 08-29-2012, 02:45 PM
  4. Replies: 1
    Last Post: 04-26-2008, 08:00 AM
  5. [SOLVED] Hide show blanks
    By PCOR in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-26-2005, 05:19 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1