VBA ド素人 リストに無いものを行ごと削除し貼付したい ・C:\\Users\\user\\Google ドライブ\\銀行関連\\売掛金 内にある、得意先管理.xlsmのシート名:銀行データ のV列に、半角カナ英数の文字がある(図A)※全角や空白セルもある・C:\\Users\\user\\Downloads 内にある、abcから始まるcsv(例:abc202512051825.csv)の同名シートのM列に、半角カナ英数の文字がある(図B)※こちらは全て半角▼やりたいこと① 図Aと図Bに同じ文字がある場合、図Bの文字がある行と1行目を残し、あとは削除したい② 削除後、得意先管理.xlsmのシート名:貼付場所のL・M列に貼り付けたい(図C)★得意先管理.xlsmを開いた状態でマクロを実行したい★csvデータは閉じた状態で、またDownloads内の最新のものを使用したい※必ずabcは付いており、あとに日時がくる※閉じた状態なので、削除とは違うのかも当方VBA素人のため、不十分な説明しかできませんが、皆様のお知恵を拝借ください。

google

1件の回答

回答を書く

1070609

2026-01-11 10:30

+ フォロー

★得意先管理.xlsmを開いた状態でマクロを実行したい

得意先管理.xlsmにマクロを入れて実行します。

★csvデータは閉じた状態で

閉じた状態ではデータのチェックは出来ません。開いて閉じます。

データの削除はしません。



Sub test()

Const fpath As String = \u0026quot;C:\\Users\\user\\Downloads\\\u0026quot;

Dim tbl As Variant

Dim fname As String

Dim no As Variant

Dim sname As String

Dim wb As Workbook

Dim otbl() As Variant

Dim i As Long, j As Long

Dim r As Long

Application.ScreenUpdating = False

'数字部分が最大のファイルを検索

fname = Dir(fpath \u0026amp; \u0026quot;*.csv\u0026quot;, vbNormal)

Do Until fname = \u0026quot;\u0026quot;

If left(fname, 3) = \u0026quot;abc\u0026quot; Then

If Mid(fname, 4, Len(fname) - 7) \u0026gt; no Then

no = Mid(fname, 4, Len(fname) - 7)

sname = fname

End If

End If

fname = Dir()

Loop

'そのCSVデータを配列へ

Set wb = Workbooks.Open(fpath \u0026amp; sname)

With wb.Worksheets(1)

tbl = .Range(\u0026quot;M2:M\u0026quot; \u0026amp; .Cells(Rows.Count, \u0026quot;M\u0026quot;).End(xlUp).Row)

End With

'チェックして残すものを配列へ

i = -1

With ThisWorkbook.Worksheets(\u0026quot;銀行データ\u0026quot;)

For r = 3 To .Cells(Rows.Count, \u0026quot;V\u0026quot;).End(xlUp).Row

For j = 1 To UBound(tbl)

If .Cells(r, \u0026quot;V\u0026quot;).Value \u0026lt;\u0026gt; \u0026quot;\u0026quot; And .Cells(r, \u0026quot;V\u0026quot;).Value = tbl(j, 1) Then

i = i + 1

ReDim Preserve otbl(1, i)

otbl(0, i) = .Cells(r, \u0026quot;A\u0026quot;).Value

otbl(1, i) = .Cells(r, \u0026quot;V\u0026quot;).Value

Exit For

End If

Next j

Next r

End With

wb.Close

With Worksheets(\u0026quot;貼付場所\u0026quot;)

For i = 0 To UBound(otbl, 2)

.Cells(i + 2, 12).Value = otbl(0, i)

.Cells(i + 2, 13).Value = otbl(1, i)

Next i

End With

Application.ScreenUpdating = True

End Sub

うったえる有益だ(0シェアするブックマークする

関連質問

Copyright © 2026 AQ188.com All Rights Reserved.

博識 著作権所有