how to format decimal in list box?

Ms.Sigma

New Member
Joined
Mar 1, 2011
Messages
35
I need to display correlation matrix in the list box. All the correlation coefficients have infinite decimals. How to display the precision of 4 for the decimal?

Another question is: how to display 0.55 rather than .55? ;)
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Where is the data coming from?
 
Upvote 0
There are several columns in one worksheet. I wrote macros to calculate correlation matrix of these columns and make the result to be displayed in the list box of user form.
 
Upvote 0
You haven't said how you are populating the listbox - in code, using .List, .AddItem or .Rowsource?
 
Upvote 0
Here is the macros for list box:

Private Sub ShowCorrelation_Click()

Dim series As Range
Dim correlation As Variant
Set series = Range(RefEdit1.Text)
correlation = CorrelationMatrix(series)

With ListBox1
.Clear
.Font.Size = 10
.List() = correlation

End With

End Sub

My macros for CorrelationMatrix is:

Function CorrelationMatrix(rng As Variant) As Variant
' Returns correlation matrix of a range
Dim i As Integer, j As Integer, K As Integer, ncols As Integer, nrows As Integer
Dim r1vector() As Variant
Dim r2vector() As Variant
Dim Cmatrix() As Variant
ncols = rng.Columns.Count
ReDim Cmatrix(ncols, ncols)
nrows = rng.Rows.Count
ReDim r1vector(nrows)
ReDim r2vector(nrows)
For i = 1 To ncols
For K = 1 To nrows
r1vector(K) = rng(K, i)
Next K
Cmatrix(i, i) = 1
For j = i + 1 To ncols
For K = 1 To nrows
r2vector(K) = rng(K, j)
Next K

Cmatrix(i, j) = Application.WorksheetFunction.Correl(r1vector, r2vector)
Cmatrix(j, i) = Cmatrix(i, j)
Next j
Next i

CorrelationMatrix = Cmatrix

End Function
:)


You haven't said how you are populating the listbox - in code, using .List, .AddItem or .Rowsource?
 
Upvote 0
Change this:
Code:
Cmatrix(i, j) = Application.WorksheetFunction.Correl(r1vector, r2vector)
to:
Code:
Cmatrix(i, j) = Format$(Application.WorksheetFunction.Correl(r1vector, r2vector),"0.0000")
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,256
Members
452,901
Latest member
LisaGo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top