Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-22-2023, 01:54 PM
Marrick13 Marrick13 is offline Sort Userform Labels Using Array Data Not Working Windows XP Sort Userform Labels Using Array Data Not Working Office 2010 32bit
Competent Performer
Sort Userform Labels Using Array Data Not Working
 
Join Date: Jun 2006
Posts: 102
Marrick13 will become famous soon enough
Default Sort Userform Labels Using Array Data Not Working

I am trying to arrange 10 label controls on a userform alphabetically (ascending or descending) according to what a user would enter in the captions. The attached form shows the labels and has the code I’ve been working with.

The code loops through the form’s controls, identifies the ‘label’ controls, and enters their captions in an array. This seemed to work well when I was just displaying the results in a msgbox, but when I tried to add code that sorts the array alphabetically so the macro could then change the label captions to display in alpha order, this works the first time the Sort function is run when the form opens, but after that, changing the sort option (ascending to descending or vice versa) in the same userform session skews the results, adds spaces between the array elements, and generally becomes a mess.

I thought this might be due to values of variable or objects carrying over from the previous Sort run, but every time I’ve set them to zero, nothing, or ““, I get the same skewed results.



While preparing this version for the Forum, I was getting an error on the ‘If frmMultiClickWrite.LblLabel2.Caption <> ““ Then frmMultiClickWrite.LblLabel2.Caption = MyArray(2 - 1)’ statement in the ‘Sort’ module. But now it sometimes produces the “An error occurred while loading this document” message, which means I have to shut down Word. This is a new error but with my limited VBA experience, I cannot look at the code and determine why this is happening, or why the resulting msgbox text and label captions worked on the first try and not afterwards, or why I’m not getting the “An error occurred while loading this document” error. Can anyone help me out?

Userform and code are attached.
Attached Files
File Type: docm Sort Userform Labels Using Array Data.docm (36.5 KB, 6 views)
Reply With Quote
  #2  
Old 11-23-2023, 12:17 AM
Guessed's Avatar
Guessed Guessed is offline Sort Userform Labels Using Array Data Not Working Windows 10 Sort Userform Labels Using Array Data Not Working Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

This userform is not how I would do this layout at all. It would make a LOT more sense to do the names as entries in a listbox with a couple of buttons and text fields to allow you to edit any of the list positions. A list in a listbox is already an array so weeding out blanks, editing, sorting and returning the results becomes simple.

Can I ask why you chose buttons and captions for what appears to be fields?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 11-23-2023, 09:43 AM
Marrick13 Marrick13 is offline Sort Userform Labels Using Array Data Not Working Windows XP Sort Userform Labels Using Array Data Not Working Office 2010 32bit
Competent Performer
Sort Userform Labels Using Array Data Not Working
 
Join Date: Jun 2006
Posts: 102
Marrick13 will become famous soon enough
Default

