selectcase statements

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Select...Case Statements

Select...Case Statements

resolvedResolved · Low Priority · Version 2010

Felicity has attended:
Excel Advanced course
Excel VBA Intro Intermediate course
Access Intermediate course

Select...Case Statements

I'm trying to ensure the data in a certain column (H) can't be changed before the data in another (F) has been updated. I started with an If statements but that meant one couldn't update anything in column H at all. I've started to work on a Select Case statement instead, to evaluate whether the user is trying to change the value of a cell in column H, undoing it and changing the active cell to column F of hte same row. However I can't seem to get my head around how to make it so that if one HAS updated column F that changing column H is also possible.

I've got this far:

Public Sub Change_Event(ByVal Target As Range)

Select Case Target.Column

Case Is = Target.Column = 6

Target.Cells.Offset(0, 2).ClearContents

Case Is = Target.Column = 8 And Target.Cells <> 0

MsgBox "Update Date Last Seen Date before changing review date " & Range("AC1").Value, _
vbCritical, "Error Message"

Application.EnableEvents = False

Application.Undo

Application.EnableEvents = True

Target.Offset(0, -2).Select

Case Else

Exit Sub

End Select

End Sub

but nothing happens.

Any help appreciated

Thanks

RE: Select...Case Statements

Hi Felicity,

Thanks for the forum question.

I have changed your code a little bit but it is working. You can only do this with the if then else decision code. Select case can only handle one object at the time.

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Column = 6 Then


Target.Offset(0, 2).ClearContents

ElseIf Target.Column = 8 And Target <> 0 Then


MsgBox "Update Date Last Seen Date before changing review date " & Range("AC1").Value, _
vbCritical, "Error Message"

Application.EnableEvents = False

Application.Undo

Application.EnableEvents = True

Target.Offset(0, -2).Select


End If
End Sub


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Select...Case Statements

Hi Jens,

Thankyou, however I'm still having trouble: that works to stop one changing the contents of the cell in column H and forces you to update the corresponding cell in column F, however, once that cell is updated, it clears the contents of the cell in H but still won't let you change it.

Sorry that sounds really longwinded! I need to be able to change the contents of the cell in H but only AFTER the corresponding cell in F has been changed. The idea is to force the user to complete the spreadsheet correctly i.e. updating the "last seen" date in column F before changing the "next due" date in H - there have been errors in reports because this is not always done. Does that make sense?

Fliss

RE: Select...Case Statements

Hi Felicity,

Sorry the late answer but I haven't been in the office for a week.

It is not straight forward what you want to do, but I found a website, where you can find the code to log changes in a specific range. I hope this can guide you in the right direction.

http://www.mrexcel.com/forum/excel-questions/500810-visual-basic-applications-code-track-changes-workbook.html


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

Wed 10 Sep 2014: Automatically marked as resolved.

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Change the default location for opening and saving spreadsheets

If you are always opening spreadsheets from and/or saving documents to a specific location that is not My Documents, save time by setting this folder as the default for opening files from and saving files to.

Here's how:
1. Go to Tools - Options.

2. Select the General tab.

3. Enter the pathname of the folder you wish to make the default in the Default File Location box (hint: it will be easier to use Windows Explorer to navigate to this folder, then copy and paste the pathname from the address bar at the top of the Windows Explorer screen).

4. Click OK.

You have now changed the default folder for opening and saving spreadsheets.

View all Excel hints and tips


Server loaded in 0.09 secs.