2

I tried everything which come to my mind to rewrite this code without the selection part.

Worksheets(3).Activate
ActiveSheet.ListObjects(ListName).Range.Select 'Listname is a string-variable
Range(ActiveCell.Offset(1), ActiveCell.Offset(-1)).Select
Selection.Delete Shift:=xlUp
Worksheets(2).Activate
MsgBox "Set-creation was canceled.", vbCritical
Broots Waymb
  • 4,713
  • 3
  • 28
  • 51
Rohfleisch
  • 23
  • 4

1 Answers1

2
Worksheets(3).Activate
ActiveSheet.ListObjects(ListName).Range.Select 'Listname is a string-variable
Range(ActiveCell.Offset(1), ActiveCell.Offset(-1)).Select
Selection.Delete Shift:=xlUp
Worksheets(2).Activate
MsgBox "Set-creation was canceled.", vbCritical

Can be re-written as:


With Sheets(3)
    .Range(.ListObjects(ListName).Range.Offset(1), .ListObjects(ListName).Range.Offset(-1)).Delete xlUp
End With    
Sheets(2).Activate    
MsgBox "Set-creation was canceled.", vbCritical

You don't need to .Select anything in especially when dealing with a Range object - all it's properties and methods can be accessed directly.

Instead of:

Range("A1").Select
Selection.Value = "Test"

Consider:

Range("A1").Value = "Test"

This is better coding and in long procedures will run much faster. The other advantage is that you don't have to keep a track of what the Selection is when debugging or improving the code.

SierraOscar
  • 17,507
  • 6
  • 40
  • 68
  • I already heard of the fact, that there is no activate or select necessary in VBA-Code ever. Im not as deep in the coding as i wish, so i try to improve it. I'll try your code later. Thx at all for the given informations and advices! – Rohfleisch Jan 26 '16 at 09:28