Text only
It is not necessary to format if you only want to import the text to Listview, as it is already formatted correctly in the spreadsheet.
A test was performed with the following data:
And the code of this answer:
Private Sub PopularListView()
'Declaração de variáveis
Dim wksOrigem As Worksheet
Dim rData As Range
Dim rCell As Range
Dim LstItem As ListItem
Dim linCont As Long
Dim colCont As Long
Dim i As Long
Dim j As Long
'Definição da planilha de origem
Set wksOrigem = Worksheets("Planilha1")
'Definição do range de origem
Set rData = wksOrigem.Range("A2").CurrentRegion
'Adicionar cabeçalho no listview com laço de repetição 'For'
For Each rCell In rData.Rows(1).Cells
Me.ListView1.ColumnHeaders.Add Text:=rCell.Value, Width:=90
Next rCell
'Alimentar variável linCont com número de linhas do intervalo fonte
linCont = rData.Rows.Count
'Alimentar variável colCont com número de linhas do intervalo fonte
colCont = rData.Columns.Count
'Popular o ListView
For i = 2 To linCont
Set LstItem = Me.ListView1.ListItems.Add(Text:=rData(i, 1).Text)
For j = 2 To colCont
LstItem.ListSubItems.Add Text:=rData(i, j).Text
Next j
Next i
End Sub
Private Sub UserForm_Initialize()
'Ajustes de algumas propriedades importantes do ListView
With Me.ListView1
.Gridlines = True 'linhas de grade visíveis (true)
.HideColumnHeaders = False 'False para cabeçalho visível e True para invisível
.View = lvwReport 'Ajustar visualização do listview
End With
'se quiser detalhes da propriedade .view consulte: https://msdn.microsoft.com/en-us/library/aa733652(v=vs.60).aspx)
'Chamar o procedimento para popular o Listview1
Call PopularListView
End Sub
The result is as follows:
Time value and format
You already want to save the time value as a variable to perform calculations, declare a time variable and assign the cell value (hora = rData(i, 2).Value
).
Then format to the desired format when inserting into Listview:LstItem.ListSubItems.Add Text:=Format(hora, "hh:mm:ss")
Private Sub PopularListView()
'Declaração de variáveis
Dim wksOrigem As Worksheet
Dim rData As Range
Dim rCell As Range
Dim LstItem As ListItem
Dim linCont As Long
Dim colCont As Long
Dim i As Long
Dim j As Long
Dim hora As Double
'Definição da planilha de origem
Set wksOrigem = Worksheets("Planilha1")
'Definição do range de origem
Set rData = wksOrigem.Range("A2").CurrentRegion
'Adicionar cabeçalho no listview com laço de repetição 'For'
For Each rCell In rData.Rows(1).Cells
Me.ListView1.ColumnHeaders.Add Text:=rCell.Value, Width:=90
Next rCell
'Alimentar variável linCont com número de linhas do intervalo fonte
linCont = rData.Rows.Count
'Alimentar variável colCont com número de linhas do intervalo fonte
colCont = rData.Columns.Count
'Popular o ListView
For i = 2 To linCont
hora = rData(i, 2).Value
Set LstItem = Me.ListView1.ListItems.Add(Text:=rData(i, 1).Text)
LstItem.ListSubItems.Add Text:=Format(hora, "hh:mm:ss")
Next i
End Sub
Private Sub UserForm_Initialize()
'Ajustes de algumas propriedades importantes do ListView
With Me.ListView1
.Gridlines = True 'linhas de grade visíveis (true)
.HideColumnHeaders = False 'False para cabeçalho visível e True para invisível
.View = lvwReport 'Ajustar visualização do listview
End With
'se quiser detalhes da propriedade .view consulte: https://msdn.microsoft.com/en-us/library/aa733652(v=vs.60).aspx)
'Chamar o procedimento para popular o Listview1
Call PopularListView
End Sub
Opa Daniel! Dear thanks so much for your help. I’m actually already able to pull the data to listview, the problem is that I don’t know how to treat the time. The thing is that when it comes to pulling the data from the spreadsheet to listview my code is different from yours, and I don’t know how to make it work with your solution, it’s not working. I wonder if you could take a look at mine code so you can help me?
– Sena
See the part of the answer that has formatting
LstItem.ListSubItems.Add Text:=Format(hora, "hh:mm:ss")
, where Format() is used. Or it could be as followsLstItem.ListSubItems.Add Text:=Format(rData(i, 2).Value, "hh:mm:ss")
. In your code you should format the cell with the data, then it could change fromPlanilha3.Range("b" & i)
forPlanilha3.Range("b" & i).Text
. Or format withFormat(Planilha3.Range("b" & i), "hh:mm:ss")
. Where column B is an example, but should be changed to the column with the time data.– danieltakeshi
Hahaha, man it worked. Thank you very much. I’m a complete Noob with Visual, I didn’t even imagine I could do it. All I had to do was put one
Planilha3.Range("b" & i).Text
. Thank you for your patience, all the best!– Sena