+ Reply to Thread
Results 1 to 2 of 2

How do I simplify this macro?

Hybrid View

  1. #1
    Registered User
    Join Date
    10-16-2009
    Location
    Chicago, IL
    MS-Off Ver
    Excel XP
    Posts
    9

    How do I simplify this macro?

    So I'm doing the same thing over and over for each worksheet that I have and its actually not working anymore, However when I just one run one set it works perfectly fine. The macro is supposed to copy any line that has the status Sold to another worksheet. Thanks in advance

    Sub DidCellsChange()
       
        Dim cell        As Range
        
        For Each cell In Worksheets("ShirinJewelers").Range("E4:E50")
            If cell.Value = "Sold" Then
                With Cells(cell.Row, "A").Resize(, 17)
                    Worksheets("ShirinSold").Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(, 6).Value = .Value
                    .ClearContents
                End With
            End If
        Next cell
        
            For Each cell In Worksheets("Treasures").Range("E4:E50")
            If cell.Value = "Sold" Then
                With Cells(cell.Row, "A").Resize(, 17)
                    Worksheets("TreaChiSold").Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(, 6).Value = .Value
                    .ClearContents
                End With
            End If
        Next cell
        
            For Each cell In Worksheets("ExoticDiamonds").Range("E4:E50")
            If cell.Value = "Sold" Then
                With Cells(cell.Row, "A").Resize(, 17)
                    Worksheets("ExoticSold").Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(, 6).Value = .Value
                    .ClearContents
                End With
            End If
        Next cell
        
                For Each cell In Worksheets("Highline").Range("E4:E50")
            If cell.Value = "Sold" Then
                With Cells(cell.Row, "A").Resize(, 17)
                    Worksheets("HighSold").Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(, 6).Value = .Value
                    .ClearContents
                End With
            End If
        Next cell
        
            For Each cell In Worksheets("TreasuresJefferson").Range("E4:E50")
            If cell.Value = "Sold" Then
                With Cells(cell.Row, "A").Resize(, 17)
                    Worksheets("TreaJeffSold").Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(, 6).Value = .Value
                    .ClearContents
                End With
            End If
        Next cell
        
            For Each cell In Worksheets("Ajs").Range("E4:E50")
            If cell.Value = "Sold" Then
                With Cells(cell.Row, "A").Resize(, 17)
                    Worksheets("AjsSold").Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(, 6).Value = .Value
                    .ClearContents
                End With
            End If
        Next cell
        
            For Each cell In Worksheets("GoldenFever").Range("E4:E50")
            If cell.Value = "Sold" Then
                With Cells(cell.Row, "A").Resize(, 17)
                    Worksheets("GoldFevSold").Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(, 6).Value = .Value
                    .ClearContents
                End With
            End If
        Next cell
        
            For Each cell In Worksheets("ForeverDiamonds").Range("E4:E50")
            If cell.Value = "Sold" Then
                With Cells(cell.Row, "A").Resize(, 17)
                    Worksheets("ForevDiaSold").Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(, 6).Value = .Value
                    .ClearContents
                End With
            End If
        Next cell
        
            For Each cell In Worksheets("DiamondTreasures").Range("E4:E50")
            If cell.Value = "Sold" Then
                With Cells(cell.Row, "A").Resize(, 17)
                    Worksheets("DiaTreaSold").Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(, 6).Value = .Value
                    .ClearContents
                End With
            End If
            
            
        Next cell
    End Sub

  2. #2
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: How do I simplify this macro?

    I don't know of a way to make it more efficient, but you can get rid of all the redundant typing by using a variable for the worksheet name, then calling the sub once for each different worksheet.

    Example:

    Sub DidCellsChange(WorksheetName As String)
       
        Dim cell        As Range
        
        For Each cell In Worksheets(WorksheetName).Range("E4:E50")
            If cell.Value = "Sold" Then
                With Cells(cell.Row, "A").Resize(, 17)
                    Worksheets(WorksheetName).Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(, 6).Value = .Value
                    .ClearContents
                End With
            End If
        Next cell
        
    End Sub
    
    Sub snooze24()
    
    DidCellsChange "ShirinJewelers"
    DidCellsChange "Treasures"
    DidCellsChange "ExoticDiamonds"
    DidCellsChange "Highline"
    DidCellsChange "TreasuresJefferson"
    DidCellsChange "Ajs"
    DidCellsChange "GoldenFever"
    DidCellsChange "ForeverDiamonds"
    DidCellsChange "DiamondTreasures"
    
    End Sub
    If you need anything else please ask.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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