Closed Thread
Results 1 to 7 of 7

running macro deletes data

  1. #1
    Registered User
    Join Date
    02-02-2015
    Location
    Arizona
    MS-Off Ver
    2013
    Posts
    12

    running macro deletes data

    I have a document that is a Crystal Report exported into Excel. When I run my macro, the data deletes. Attached is my code and document. Need help please!!!! (I don't know if I'm posting this correctly)

    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,151

    Re: running macro deletes data

    Data deletion is caused by excessive use of "Select/Selection" in the code, try this way:
    Please Login or Register  to view this content.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,463

    Re: running macro deletes data

    I haven't had the opportunity to look at the sample workbook but I suspect that Coral Reports produces some/many merged cells which could/would cause full column selection to select more than on column.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Registered User
    Join Date
    02-02-2015
    Location
    Arizona
    MS-Off Ver
    2013
    Posts
    12

    Re: running macro deletes data

    Thank you a million times over. It works!

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,463

    Re: running macro deletes data

    The problem is, as I suspected, that the Report Header, "Action Wine & Spirits" is one Merged Cell crossing all the columns. When you try to Select column E, it actually selects all the columns that the header crosses. Then, when you Delete the column, it deletes ALL the columns.

    By not selecting column E and just deleting it, the issue is resolved.

    There are other Merged Cells in the data rows in columns D:E

    porucha vevrku's solution addresses this problem.


    If you were too add the following lines at the beginning of your macro, your code would work unchanged but it would not be as efficient as the solution provided.

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    05-01-2023
    Location
    australia
    MS-Off Ver
    2303
    Posts
    2

    Exclamation Re: running macro deletes data

    Hi Everyone,

    Trying to learn macros to help my boss with formatting but I can't figure out why this doesn't work, sometimes it just deletes everything and sometimes it only sort of works.

    Sub FORMATDATA()
    '
    ' FORMATDATA Macro
    '
    ' Keyboard Shortcut: Ctrl+w
    '
    Cells.Replace What:="directions", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Cells.Replace What:="website", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Cells.Replace What:="20+ years in business", Replacement:="", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Cells.Replace What:="19+ years in business", Replacement:="", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Cells.Replace What:="18+ years in business", Replacement:="", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Cells.Replace What:="17+ years in business", Replacement:="", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Cells.Replace What:="16+ years in business", Replacement:="", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Cells.Replace What:="15+ years in business", Replacement:="", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Cells.Replace What:="14+ years in business", Replacement:="", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Cells.Replace What:="13+ years in business", Replacement:="", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Cells.Replace What:="12+ years in business", Replacement:="", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Cells.Replace What:="11+ years in business", Replacement:="", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Cells.Replace What:="10+ years in business", Replacement:="", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Cells.Replace What:="9+ years in business", Replacement:="", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Cells.Replace What:="8+ years in business", Replacement:="", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Cells.Replace What:="7+ years in business", Replacement:="", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Cells.Replace What:="6+ years in business", Replacement:="", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Cells.Replace What:="5+ years in business", Replacement:="", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Cells.Replace What:="4+ years in business", Replacement:="", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Cells.Replace What:="3+ years in business", Replacement:="", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Cells.Replace What:="2+ years in business", Replacement:="", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Cells.Replace What:="1+ years in business", Replacement:="", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    With Selection
    Cells.Replace What:="", Replacement:="", LookAt:=xlPart, SearchOrder:= _
    xlByRows, MatchCase:=False, SearchFormat:=True, ReplaceFormat:=True, _
    FormulaVersion:=xlReplaceFormula2
    End With

    With Selection
    .HorizontalAlignment = xlGeneral
    .VerticalAlignment = xlCenter
    .WrapText = True
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
    End With
    With Selection
    .HorizontalAlignment = xlGeneral
    .VerticalAlignment = xlCenter
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
    End With
    Cells.Select
    With Selection
    .WrapText = True
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    End With
    With Selection
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    End With

    Range("A3:A350").Select
    For Each Cell In Selection
    If Cell.Value = "YES" Then
    Rows(Cell.Row).ClearContents
    End If
    Next
    Selection.SpecialCells(xlCellTypeBlanks).Select
    Selection.EntireRow.Delete

    With Selection
    Cells.Select
    ActiveSheet.DrawingObjects.Select
    Selection.Delete
    Range("A4").Select
    End With

    End Sub

  7. #7
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,916

    Re: running macro deletes data

    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Button Click Macro runs but deletes Data
    By ChrisMP in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-08-2019, 09:59 AM
  2. Replies: 2
    Last Post: 02-07-2016, 03:28 PM
  3. Running total and i can enter number in a cell and it deletes
    By joeteresa in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-14-2014, 11:55 AM
  4. [SOLVED] Auto Macro the pulls data from on sheet, prints on another sheet, then deletes information
    By wjwelch1 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-06-2013, 01:54 PM
  5. Macro that deletes data in cells with no colors
    By pezalmendra in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-24-2012, 01:29 PM
  6. How to make a macro that deletes data in an ever-changing range
    By lynv in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-21-2010, 07:04 PM
  7. running macro deletes non designated cells
    By chrismcclain077 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-22-2007, 08:54 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