Can this VBA code be written better?

kalim

Board Regular
Joined
Nov 17, 2010
Messages
87
Hi excel users.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
I was wondering if anyone could look at this VBA code to see if it can be done more efficiently (not good with VBA) Thanks. In case you are wondering "b2" has a value that needs to be cleared first.

Code:
Sub list_1()
 
Sheet1.Activate
 
    Range("B2").ClearContents
    Range("f2:f41").Select
    Selection.Copy
    Range("A2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        Range("A42:A53").Select
    Selection.ClearContents
    Sheet3.Select
    Range("B1").Select
 
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi,

You can tidy it up by getting rid of the unnecessary Active/Activate/Select/Selection bits.
Code:
Sub list_1()
 
    Sheet1.Range("B2").ClearContents
    
    With Sheet1.Range("f2:f41")
        Sheet1.Range("A2").Resize(.Rows.Count, .Columns.Count).Value = .Value
    End With
    
    Sheet1.Range("A42:A53").ClearContents
    
End Sub
 
Upvote 0
Hi. Try this

Code:
Sub list_1()
 
Sheet1.Activate
 
    Range("B2").ClearContents
    Range("f2:f41").Copy
    Range("A2").PasteSpecial Paste:=xlPasteValues
    Range("A42:A53").ClearContents
    Sheet3.Select
    Range("B1").Select
 
End Sub
 
Upvote 0
Code:
With Sheet1
    .[B2].ClearContents
    .[A2:A31] = .[F2:F31].Value
    .[A42:A53].ClearContents
End With
Sheet3.Select
[B1].Select
 
Upvote 0
This suffices:
Code:
With Sheet1
  .[A2:A31] = .[F2:F31].Value
  .[B2,A42:A53].ClearContents
End With
 
Upvote 0
Using the Range property is better than using Evaluate []. It benefits from early binding, thus you get intellisense and it is more efficient. It also (IMO) makes the code clearer to read.
 
Upvote 0
Certainly just my opinion, but Evaluate is not what I would use for this. Regardless of how handy the help topic makes is sound, Evaluate has to, well, evaluate what it's supposed to be doing and return an object or a value. I would suggest similar to Colin's.
Rich (BB code):
Sub list_1()
    
    With Sheet1
        .Range("B2").ClearContents
        .Range("A2:A41").Value = .Range("f2:f41").Value
        .Range("A42:A53").ClearContents
    End With
    Application.Goto Sheet3.Range("B1").Select
    
End Sub

This way you maintain good explicit references, and keep intellisense helping you along the way.

Mark
 
Upvote 0
For what it's worth, depending on what you mean by better (Easier to read? Faster execution? Less code?), a tiny modification to Mark's suggestion (by no means am I trying to be pedantic, apologies in advance!) might be:
Rich (BB code):
Sub list_1()
    
    With Sheet1
        .Range("A42:A53,B2").ClearContents
        .Range("A2:A41").Value = .Range("F2:F41").Value
    End With
    Application.Goto Sheet3.Range("B1").Select
    
End Sub

Ok, it's only reducing the number of lines of code by 1, but it really depends on what you mean by "better" (IMO)

Jack
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,551
Members
452,927
Latest member
rows and columns

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