If - then headache

51DegreesNorth

New Member
Joined
Mar 29, 2022
Messages
11
Office Version
  1. 365
Platform
  1. Windows
This piece of code has a problem:

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim Loc_Var As String
    LocVar = "AFR"

'First step: select location

    If Not Intersect(Target, Range("F9")) Is Nothing Then
        Range("A" & Sheets("VBA Data").Range("O35")).Select
        LocVar = "XFD"
    End If
    
    If Not Intersect(Target, Range("F10")) Is Nothing Then
        Range("A" & Sheets("VBA Data").Range("O36")).Select
        LocVar = "BVC"
    End If
        
    If Not Intersect(Target, Range("F11")) Is Nothing Then
        Range("A" & Sheets("VBA Data").Range("O37")).Select
        LocVar = "SDF"
    End If

End Sub

I want LocVar to have the correct value. My code stubbornly refuses to change LocVar to the specified value.
Is it right that the If_Then statement only accepts one line of code and if so, is there a workaround ?

Thank you !
 
I like option explicit. But it runs only on module level, this is sheet level.
Not sure what you mean. Works in a standard module, userform module and a sheet module - which is a module as well.
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top