VBA code question

D

Damian

I have been using the amature way of writing micros, which is RecordMicro.
Then I will got the code, make few changes and it worked fine.

I would like to learn how to write the code a little better, more efficient
and effective. Most of all I like to learn how to get rid of repeted
Selection that you get when using RecordMicro.

Example code to be fixed: (how to make it better)

ActiveSheet.Unprotect Password:="eli"
Range("D:D,B:B").ColumnWidth = 4.57
Range("A:A,E:E").ColumnWidth = 0
Range("B1", Range("Last")).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlDash
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDash
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
EnableSelection = Excel.XlEnableSelection.xlNoSelection
With ActiveSheet
.EnableSelection = xlUnlockedCells
.Protect Password:="eli", _
DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True, AllowFormattingRows:=True
End With

Any help appriciated. Thank you.
 
J

Jim Thomlinson

Here is my take on what it would approximately look like if I wrote it from
scratch...

ActiveSheet.Unprotect Password:="eli"
Range("D:D,B:B").ColumnWidth = 4.57
Range("A:A,E:E").ColumnWidth = 0
With Range("B1", Range("Last"))
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
.Borders(xlEdgeLeft).LineStyle = xlDash
.Borders(xlEdgeTop).LineStyle = xlNone
.Borders(xlEdgeBottom).LineStyle = xlNone
.Borders(xlEdgeRight).LineStyle = xlDash
.Borders(xlInsideVertical).LineStyle = xlNone
End With
EnableSelection = Excel.XlEnableSelection.xlNoSelection
With ActiveSheet
.EnableSelection = xlUnlockedCells
.Protect Password:="eli", _
DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True, AllowFormattingRows:=True
End With


Note that all of the selections are gone. I would also be inclined to remove
the activesheet reference and chane it to referenece a specific sheet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top