Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Speeding up a Data Validation macro

Hi all,

I'm a novice at VBA and I'd appreciate some pointers on how this macro
I've written (well, recorded mostly) could be made to run faster. I'm
reading the defined range D7:CU213 on 84 sheets and looking for cells
which aren't protected (sub totals are protected)to apply some Data
Validation to. At the moment it's taking about 4 minutes per sheet,
which seems a tad slow to me for such a small range.

Anyways, here's the code, any help most appreciated.

-------------------------------------------
Sub DataVal()
' Loops through Range, tests for locked cells, applies Data Validation
to unlocked cells.

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
ActiveSheet.Unprotect

For Each C In ActiveSheet.Range("D7:CU213").Cells
If C.Locked = False Then
C.Select
With Selection.Validation
.Delete
.Add Type:=xlValidateDecimal, AlertStyle:=xlValidAlertStop,
Operator _
:=xlBetween, Formula1:="0", Formula2:="99999"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = "Invalid Input"
.InputMessage = ""
.ErrorMessage = "Input value must be numeric."
.ShowInput = False
.ShowError = True
End With
End If
Next

ActiveSheet.Protect
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub
-------------------------------------------------

Ryan
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Speeding up a Data Validation macro

Ryan,

This should help a little as it avoids cell selection,
eliminates the input message and declares the variable.

Regards,
Jim Cone
San Francisco, USA

'-----------------------
Sub DataVal()
' Loops through Range, tests for locked cells,
' applies Data Validation to unlocked cells.
Dim C As Excel.Range

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
ActiveSheet.Unprotect

For Each C In ActiveSheet.Range("D7:CU213").Cells
If C.Locked = False Then
With C.Validation
.Delete
.Add Type:=xlValidateDecimal, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="0", Formula2:="99999"
.IgnoreBlank = True
.InCellDropdown = True
.ErrorTitle = "Invalid Input"
.ErrorMessage = "Input value must be numeric."
.ShowInput = False
.ShowError = True
End With
End If
Next

ActiveSheet.Protect
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
'--------------------------------


"Ryan" wrote in message
...
Hi all,
I'm a novice at VBA and I'd appreciate some pointers on how this macro
I've written (well, recorded mostly) could be made to run faster. I'm
reading the defined range D7:CU213 on 84 sheets and looking for cells
which aren't protected (sub totals are protected)to apply some Data
Validation to. At the moment it's taking about 4 minutes per sheet,
which seems a tad slow to me for such a small range.
Anyways, here's the code, any help most appreciated.
-------------------------------------------
Sub DataVal()
' Loops through Range, tests for locked cells, applies Data Validation
to unlocked cells.

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
ActiveSheet.Unprotect

For Each C In ActiveSheet.Range("D7:CU213").Cells
If C.Locked = False Then
C.Select
With Selection.Validation
.Delete
.Add Type:=xlValidateDecimal, AlertStyle:=xlValidAlertStop,
Operator _
:=xlBetween, Formula1:="0", Formula2:="99999"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = "Invalid Input"
.InputMessage = ""
.ErrorMessage = "Input value must be numeric."
.ShowInput = False
.ShowError = True
End With
End If
Next

ActiveSheet.Protect
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
-------------------------------------------------
Ryan
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Speeding up a Data Validation macro

Jim Cone wrote:
Ryan,

This should help a little as it avoids cell selection,
eliminates the input message and declares the variable.

Regards,
Jim Cone
San Francisco, USA


Thanks mate. Your method is 22% faster.



cheers
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
Data Validation List Macro Lisa C. Excel Discussion (Misc queries) 18 April 6th 09 12:53 PM
Macro Data Entry Validation Sandy Excel Worksheet Functions 4 April 26th 07 04:12 PM
Data Validation/Macro Problem andyp161[_4_] Excel Programming 1 September 12th 04 06:48 PM
Data Validation and Macro Mr.Z Excel Programming 1 December 3rd 03 08:31 PM
speeding up a macro Brenda[_5_] Excel Programming 4 August 21st 03 12:56 AM


All times are GMT +1. The time now is 12:36 PM.

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"