Bear with me, I'm learning Excel VBA as I go so excuse any dodgy code. This one has just stumped me - I'm sure I'm missing something pretty obvious but I just can't see it!
I'm trying to refine my code from an extended IF (which works) to a Select Case with Calls to predefined Macros.
The code below seems to run and do what I want it to do, but then crashes Excel with 'Microsoft Excel has stopped working' when calling the Code or Description Macro's. When calling the Freetype Macro, I get 'Not Enough System Resources to Display Completely'
Main Worksheet Code
Private Sub Worksheet_Change(ByVal Target As Range)
Dim OrderBox As String
OrderBox = Range("E3")
Select Case OrderBox
Case "Order by Description"
Call UnProtect(1234)
Call Description
Call Protect(1234)
Case "Order by Code"
Call UnProtect(1234)
Call Code
Call Protect(1234)
Case "Free Type"
Call UnProtect(1234)
Call Freetype
Call Protect(1234)
End Select
End Sub
And here's my Macros:
Sub Protect(myPassword As String)
ActiveWorkbook.Sheets.Protect
Password = myPassword
ActiveWorkbook.Protect
Password = myPassword
End Sub
Sub UnProtect(myPassword As String)
ActiveWorkbook.ActiveSheet.UnProtect
Password = myPassword
ActiveWorkbook.UnProtect
Password = myPassword
End Sub
Sub Description()
Dim Range1 As Range, Range2 As Range, Range3 As Range
Set Range1 = Range("A18:B23")
Set Range2 = Range("A18:A23")
Set Range3 = Range("B18:B23")
Range1.Locked = False
Range1.Validation.Delete
Range3.Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=indirect(""databydesc[description]"")"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Range2.FormulaR1C1 = "=IFERROR(VLOOKUP(RC[1],DATABYDESC,2,FALSE),"""")"
Range3.ClearContents
Range2.Locked = True
Range("B18").Select
End Sub
Sub Code()
Dim Range1 As Range, Range2 As Range, Range3 As Range
Set Range1 = Range("A18:B23")
Set Range2 = Range("A18:A23")
Set Range3 = Range("B18:B23")
Range1.Locked = False
Range1.Validation.Delete
Range2.Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=indirect(""databycode[code]"")"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Range3.FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-1],DATABYCODE,2,FALSE),"""")"
Range2.ClearContents
Range3.Locked = True
Range("A18").Select
End Sub
Sub Freetype()
Range("A18:B23").Locked = False
Range("A18:B23").Validation.Delete
Range("A18:B23").ClearContents
Range("B18").Select
Range("A18").Select
End Sub
Any suggestions or comments on where I've gone wrong are gratefully appreciated.