電子書籍の厳選無料作品が豊富!

Webで色々調べ試行錯誤しながらVBAでUserFormを作成し、Text-BoxにファイルをD&Dして
表示まで出来るようにはなったのですが、これ以降が中々進まず時間も無くなってきたので
皆さんのお知恵をお借りしたく質問させて頂きました。

①Excel-VBAよりUserFormでText-Boxを作成。
②ファイルをD&Dし、Text-Boxへの表示までどうにかこぎつけた。
③UserFormの【次へ】ボタンをクリック後、D&Dした全てのファイル名を
 Excel-Sheet1のB9から下方向(B10、B11・・・)に1ファイルづつ、
 値の入力をする。
 例)
  B10セル値:***/***/a.pdf
  B11セル値:***/***/b.xlsm

①②までは変なことをしているかも知れませんがどうにか作成出来たのですが、
③が出来ていないのです。
ちなみに①②は下記になっています。
---------------------------------------------------------------------
Private Sub UserForm_Initialize()

With Me.ListView1
.View = lvwReport
.LabelEdit = lvwAutomatic
.AllowColumnReorder = True
.FullRowSelect = True
.Gridlines = True
.MultiSelect = True
.OLEDragMode = ccOLEDragAutomatic
.OLEDropMode = ccOLEDropManual

.ColumnHeaders.Add , "key1", "File Name", 150, lvwColumnLeft
.ColumnHeaders.Add , "key2", "File Path", 400, lvwColumnLeft
End With
End Sub


Private Sub ListView1_OLEDragDrop(Data As MSComctlLib.DataObject, _
Effect As Long, Button As Integer, _
Shift As Integer, x As Single, y As Single)
Dim filePath As String
Dim fileName As String
Dim fileCount As Long
Dim indexFile As Long

fileCount = Data.Files.Count

For indexFile = 1 To fileCount
filePath = Data.Files(indexFile)
fileName = Dir(filePath)
With Me.ListView1.ListItems.Add
.Text = fileName
.SubItems(1) = filePath
End With
End Sub
---------------------------------------------------------------------

目的を達成するにはどのように記述すれば良いでしょうか?
ご教授頂けると助かります。

以上、よろしくお願い致します。

「[EXCEL-VBA]UsrFormより」の質問画像

A 回答 (2件)

#1です


取得のfilePathだけで良いようですね。
コードも整理してみました。
Private Sub CommandButton1_Click()
 Dim i As Long
 With Me.ListView1.ListItems
   If .Count <> 0 Then
     For i = 1 To .Count  'アイテム数
       'ListViewの 2列目は、SubItems(1)
       Sheets("Sheet1").Cells(i + 8, "B").Value = _
           .Item(i).SubItems(1)
     Next i
   Else
'添付ファイルが無い時の処理
'処理が必要ない場合は、IF・Else・End Ifも不要
   End If
 End With
End Sub

ListViewの 1列目は、 .Item(i)
2列目は、.Item(i).SubItems(1) 
3列目は、.Item(i).SubItems(2)
    • good
    • 0
この回答へのお礼

重ねてありがとうございます。
すごく助かりました。

お礼日時:2021/04/12 18:06

こんにちは


こんな感じでしょうか
Private Sub CommandButton1_Click()
Dim i As Long
For i = 1 To ListView1.ListItems.Count 'アイテム数
'ListView1列目:2列目 >> Item(i):SubItems(1)
Sheets("Sheet1").Cells(i + 8, 2).Value = _
ListView1.ListItems.Item(i) & " : " & ListView1.ListItems.Item(i).SubItems(1)
Next i
End Sub
    • good
    • 0
この回答へのお礼

早々に回答頂き、ありがとうございます。
希望していた処理になっていました。

お礼日時:2021/04/12 18:04

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!