+ Reply to Thread
Results 1 to 2 of 2

Something wrong with my code

  1. #1
    Registered User
    Join Date
    09-17-2013
    Location
    Evansville, USA
    MS-Off Ver
    Excel 2007
    Posts
    31

    Something wrong with my code

    I used this code to delete anything in column A that ends in Z, 1 and all consecutive numbers except for the largest number. It works when I use it on a data set of one or two weeks but when I start running this macro on a whole year's worth of data I get an error. The error is highlighted below as such:

    'Delete Z and 1, consecutive number --> keep only the largest

    Dim LR As Long, LR1 As Long
    Dim Rng As Range, cel As Range, i As Long

    Application.ScreenUpdating = False
    With Sheets("5data") '<----Change Sheet Name
    LR = .Range("A" & .Rows.Count).End(xlUp).Row
    LR1 = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious).Row
    .Range(.Cells(LR + 1, "A"), .Cells(LR1, "A")).EntireRow.Delete
    Set Rng = Range("B2:B" & LR)
    With Rng
    For i = LR To 1 Step -1
    If Right(Rng(i).Value, 1) = "Z" Or Right(Rng(i).Value, 1) = "1" Then
    Rng(i).EntireRow.Delete
    End If
    Next
    End With
    End With

    With Sheets("5data") '<----Change Sheet Name
    LR = .Range("A" & .Rows.Count).End(xlUp).Row
    Set Rng = Range("B2:B" & LR)
    With Rng
    For i = LR To 1 Step -1
    If Val(Rng(i).Offset(-1, 0).Value) = Val(Rng(i).Value - 1) Then
    Rng(i).Offset(-1, 0).EntireRow.Delete
    End If
    Next
    End With
    End With
    Application.ScreenUpdating = True

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,945

    Re: Something wrong with my code

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    Also...
    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution.

    Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.

    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Last edited by FDibbins; 11-04-2013 at 07:06 PM.
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

+ 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. what is wrong with this code- error from vba code
    By punna111 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-14-2013, 03:14 PM
  2. Code for email alerts from excel isn't working, wrong code possibly?
    By jessthorogood in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-27-2012, 01:45 AM
  3. Wrong line of code banned me out and can't access vba code...
    By albertc in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 05-14-2012, 02:41 PM
  4. Code wrong
    By Aussie_Striker in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-17-2007, 09:06 AM
  5. [SOLVED] What's wrong with my code ?
    By Don Guillett in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-06-2005, 06:05 AM

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