• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA code works in excel, but not on my file

Jamie

New Member
Having difficulties with two codes:

Code:
Option Explicit

Sub CleanUp()
Dim lastRow As Long
lastRow = Cells.SpecialCells(xlCellTypeLastCell).Row

Application.EnableEvents = False
'Keep all rows that have been edited
Range("Z2:Z" & lastRow).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Application.EnableEvents = True
End Sub
Sub CleanUpCols()
Dim lastCol As Long
lastCol = Cells.SpecialCells(xlCellTypeLastCell).Column

Application.EnableEvents = False
'Keep all rows that have been edited
Range("A1", Cells(1, lastCol)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
Application.EnableEvents = True
End Sub

The point is to keep all rows that have been edited and to delete all columns that have not been edited (edited as is in -- text being changed, added or deleted). These codes work in excel, but they are not working on my attached template. I am wondering if I am missing some formatting somewhere that is causing problems? Any help is very much appreciated.
 

Attachments

  • Template.xls
    39 KB · Views: 2
Hello again Jamie.
Your attached workbook doesn't seem to have any data or code attached to it. Can you explain further what isn't working? :(
 
Hey Luke! I tried reaching back out to you but I haven't heard anything. I used the macros exactly how you gave them to me but they don't seem to be working on this particular excel sheet. It deletes everything whether edited or not. Here's the macro enabled file. . .but if you add some text to a few rows and columns and then run it, hopefully you'll see what I'm trying to explain.
 

Attachments

  • Template.xlsm
    17.5 KB · Views: 3
Oh I see, I didn't understand how an 'event' worked (this is new to me) and didn't realize it would be in a different place. Is it necessary to have the helper column and row? I see where I can modify where they go, but can they be removed or is there something I can add so they don't show? (This is different from the worksheet_open, which I do find helpful if I must have the helper column)
 
Not a problem, it was easy to miss. :)
Need to have them at least somewhere, as it is what XL later reads when it does the cleanup. You can certainly move them. You could even make the row/column hidden, as the macro doesn't need cells to be unhidden in order to read/write to them.

If you do move them, make sure to make the appropriate change in both the change event and in the cleanup macro.
 
Alrighty, cool. Let me play some more and I'll let you know if I run into anymore 'bumps' in the road. It's been a really great macro to work with, and I think I may finally have gotten the hang of it. You've been a huge help!
 
Back
Top