前提・実現したいこと
excelにcsvデータを追記で取込むときに、すでに登録されている指定列”G”と”O”のデータと重複チェックを行い
重複がある場合は、エラーリスト(別シート)に出力、重複がない場合はcsv取り込みを進めるようなVBAを書きたいのですが、
どのようにしたらよいか、お知恵をお貸しください。(最近VBAを触り始めました)
該当のソースコード
素人ながらに現状、下記コードを書いています。
Sub cmdBtn_Csv_Import()
Dim myFileName As Variant
Dim Fcn As Long
Dim i As Long
Dim buf As String
Dim tmp As Variant
Dim n, m, h As Long
' csvファイル選択 myFileName = Application.GetOpenFilename("CSVファイル(*.csv),*.csv", _ Title:="CSVファイルの選択") If myFileName = False Then MsgBox "キャンセルされました。" Exit Sub End If ' イベント停止 Application.DisplayAlerts = False Call EventTermination With Worksheets("Sheet1") ' 最終行を取得 n = .Cells(.Rows.Count, 4).End(xlUp).Row If INT_ROW_DATAHEAD_START <= 5 Then End If ' 重複チェック Set ws1 = Worksheets("Sheet1") Workbooks.Open FileName:=myFileName Set ws2 = ActiveWorkbook.Worksheets() For m = 5 To n For h = 2 To 200 If ws2.Range("G", h).Value = ws1.Range("G", m).Value Then MsgBox "重複データがあります" Exit Sub Exit For End If Next Next '書き出し Open myFileName For Input As #1 Line Input #1, buf Do Until EOF(1) Line Input #1, buf Fcn = Fcn + 1 tmp = Split(buf, ",") n = n + 1 ws1.Cells(n, 4).Value = tmp(2) ws1.Cells(n, 5).Value = tmp(3) ws1.Cells(n, 6).Value = tmp(4) ws1.Cells(n, 7).Value = tmp(6) ws1.Cells(n, 8).Value = tmp(7) ws1.Cells(n, 9).Value = tmp(8) ws1.Cells(n, 10).Value = tmp(9) ws1.Cells(n, 11).Value = tmp(10) ws1.Cells(n, 12).Value = tmp(11) ws1.Cells(n, 13).Value = tmp(14) ws1.Cells(n, 14).Value = tmp(12) ws1.Cells(n, 15).Value = tmp(13) Loop Close #1 ' イベント開始 Application.DisplayAlerts = True Call EventRestart ThisWorkbook.Activate MsgBox "処理が完了しました。" ws1.Activate
End With
End Sub
回答2件
あなたの回答
tips
プレビュー