Share via

How to determine or find out what list separator will VBA use for range validation? (Excel VBA Range.Validation list separator interference with Hyperlinks)

Anonymous
2021-10-05T19:26:29+00:00

Hello,

Description + Reproduction of the problem + Purpose

Microsoft Excel uses different list separators in different conditions while working with Hyperlinks and Validation in Cells. I use European version of Office so my regional list separator is semicolon while VBA default list separator is comma.

While working with Validation and Hyperlinks, list separator behave differently according to order when individual commands are called. To better describe the issue lets use simplified procedures as an example:

Private Sub addLink()
  Range("B1").Hyperlinks.Add Range("B1"), "", "B1", , "Link"
End Sub

Private Sub addValidation()

  With Range("A1").Validation
    .Delete

    .Add xlValidateList, , , "one,two,three;four,five,six"

  End With

End Sub

Sub runOk()

  addValidation

  addLink

End Sub

Sub runWrong()

  addLink

  addValidation

End Sub

If I call "runOk" procedure (first Validation, then Hyperlink), comma as list separator is used so the validation list in Cell is as follows:

one

two

three;four

five

six

If I call "runWrong" procedure (first Hyperlink, then Validation), semicolon as list separator is used so the validation list in Cell is as follows:

one,two,three

four,five,six

Purpose of the solution is meant especially for restoring accidentally deleted Hyperlinks and Validations in Sheet Cells and updating its content (different values in Validation lists, different references in Hyperlinks) on Change / FollowHyperlink events.

Question

I would like to know if there is some elegant solution how to force VBA to use either comma separated list or semicolon separated list to be used in all cases, so no matter the order when I call addLink and addValidation, I get the same resulting validation list in the Cell.

Another acceptable approach would be if there is some Application.International(xlListSeparator) alternative returning separator that will VBA use on runtime in that particular case so I can save it in a variable and use it instead of hard coded comma or semicolon.

Limitations

I cannot make the solution dependant on changing default list separator in system settings or any other settings. Need a VBA programatically solution only.

I would like to avoid using a Worksheet Range for Validation Formula1 argument as well as create a temporary hyperlink to force VBA to use regional list separator. I would like to avoid also any similar solution that works with Cells/Sheets.

Also I would like to avoid approach to keep an eye on correct order (to always Call all Validations before Hyperlinks) in all procedures as it might not be possible in all cases as well as if someone would edit the code it could possibly break the entire functionality because of wrong validation list in Cell.

What I have tried + Specification

With help of internet search as well as debugging the code I have found that the issue appears when Hyperlinks are used before Validations called in one procedure or in sub procedures (i.e. calling Hyperlink action will make subsequent Validation to use regional list separator even if it is in sub sub sub procedure). If procedures are called separately, Hyperlink can be called first and subsequent Validation uses comma as list separator, so some reset of list separator must happen when the entire macro ends.

Since this I had no idea what to try that could help (except things listed in Limitations above) as I didn't find any usable Application.Path/DecimalSeparator alternatives for list separator. Although I might forgot something that I've tried.

Application.UseSystemSeparators doesn't affect list separator.

Tested on Windows 10 with Microsoft 365 64-bit and Windows 7 with Microsoft Office 2010 32-bit - same behavior on both specifications. European version of Windows and Excel with semicolon (;) as regional list separator.

Any advice or idea how to work this out with elegance is very appreciated,

Thanks

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

10 answers

Sort by: Most helpful
  1. Anonymous
    2021-10-07T16:54:55+00:00

    Thank you very much for your effort CDN-Carl,

    I can see we had similar ideas, I also tried something like WasteTime subroutine before I found out it interferences with hyperlinks.

    I also thought about using global variable but resetting of the gVariable in my project would become similar technique as to keep an eye on order in which addLink and addValidation are called and that is not practicable in such big project due to a lot of subroutines, possible different orders and stuff. It could happen that gVariable is reset unintentionally and broken validation list appears.

    However I kept playing with Excel and found another solutions in case global variable cannot be used:

    Solution 1

    Application.Run. I don't know the entire process how exactly it works behind the scene but I found out that calling the procedure with Run command surrounds the procedure outside with its own list separator state, so a default list separator (comma) is used even when executed after hyperlink, instead of regional list separator (semicolon).

    ' Continues procedures from original post with no changes
    
    Sub runWrong_UsingRun1()
    
      Application.Run "addLink"
    
      addValidation
    
    End Sub
    
    Sub runWrong_UsingRun2()
    
      addLink
    
      Application.Run "addValidation"
    
    End Sub
    

    Doesn't matter if Application.Run is used for addLink or addValidation procedure, both variants returns correct result.

    Solution 2

    A little tweak of addValidation procedure. It puts a validation in the Cell, then reads its content back. I have found that reading the content of Formula1 parameter returns semicolons (regional list separator) instead of commas (default list separator). If there is not the right list separator, modify the validation using the right one.

    ' Continues procedures from original post with no changes
    
    Sub addValidation2()
      With Range("A1").Validation
        .Delete
        .Add xlValidateList, Formula1:="one,two,three"
        If InStr(1, .Formula1, ",") > 0 Then .Modify Formula1:=Replace(.Formula1, ",", ";")
      End With
    End Sub
    
    Sub runOk2()
      addValidation2
      addLink
    End Sub
    
    Sub runWrong2()
      addLink
      addValidation2
    End Sub
    

    Both runOk2 and runWrong2 returns the correct result. Procedure runOk2 calls addValidation2 subroutine, puts validation (one,two,three) into Cell, reads it back (one;two;three) and goes to End Sub. When procedure runWrong2 calls addValidation2 subroutine, puts validation (one,two,three) into Cell, reads it back (one,two,three) and the Replace function is executed.

    Drawbacks

    Any idea of significant drawbacks of the solutions?

    Thank you CDN-Carl for your time and help.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2021-10-06T16:36:45+00:00

    Hello Martin

    After changing my Regional settings to French(France) the below screen shot is what I received from runWrong;

    And below is when I run runOK

    This appears to be what you are seeing.

    I will now see if I can find away to solve the issue.

    CDN-Carl

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2021-10-06T14:58:39+00:00

    Thank you for your reply, CDN-Carl.

    I just realized I didn't post my Excel's 32/64-bit versions, so I updated the post.

    We use same specifications, my Excel 365 is also 64-bit as well as Excel 2010 is 32-bit, so this will not be the case.

    Do you use US/UK regional settings of Office? US/UK regions use same regional separator as standard VBA - comma. The issue should appear when different regional separator (i.e. semicolon) is used (most of European countries).

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2021-10-05T22:35:06+00:00

    Hello Martin,

    My Office 2010 32 Bit could not reproduce your issue.

    Again it provide a validation list of;

    one

    two

    three,four

    five

    six

    CDN-Carl

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2021-10-05T22:09:46+00:00

    Hello Martin,

    I have not been able to reproduce your issue. I am using Excel 365

    In each case the validation list is the same;

    one

    two

    three;four

    five

    six

    I will try on my Office 2010 and see how that responds.

    CDN-Carl

    Was this answer helpful?

    0 comments No comments