Clean up code

ozhunter

New Member
Joined
Dec 12, 2012
Messages
19
Hello

After a prompt from MARK858 I have been re-writing my existing code without using select and code that doesn't need to be there. The code below is what I have come up with. It works as it is supposed to. My question is:

Is there anything I could do to clean this code up more, or do things better?

Interstingly, this code runs in about the 0.50-0.60 sec range. The code it replaced runs in around 0.90 sec.
Code:
Sub f2()

Application.ScreenUpdating = False

    Dim ws As Worksheet
    Set ws = Sheet1
    
    Dim aRng As Range
    Dim bRng As Range
    
    Set aRng = ws.Range("A1").End(xlDown)
    With Range(aRng, aRng.Offset(0, 13))
        .Copy aRng.Offset(1, 0)
        .Value = .Value
        
    
    Set bRng = ws.Range("a1").End(xlDown).Offset(-1, 21)
    With Range(bRng, bRng.Offset(, 8))
        .Copy bRng.Offset(1, 0)
        .Value = .Value


Application.ActivePrinter = "Brother QL-700 on Ne05:"
    With Sheet3
        .Visible = True
        .PrintOut Copies:=1
        .Visible = False
Application.ActivePrinter = "FX DocuPrint P205 b on Ne03:"

Dim ws2 As Worksheet
    Set ws2 = Sheet4
    
    Dim cRng As Range
    Dim dRng As Range
    
    Set cRng = ws2.Range("a60")
    Set dRng = ws2.Range("a58")
    With cRng
        .Cut Destination:=dRng
        
    With ws2
        .PrintOut Copies:=1
    
    Set cRng = ws2.Range("a60")
    Set dRng = ws2.Range("a58")
    With dRng
        .Cut Destination:=cRng
        
    Set cRng = ws2.Range("d60")
    Set dRng = ws2.Range("d58")
    With cRng
        .Cut Destination:=dRng
        
    With ws
        .PrintOut Copies:=1
    
    Set cRng = ws2.Range("d60")
    Set dRng = ws2.Range("d58")
    With dRng
        .Cut Destination:=cRng
        
    Set cRng = ws2.Range("h60")
    Set dRng = ws2.Range("h58")
    With cRng
        .Cut Destination:=dRng
        
    With ws
        .PrintOut Copies:=1
        
    
    Set cRng = ws2.Range("h60")
    Set dRng = ws2.Range("h58")
    With dRng
        .Cut Destination:=cRng


Dim ws3 As Worksheet
    Set ws3 = Sheet5
    
    Dim eRng As Range
    Dim fRng As Range
    Dim gRng As Range
    Dim hRng As Range
    Dim iRng As Range
    
    Set eRng = ws3.Range("b55")
    Set fRng = ws3.Range("b56")
    Set gRng = ws3.Range("b57")
    Set hRng = ws3.Range("b58")
    Set iRng = ws3.Range("b43")
    Set jRng = ws3.Range("B40:B42,B44:B45,B50:B54,B59:B61")
    
    With eRng
        .Value = Sheet6.Range("o2")
        
    With fRng
        .Value = Sheet6.Range("p2")
        
    With gRng
        .Value = Sheet6.Range("q2")
        
    With hRng
        .Value = Sheet6.Range("r2")
        
    With iRng
        .Formula = "=TODAY()"
        
    With jRng
        .ClearContents
        
 
        
    End With
    End With
    End With
    End With
    End With
    End With
    End With
    End With
    End With
    End With
    End With
    End With
    End With
    End With
    End With
    End With
    End With
    'End With
    Application.ScreenUpdating = True
   
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
You could get rid of a lot of the Withs.
 
Upvote 0

Forum statistics

Threads
1,214,617
Messages
6,120,541
Members
448,970
Latest member
kennimack

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top