Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Dim MyString() As String, MyString1() As String
If Len(Selection.Address) > 240 Then
Astr = Selection.Address
MyString = Split(Astr, ",")
For i = 0 To UBound(MyString) - 1
Astr = Replace(MyString(i), "$", "")
MyString1 = Split(Astr, ":")
If MyString1(0) <> MyString1(1) Then
FormSlt.LBrowsSlt.AddItem MyString1(0) & "-" & MyString1(1)
Else
FormSlt.LBrowsSlt.AddItem MyString1(0)
End If
ActiveSheet.Range(MyString(UBound(MyString))).Select
Next i
End If
End Sub
Mike said:In words, what is that code supposed to be doing?
It looks like using the .Areas property of a range object would be a better way to go than splitting the Address at commas.
Also, you can avoid the problem with $ by using .Address(False,False) rathe than .Address.
Is FormSlt.LBrowsSlt a collection? If so, it might be better to use the (inherent) range.Areas collection rather than parsing the sub-address strings.
Dim oneArea as Range
For each oneArea in Range("A1:B4, A5:C8, D6:E5")
MsgBox oneArea.Address
Next oneArea
Ranges can be discontinous like the range a1:c2, d4:e5, a8:c12
An area is a rectangular region. Every range is composed of areas. Try this to see how it works
Code:Dim oneArea as Range For each oneArea in Range("A1:B4, A5:C8, D6:E5") MsgBox oneArea.Address Next oneArea