1

I have some code in a spreadsheet which should change the value in a validaiton list on a worksheet called 'analysis'. The value in cell B1 on the analysis list should change to FTE if the value 'Costs' is already selected, else it should select the value 'costs'.

My code below doesn't seem to be changing the value in the validation list, and when I try to record a macro to get an idea of how a macro recording would record the validation change, the recorder doesn't record the validation change. Does anybody have any idea how to select a value from a validation list via VBA?

My current code is below

Sub ChangeValue()

Worksheets("Analysis").Select
With Worksheets("Analysis")
   If Range("B1").Value = "Costs" Then
      Range("B1").Value = "FTE"
   Else
      Range("B1").Value = "Costs"
   End If
End With

End Sub()
2
  • 2
    There's nothing wrong with that code. Commented Nov 9, 2014 at 23:52
  • This is slightly confusing. Why would you change the value to FTE by clicking on the costs in the same cell? or is the list in some other worksheet? Are the above B cells in different sheets (One being "Analysis")? Commented Nov 10, 2014 at 11:29

1 Answer 1

3

This was a total oversight on my behalf, the code was written as Range rather than .Range. Fix below

Sub ChangeValue()

Worksheets("Analysis").Select
With Worksheets("Analysis")
   If .Range("B1").Value = "Costs" Then
      .Range("B1").Value = "FTE"
   Else
      .Range("B1").Value = "Costs"
   End If
End With

End Sub()
1
  • 2
    I agree with @TimWilliams comment. There's nothing wrong with your code above since you select the worksheet first. However above code is better and also you can remove the Worksheets("Analysis").Select line.
    – L42
    Commented Nov 10, 2014 at 1:41

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.