PC Review


Reply
Thread Tools Rate Thread

How to align decimals in a Listbox

 
 
NoSpam@aol.com
Guest
Posts: n/a
 
      19th Apr 2008
I would like to have a column in a list box that contains numbers with
decimals and have the decimals aligned. Another column has text that
should be left aligned. Is there ANY way to do this.

A similar question: Can I apply the format of the cells in the worksheet to
the "same" "cells" in the list box? (If the contents of cell A6 is
displayed in a listbox, I would like the format of cell A6 to be used.)

Many thanks.
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      19th Apr 2008
Can you use a non-proportional font--like Courier New?

If yes...

I created a small userform with a listbox and a couple of command buttons in
it. And used a two column range and formatted the second column by specifying a
numberformat and adding some leading spaces.

Option Explicit
Private Sub CommandButton1_Click()
Dim iCtr As Long
With Me.ListBox1
For iCtr = 0 To .ListCount - 1
If .Selected(iCtr) Then
MsgBox .List(iCtr, 0) & "--" & CDbl(.List(iCtr, 1))
End If
Next iCtr
End With
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
Dim myRng As Range
Dim myCell As Range

With Worksheets("Sheet1")
Set myRng = .Range("a1:b10")
End With

With Me.ListBox1
.MultiSelect = fmMultiSelectMulti
.ColumnCount = myRng.Columns.Count

.Font = "Courier New"
For Each myCell In myRng.Columns(1).Cells
.AddItem myCell.Value
.List(.ListCount - 1, 1) = NicelySpaced(myCell.Offset(0, 1).Value)
Next myCell
End With

With Me.CommandButton1
.Caption = "Ok"
.Default = True
End With

With Me.CommandButton2
.Caption = "Cancel"
.Cancel = True
End With

End Sub
Function NicelySpaced(myVal As Double) As String
Dim myStr As String
Dim HowWide As Long

HowWide = 15
myStr = Format(myVal, "#,###.####")
myStr = Right(Space(HowWide) & myStr, HowWide)
NicelySpaced = myStr
End Function



(E-Mail Removed) wrote:
>
> I would like to have a column in a list box that contains numbers with
> decimals and have the decimals aligned. Another column has text that
> should be left aligned. Is there ANY way to do this.
>
> A similar question: Can I apply the format of the cells in the worksheet to
> the "same" "cells" in the list box? (If the contents of cell A6 is
> displayed in a listbox, I would like the format of cell A6 to be used.)
>
> Many thanks.


--

Dave Peterson
 
Reply With Quote
 
Martin Fishlock
Guest
Posts: n/a
 
      19th Apr 2008
1. You need to format the numbers into a string and then put them onto to the
list box. ie:

s = Format(1.22223, "#,##0.00")
You may then need to pad the left had side and there are always problems
with bigger numbers.

For the text you need to pad it with spaces. (ie check the length with len()
and then add spaces in the right hand side.


2. As seen in the format function you can use the same formats as the cells.

--
Hope this helps
Martin Fishlock, www.nyfconsultants.com, Bangkok, Thailand
Please do not forget to rate this reply.


"(E-Mail Removed)" wrote:

> I would like to have a column in a list box that contains numbers with
> decimals and have the decimals aligned. Another column has text that
> should be left aligned. Is there ANY way to do this.
>
> A similar question: Can I apply the format of the cells in the worksheet to
> the "same" "cells" in the list box? (If the contents of cell A6 is
> displayed in a listbox, I would like the format of cell A6 to be used.)
>
> Many thanks.
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How To Align Decimals In A PP 2007 Table WSR Microsoft Powerpoint 3 27th Apr 2010 02:03 PM
How do I align decimals in Excel when not all numbers have % sym. =?Utf-8?B?c2pnaWxlcw==?= Microsoft Excel Misc 2 7th Sep 2006 10:36 PM
Accounting format does not align decimals. =?Utf-8?B?QWNjb3VudGluZyBBbGlnbm1lbnQ=?= Microsoft Excel Misc 1 7th Jul 2006 08:42 PM
How do I align numbers by their decimals in a word table? =?Utf-8?B?Qks=?= Microsoft Word Document Management 5 11th Oct 2005 08:53 PM
Align decimals in text boxes? Kate Microsoft Access Reports 6 5th Jul 2005 06:39 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:43 AM.