+ Reply to Thread
Results 1 to 9 of 9

If cell equals "CR" then make cell to left negative and delete "CR".......

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-09-2013
    Location
    California, USA
    MS-Off Ver
    MS 365 Subscription
    Posts
    130

    If cell equals "CR" then make cell to left negative and delete "CR".......

    What I need it to do is look in a large range for and cell that equals "CR", then change the cell to the left of it to a negative and remove the "CR".

    I have a code that I was working for a day. But now when I run the macro, it no longer does anything. Could it be a setting that I am overlooking, or is there a more reliable way to write this code?

    Sub Negative_Convert()
    For Each r In Range("A1:BA" & Cells(Rows.Count, "Y").End(xlUp).Row)
          
          If r.Offset(0, 1).Value = "CR" Then
                r.Value = r.Value * -1
          End If
          
          If r.Value < 0 Then
                r.Offset(0, 1).Value = ""
          End If
    
    Next r
    
    End Sub

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

    Re: If cell equals "CR" then make cell to left negative and delete "CR".......

    I am sorry my friend but your code is no good.

    1. If you are looking at a large range the last thing you want to do is to use a loop.


    2.
    LR = Cells(rows.count,1).End(xlUp).Row
    Will return the last row in column A.
    LR = Cells(Rows.Count, "Y").End(xlUp).Row
    Will return the last row in column Y.

    What makes you sure that Column Y is your longest column?

    Perhaps you should use
    LCR = Selection.SpecialCells(xlCellTypeLastCell).Row
    I will rewrite your code for you.

    This is more complicated than your code but hundreds of times faster.
    Mainly because it uses the find function rather than a loop.

    
    Sub Negative_Convert()
    
    Application.ScreenUpdating = False
    
        Cells.Select
        Set RngLook = Selection
        Range("A1").Select
        
        On Error Resume Next
        
        With RngLook
            Set rngFind = .Find("CR", .Cells(1, 1), LookIn:=xlValues, lookat:=xlWhole)
            If Not rngFind Is Nothing Then
            
                rngFind.Offset(0, -1).Value = -rngFind.Offset(0, -1).Value
                rngFind.Value = ""
                
                Do
                Set rngFind = .FindNext(rngFind)
                rngFind.Offset(0, -1).Value = -rngFind.Offset(0, -1).Value
                rngFind.Value = ""
                Loop While Not rngFind Is Nothing
            End If
        End With
    
        On Error GoTo 0
        
    Application.ScreenUpdating = True
    
    End Sub
    Last edited by mehmetcik; 01-14-2016 at 01:39 PM.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: If cell equals "CR" then make cell to left negative and delete "CR".......

    You said the cell to the left, but Offset(0,1) is the cell to the right. Offset(0,-1) would be the cell to the left. Not sure which you want, adjust the code below accordingly.

    Sub pas()
    With Range("A:BA")
    Set c = .Find("CR", lookat:=xlWhole)
    If Not c Is Nothing Then
    FirstAdd = c.Address
    Do
    c.Offset(0, 1).Value = c.Offset(0, 1).Value * -1
    c.ClearContents
    Set c = .FindNext(c)
    Loop While Not c Is Nothing And c.Address <> FirstAdd
    End If
    End With
    End Sub
    I'm interested in starting a career working with VBA, if anyone knows of any opportunities!

  4. #4
    Forum Contributor
    Join Date
    09-09-2013
    Location
    California, USA
    MS-Off Ver
    MS 365 Subscription
    Posts
    130

    Re: If cell equals "CR" then make cell to left negative and delete "CR".......

    Quote Originally Posted by walruseggman View Post
    You said the cell to the left, but Offset(0,1) is the cell to the right. Offset(0,-1) would be the cell to the left. Not sure which you want, adjust the code below accordingly.

    Sub pas()
    With Range("A:BA")
    Set c = .Find("CR", lookat:=xlWhole)
    If Not c Is Nothing Then
    FirstAdd = c.Address
    Do
    c.Offset(0, 1).Value = c.Offset(0, 1).Value * -1
    c.ClearContents
    Set c = .FindNext(c)
    Loop While Not c Is Nothing And c.Address <> FirstAdd
    End If
    End With
    End Sub
    Thank you very much for helping me!
    I just testing this code and it worked, but it does give me an error on the "Loop..." line once the code is done.
    The error message says "Run-time error '91': Object variable or with Block variable not set."

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

    Re: If cell equals "CR" then make cell to left negative and delete "CR".......

    walruseggmans code would work if you inserted the two lines

    On Error Resume Next
    and

    On Error Goto 0
    The reason being that you are Deleting CR, eventually there will be no CR to find giving you an error.

    you should also remove all reference to c.address for the same reason.
    Turning off screen updates would make it even faster.

    
    Sub pas()
    
    On Error Resume Next
    Set c = Range("A:BA").Find("CR", lookat:=xlWhole)
    If Not c Is Nothing Then
    Do
    c.Offset(0, -1).Value = -c.Offset(0, -1).Value
    c.ClearContents
    Set c = Range("A:BA").FindNext(c)
    Loop While Not c Is Nothing
    End If
    
    On Error GoTo 0
    End Sub
    Last edited by mehmetcik; 01-14-2016 at 01:49 PM.

  6. #6
    Forum Contributor
    Join Date
    09-09-2013
    Location
    California, USA
    MS-Off Ver
    MS 365 Subscription
    Posts
    130

    Re: If cell equals "CR" then make cell to left negative and delete "CR".......

    Quote Originally Posted by mehmetcik View Post
    walruseggmans code would work if you inserted the two lines

    On Error Resume Next
    and

    On Error Goto 0
    The reason being that you are Deleting CR, eventually there will be no CR to find giving you an error.

    you should also remove all reference to c.address for the same reason.
    Turning off screen updates would make it even faster.

    
    Sub pas()
    
    On Error Resume Next
    Set c = Range("A:BA").Find("CR", lookat:=xlWhole)
    If Not c Is Nothing Then
    Do
    c.Offset(0, -1).Value = -c.Offset(0, -1).Value
    c.ClearContents
    Set c = Range("A:BA").FindNext(c)
    Loop While Not c Is Nothing
    End If
    
    On Error GoTo 0
    End Sub
    This code worked great!! Thank you guys very much for helping me

  7. #7
    Forum Contributor
    Join Date
    09-09-2013
    Location
    California, USA
    MS-Off Ver
    MS 365 Subscription
    Posts
    130

    Re: If cell equals "CR" then make cell to left negative and delete "CR".......

    I had one more question about this code. I saved this macro in a master file that opens at startup that is hidden so that I can use this macro in all my workbooks.
    When I run the macro in another workbook it work, but it seems to continue to loop. It locks up excel until I hit the ESC key.
    But when I run this code in the workbook that it is saved in, it does not lock up excel and works just fine.

    Do you know why this could be?

    Thank you

  8. #8
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: If cell equals "CR" then make cell to left negative and delete "CR".......

    @mehmetcik: Good point, wasn't thinking about that.

    @pas: You'll need to also remove And c.Address <> FirstAdd from mehmetcik's modified code.

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

    Re: If cell equals "CR" then make cell to left negative and delete "CR".......

    I cannot see any reason for that.

    I pasted the last code into my Peersonal workbook, also a hidden book and ran the macro with no issues.

+ 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. Replies: 35
    Last Post: 01-13-2016, 02:16 AM
  2. Replies: 1
    Last Post: 11-04-2015, 04:34 AM
  3. "Form and macro" to delete "Active" cell contents.
    By wanty in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-22-2013, 11:33 PM
  4. [SOLVED] Delete row if cell in Column N equal "Different" or Column P equals "Blank"
    By Cyberpawz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-17-2012, 08:25 AM
  5. Replies: 5
    Last Post: 06-26-2006, 09:23 PM
  6. If changed array formula reduce ""\""\""\ - signs to #Missing, will it make ...
    By Maria J-son in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-05-2006, 08:25 AM
  7. Replies: 6
    Last Post: 01-08-2006, 06:20 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