+ Reply to Thread
Results 1 to 3 of 3

problem with MovebasedonValue function

Hybrid View

nicky1130 problem with MovebasedonValue... 03-18-2024, 01:53 AM
TMS Re: problem with... 03-18-2024, 03:43 AM
bakerman2 Re: problem with... 03-19-2024, 11:30 PM
  1. #1
    Registered User
    Join Date
    03-18-2024
    Location
    Willis, Texas
    MS-Off Ver
    Microsoft 365
    Posts
    1

    problem with MovebasedonValue function

    I am using the code below to move rows from one worksheet to another when "Done" is selected from a drop down menu.?* It worked fine on my computer and when I first shared with another user however now it freezes excel and you have to restart the program.?* She (other user) is on a network shared folder; i am not.?* Can anyone help??* I have included code below.



    Sub MoveBasedOnValue()
    
    
    
    Dim xRg As Range
    
    Dim xCell As Range
    
    Dim A As Long
    
    Dim B As Long
    
    Dim C As Long
    
    A = Worksheets("master").UsedRange.Rows.Count
    
    B = Worksheets("completed").UsedRange.Rows.Count
    
    If B = 1 Then
    
    If Application.WorksheetFunction.CountA(Worksheets("completed").UsedRange) = 0 Then B = 0
    
    End If
    
    Set xRg = Worksheets("master").Range("AC7:AC" & A)
    
    On Error Resume Next
    
    Application.ScreenUpdating = False
    
    For C = 1 To xRg.Count
    
    If CStr(xRg(C).Value) = "Done" Then
    
    xRg(C).EntireRow.Copy Destination:=Worksheets("completed").Range("A" & B + 1)
    
    xRg(C).EntireRow.Delete
    
    If CStr(xRg(C).Value) = "Done" Then
    
    C = C - 1
    
    End If
    
    B = B + 1
    
    End If
    
    Next
    
    Application.ScreenUpdating = True
    
    End Sub
    
    
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim Z As Long
    
    Dim xVal As String
    
    On Error Resume Next
    
    If Intersect(Target, Range("AC:AC")) Is Nothing Then Exit Sub
    
    Application.EnableEvents = False
    
    For Z = 1 To Target.Count
    
    If Target(Z).Value > 0 Then
    
    Call MoveBasedOnValue
    
    End If
    
    Next
    
    Application.EnableEvents = True
    
    End Sub
    Attached Files Attached Files
    Last edited by AliGW; 03-18-2024 at 03:44 AM. Reason: Code tags added - please review the forum guidelines.

  2. #2
    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
    48,515

    Re: problem with MovebasedonValue function

    Step through the code using F8. Check the value of xRg.Count. I suspect the Used Range may be very high for some reason. I think that Excel is not actually freezing, just taking a long time to work through a lot of rows.
    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


  3. #3
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,358

    Re: problem with MovebasedonValue function

    If you want to stick to a 2 sub solution change both subs to this.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Not Intersect(Target, Range("AC:AC")) Is Nothing Then
    
            If Target.Value = "Done" Then
            
                Application.EnableEvents = False
        
                wRow = Target.Row: Call MoveBasedOnValue: wRow = 0
                
                Application.EnableEvents = True
            
            End If
            
        End If
    
    
        
    End Sub
    and

    Public wRow As Long
    
    Sub MoveBasedOnValue()
    
        Dim B As Long
    
        B = Sheets("completed").Range("B" & Rows.Count).End(xlUp).Row 'Worksheets("completed").UsedRange.Rows.Count
    
        If B = 1 Then
        
            If Application.CountA(Worksheets("completed").UsedRange) = 0 Then B = 0
        
        End If
    
        Application.ScreenUpdating = False
    
        With Sheets("master")
            
            .Unprotect
        
            .Rows(wRow).EntireRow.Copy Sheets("completed").Range("A" & B + 1)
        
            .Rows(wRow).EntireRow.Delete xlUp
            
            .Protect
            
        End With
        
        Application.ScreenUpdating = True
    
    End Sub
    Last edited by bakerman2; 03-19-2024 at 11:40 PM.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

+ 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. [SOLVED] Problem with indirect function in XIRR function
    By lynnsong986 in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 01-17-2020, 05:12 PM
  2. Function problem: Function correct but not running
    By helterskelter101 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-04-2018, 10:17 AM
  3. [VBA] Problem with outmail function - function sends mails only to 1 recepient
    By sauron12 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-21-2015, 03:30 PM
  4. If function problem - Please help
    By ExcelSheep in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-17-2015, 10:20 AM
  5. Replies: 6
    Last Post: 10-20-2013, 07:16 PM
  6. Problem Inserting Round function into an IF function
    By Ash87 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-11-2013, 05:37 PM
  7. Function Problem
    By SPORTS96 in forum Excel General
    Replies: 9
    Last Post: 02-16-2007, 06:32 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