Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 20th, 2006, 01:13 PM
Authorized User
 
Join Date: Jan 2005
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default Named Range and Combobox

I have built a combobox and filled it with all the named ranges within a specific Worksheet.
  However, I want the named Ranges in the combobox list to be sorted alphabetically, each time a new named range is added.
   Is there anybody out there who can assist with the codes on how to arrange the named ranges in alphabetical order?

 
Old November 20th, 2006, 01:23 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 173
Thanks: 0
Thanked 3 Times in 3 Posts
Default

1) Use the Workbook_SheetChange event to trigger your code
2) Write a loop through all names in ThisWorkbook.Names (NB you might also need to loop through all Sheets and all anems within each WorkSheet); record the names of the range names and whatever else to an array.
3) Use the Quick Sort alogorthim to sort the array alphbetically.
4) Re-populate the ComboBox with the new sorted array.

There's a fair bit of coding in there but it shouldn't be too difficult.

Maccas

 
Old November 20th, 2006, 08:03 PM
Authorized User
 
Join Date: Jan 2005
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks ,Maccas, for your prompt reply. I shall be trying out your suggestions. By the way ,what is Quick Sort Algorithim? Is there any lead or reference where I can learn something about this.
 Really appreciate your asistance.
 
Old November 21st, 2006, 04:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 173
Thanks: 0
Thanked 3 Times in 3 Posts
Default

QuickSort is a pretty standard and elegant sorting algorithm, you can find reference to it in all decent numerical methods books and pretty easily on Google.

Since its so useful I thought I'd post an example of the QuickSort algorithm being implemented in VBA so that you can dump the code into a separate module and not worry too much as to what its doing. NB I only just knocked this together. I've tested this out with a bunch of random numbers and its appears to work ok but give me a shout if its not working properly and I'll have a look into it.

Maccas

Code:
Option Explicit

Public Sub QuickSort(ByRef A() As Variant, ByVal Lb As Long, ByVal Ub As Long)

Dim m As Long

    ' Sort array A(lb..ub)
    Do While Lb < Ub

        ' Quickly sort short lists
        If (Ub - Lb <= 12) Then
            Call InsertSort(A, Lb, Ub)
            Exit Sub
        End If

        ' Partition into two segments
        m = Partition(A, Lb, Ub)

        ' Sort the smallest partition to minimize stack requirements
        If m - Lb <= Ub - m Then
            Call QuickSort(A, Lb, m - 1)
            Lb = m + 1
        Else
            Call QuickSort(A, m + 1, Ub)
            Ub = m - 1
        End If

    Loop

End Sub

Private Function Partition(ByRef A() As Variant, ByVal Lb As Long, ByVal Ub As Long) As Long

Dim t As Variant
Dim pivot As Variant
Dim i As Long
Dim j As Long
Dim p As Long

    ' Partition array[lb..ub]

    ' Select pivot and exchange with 1st element
    p = Lb + (Ub - Lb) \ 2
    pivot = A(p)
    A(p) = A(Lb)

    ' Sort Lb+1 .. Ub based on pivot
    i = Lb
    j = Ub + 1
    Do

        Do
            j = j - 1
        Loop While j > i And A(j) > pivot

        Do
            i = i + 1
        Loop While i < j And A(i) < pivot

        ' Only way out of do loop
        If i >= j Then Exit Do

        ' Swap A(i), A(j)
        t = A(i)
        A(i) = A(j)
        A(j) = t

    Loop While True

    ' Pivot belongs in A(j)
    A(Lb) = A(j)
    A(j) = pivot
    Partition = j

End Function

Private Sub InsertSort(ByRef A() As Variant, ByVal Lb As Long, ByVal Ub As Long)

Dim t As Variant
Dim i As Long
Dim j As Long

    ' Sort A[Lb..Ub]
    For i = Lb + 1 To Ub

        t = A(i)

        ' Shift elements down until insertion point found
        For j = i - 1 To Lb Step -1
            If A(j) <= t Then Exit For
            A(j + 1) = A(j)
        Next j

        ' Insert
        A(j + 1) = t

    Next i

End Sub
 
Old November 22nd, 2006, 09:27 AM
Authorized User
 
Join Date: Jan 2005
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's nice of you to create this example.But I have still a long way to learn from a scratch. Yes , there are some sites on this subject. I hope to grasp its methodology.
As advised by you, I pasted your codes into a separate Module, and tried running a bunch of random numbers on sheet1, but does not activate anything.
  I believe I must do more reading about this stuff. Meantime, I am posting a new topic on "sendmail" problem.
 
Old November 23rd, 2006, 01:16 PM
Authorized User
 
Join Date: Nov 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

nice code, 2 of the functions/subs are private may need to change to public

cheers
scott





Similar Threads
Thread Thread Starter Forum Replies Last Post
Combobox List Fill Range jani Access VBA 1 July 29th, 2008 05:04 PM
named anchors stekker XSLT 1 November 16th, 2006 05:03 AM
ComboBox & Named Range bahachin Excel VBA 2 June 13th, 2006 12:59 PM
Named Pipes bpe_xmind Visual C++ 0 February 5th, 2006 08:21 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.