This is part of an AutoText Tool I developed (as an adjunct to Word's autotext feature) that allows one to enter data in any of three lists and choose them to be written to the document. I recently added a toggle function whereby one can select ant two items from any of these lists and click a Toggle button. The macro would then alternate writing those selections to the document just by clicking that button.

However, the toggle is limited to the last two selections -- good if one wants only to alternate writing two pieces of text to a document (such as for a script or screenplay with only two character names to precede their dialog). But as I wanted to be able to click a button to write up to ten pieces of text (or character names) without having to select them from any of the listboxes, I hit upon the idea of setting up a separate form with the ten labels you see, and an Edit and Delete command for each. The idea was to allow one to click an Edit button, which would open an input box, type or paste in the text, and do this for however many pieces of text he wants to have so he can just click the button and the application would write its caption to the document. But I wanted to be able to sort those labels alphabetically (to make the text easier to find while typing and clicking the buttons), and thought that the only way to do that was to put the captions in an array, sort the array, and then have the application apply the ordered captions to the labels (instead of actually moving the labels, which was my first thought). I thought that the alphabetical sorting function would be the hardest to achieve, and I was right. But I do not want a listbox because I already have that in the main application (the three listboxes); for this, I wanted to be able to write up to ten pieces of text to the document by just clicking a button. Imagine a script or interview where one has to repeat several speaker names in a document. If there are more than two, the toggle won't be adequate for this, and having to find and select them from a listbox adds mouse action and time. But if I want to be able to choose from among up to ten pieces of text and continually repeat those pieces of text by just clicking a button, this is what I came up with.

The multiclickwrite form I shared on the forum is only part of this AutoText Tool and I wanted to get it working separately before adding it to the main application.
Reply With Quote
  #4  
Old 11-24-2023, 07:34 PM
gmaxey gmaxey is offline Sort Userform Labels Using Array Data Not Working Windows 10 Sort Userform Labels Using Array Data Not Working Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Code:
Private Sub cmdSort_Click()
Dim lngIndex As Long, lngLblCount As Long
Dim j As Long
Dim oColLbls As New Collection
  ReDim arrCaps(0)
  For Each oCtrl In Controls
    'Filter for specific type
    If TypeName(oCtrl) = "Label" Then
      'Add to collection and index label counter
      oColLbls.Add oCtrl
      lngLblCount = lngLblCount + 1
      If oCtrl.Caption <> "" Then
        'Dimension, add to array and index counter
        ReDim Preserve arrCaps(lngIndex)
        arrCaps(lngIndex) = Trim(oCtrl.Caption)
        lngIndex = lngIndex + 1
      End If
     End If
  Next oCtrl
  For lngIndex = LBound(arrCaps) To UBound(arrCaps) - 1
    For j = lngIndex + 1 To UBound(arrCaps)
      If frmMultiClickWrite.optAscending.Value = True Then
        If arrCaps(lngIndex) > arrCaps(j) Then '>' for ascending
          Temp = arrCaps(j)
          arrCaps(j) = arrCaps(lngIndex)
          arrCaps(lngIndex) = Temp
        End If
      End If
      If frmMultiClickWrite.optDescending.Value = True Then
        If arrCaps(lngIndex) < arrCaps(j) Then '<' for descending
          Temp = arrCaps(j)
          arrCaps(j) = arrCaps(lngIndex)
          arrCaps(lngIndex) = Temp
        End If
      End If
    Next j
  Next lngIndex
  'Clear captions
  For lngIndex = 1 To lngLblCount
    oColLbls(lngIndex).Caption = vbNullString
  Next lngIndex
  'Add sorted captions
  For lngIndex = 0 To UBound(arrCaps)
    oColLbls(lngIndex + 1).Caption = arrCaps(lngIndex)
  Next lngIndex
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 11-25-2023, 08:58 AM
Marrick13 Marrick13 is offline Sort Userform Labels Using Array Data Not Working Windows XP Sort Userform Labels Using Array Data Not Working Office 2010 32bit
Competent Performer
Sort Userform Labels Using Array Data Not Working
 
Join Date: Jun 2006
Posts: 102
Marrick13 will become famous soon enough
Default

Greg,

Your code is wonderful -- just what I wanted. Thanks so much! I never thought of using a collection, or putting controls into it, but then, I'm not a developer, either (and rather poor at constructing arrays, even though they are quite powerful).

Thank you again -- I really appreciate your help!
Reply With Quote
  #6  
Old 11-25-2023, 12:36 PM
gmaxey gmaxey is offline Sort Userform Labels Using Array Data Not Working Windows 10 Sort Userform Labels Using Array Data Not Working Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

You are welcome.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Tags
array, word vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort Userform Labels Using Array Data Not Working VBA help non breaking spaces for array of words not working correctly Shelley Lou Word VBA 2 03-08-2023 03:15 AM
Sort option is not working Francois-Spain Excel 1 07-04-2019 02:01 AM
Sort Userform Labels Using Array Data Not Working Sort option is not working Francois-Spain Excel 7 06-27-2019 11:04 AM
Sort Userform Labels Using Array Data Not Working How do I make a checkbox or dropdown item reference data in that specific section of the data array? dhare Excel Programming 2 02-24-2016 12:36 PM
Powerpoint: adding data to trend lines w/o data labels HaiLe PowerPoint 0 04-11-2011 09:21 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:10 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft