#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 71
Default VBA code question

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.
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,939
Default VBA code question

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.
--
HTH...

Jim Thomlinson


"Damian" wrote:

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.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
code question Shu of AZ Excel Discussion (Misc queries) 2 July 12th 08 01:10 AM
Code Question FP Novice Excel Discussion (Misc queries) 16 May 19th 08 07:22 PM
question on VB code peyman Excel Discussion (Misc queries) 6 October 22nd 07 11:47 PM
Another code question M&M[_2_] Excel Discussion (Misc queries) 3 August 9th 07 10:00 PM
VBA code question JEV Excel Discussion (Misc queries) 2 March 1st 07 06:02 PM


All times are GMT +1. The time now is 09:04 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"