VBA初学者です、リストボックスにシートから値を代入するのですが、始めはアドアイテムを使っていたのですが、文字数の関係で配列に変更したのですが、 アドアイテムの時のフォーマット関数を配列の時にどの様に指定すれば良いのかわかりません、ご教授くださいましたら幸いです Private Sub UserForm_Initialize() Dim i As Long Dim j As Long Dim lastrow As Long Dim myPath As String Dim myFileName As String Dim ws1 As Worksheet Dim mydata As Variant Dim mydata2() As Variant myPath = "C:\Users\**\Desktop\見積資料\" ' myPath = "★★★" ★★★にファイル場所を記入 myFileName = Dir(myPath & "*** オプションリスト.xlsm") '(myPath & "*.xlsx") *にファイル名 Application.DisplayAlerts = False Workbooks.Open myPath & myFileName, ReadOnly:=True Set ws1 = Workbooks(myFileName).Worksheets(1) lastrow = ws1.Cells(Rows.Count, 2).End(xlUp).Row mydata = ws1.Range(ws1.Cells(1, 1), ws1.Cells(lastrow, 10)).Value ReDim mydata2(1 To lastrow, 1 To 6) For i = LBound(mydata) To UBound(mydata) mydata2(i, 1) = mydata(i, 2) mydata2(i, 2) = mydata(i, 3) mydata2(i, 3) = mydata(i, 5) mydata2(i, 4) = mydata(i, 6) mydata2(i, 5) = mydata(i, 8) mydata2(i, 6) = mydata(i, 10) Next i With ListBox1 .ColumnCount = 6 .ColumnWidths = "120;300;20;50;50;10" .List = mydata2 ※以下コメントアウトはアドアイテムの時 ' For i = 2 To 51 ' .AddItem "" ' .List(.ListCount - 1, 0) = ws1.Cells(i, "B").Value =元データーのセル ' .List(.ListCount - 1, 1) = ws1.Cells(i, "C").Value ' ' .List(.ListCount - 1, 2) = ws1.Cells(i, "E").Value ' ' .List(.ListCount - 1, 3) = Format(ws1.Cells(i, "F").Value, "#,###") ' ' .List(.ListCount - 1, 4) = Format(ws1.Cells(i, "H").Value, "#,###") ' ' .List(.ListCount - 1, 5) = Format(ws1.Cells(i, "J").Value, "Percent") ' Next i End With Application.DisplayAlerts = True Workbooks(myFileName).Close savechanges:=False End Sub
Visual Basic