Changing the format of a column inside a 2-D array

RawlinsCross

Active Member
Joined
Sep 9, 2016
Messages
437
Greetings,

I have a 2-D array (variable # of rows, 6 columns), I'm looking to go down the second column and convert a number (between 0 and 1) into a time. Trying a few things but unsuccessful. I can index the column (confirmed through debug.Print) but am incorrectly attempting to apply a format to each item in that second column. As an aside, the array subsequently is placed into a listbox (works but time numbers are in 0.00 format)

Attempt
VBA Code:
Private Sub SummarizeData()

Dim wSht As Worksheet
Dim rRange As Range
Dim tempArray() As Variant
Dim item As Variant

Set wSht = ThisWorkbook.Worksheets("RawData")
Set rRange = wSht.Range("rngData")

tempArray = rRange.Value

For Each item In Application.Index(tempArray, 0, 2)
     item = Format(item, "h:mm AM/PM")
Next item

Me.lbHistory.List = tempArray

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
You're not changing the array, just the variable "item"
How about
VBA Code:
For i = 1 To UBound(tempArray)
     tempArray(i, 2) = Format(tempArray(i, 2), "h:mm AM/PM")
Next i
I would also advise against using VBA keywords (such as item) for the names of variables
 
Upvote 0
You're welcome & thanks for the feedback.

I tend to use "Keywords" loosely, to refer to any word, phrase, string etc that is used by VBA
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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