Monday, April 12, 2010

Scrolling about a chosen worksheet.

Here's a very simple line of code that will stop users from scrolling about a chosen worksheet. To place the code in, right click on the sheet name tab, select "View Code" and paste in this code.

Private Sub Worksheet_Activate()

Me.ScrollArea = "A1:L20"

'To set back to normal use:

Me.ScrollArea = ""

End Sub

Making sure the Combobox selection is part of the list:

Private Sub ComboBox1_Change()

If ComboBox1.ListIndex >= 0 Then

'Your code here

End If

End Sub

Stop a user from closing a UserForm via the X:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

If CloseMode = 0 Then

Cancel = True

MsgBox "Please use the Cancel button", vbCritical

End If

End Sub

When coding with a UserForm use the keyword Me.

Private Sub UserForm_Activate()

MsgBox Me.Name

End Sub

When coding with the Worksheet events use the keyword Me.

Private Sub Worksheet_Activate()

MsgBox Me.CodeName

End Sub

When coding with the Workbook events use the keyword Me.

Private Sub Workbook_Open()

MsgBox Me.FullName

End Sub

Prevent endless loops within events:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
On Error GoTo ResetEvents
Application.EnableEvents = False
'Your code here.
Application.EnableEvents = True

Exit Sub
ResetEvents:
Application.EnableEvents = True
End Sub