質問するログイン新規登録

質問編集履歴

1

ソースコードを記入

2023/11/21 02:45

投稿

Crucian_carp
Crucian_carp

スコア11

title CHANGED
File without changes
body CHANGED
@@ -13,12 +13,98 @@
13
13
 
14
14
  ### 該当のソースコード
15
15
 
16
+ Option Compare Database
16
- ```Excel VBA
17
+ Option Explicit
17
- Range(CellAddress_).CurrentRegion.Select
18
- Selection.Offset(OffsetNumber_, 0).Resize(Selection.Rows.Count - ResizeNumber_).Select
19
18
 
19
+ Dim Con As Object
20
+ Dim FolderPath As String
21
+ Dim ExcelApp As Object
22
+ Dim FilePath As String
23
+ Dim ExcelWorkbook As Object
24
+ Dim ExcelWorksheet As Object
20
- CellsAddress_、OffsetNumber_は変数
25
+ Dim CellsRange As Variant
26
+ Dim RecordExist As Boolean
27
+ Dim ShipmentDate As Date
21
28
 
29
+ Dim i As Long
30
+
31
+ Sub ImportDataFromExcel()
32
+
33
+ FolderPath = Application.CurrentProject.Path & "\"
34
+
35
+ Set ExcelApp = CreateObject("Excel.Application")
36
+
37
+ FilePath = Dir(FolderPath & "*.xls")
38
+
39
+ Do While FilePath <> ""
40
+
41
+ Set ExcelWorkbook = ExcelApp.workbooks.Open(FolderPath & FilePath, , True)
42
+
43
+ Set ExcelWorksheet = ExcelWorkbook.Sheets("List")
44
+
45
+ ShipmentDate = ExcelWorksheet.range("G4").Value
46
+ CellsRange = ExcelWorksheet.range("B9:G32")
47
+
48
+ Dim db As DAO.Database
49
+ Set db = CurrentDb()
50
+
51
+ Dim rs As DAO.Recordset
52
+ Set rs = db.OpenRecordset("SELECT * FROM A_TBL")
53
+
54
+ RecordExist = False
55
+
56
+ For i = LBound(CellsRange, 1) To UBound(CellsRange, 1)
57
+
58
+ Do Until rs.EOF
59
+
60
+ If rs.Fields("ID").Value = CellsRange(i, 5) Then
61
+
62
+ RecordExist = True
63
+
64
+ Exit Do
65
+
66
+ End If
67
+
68
+ rs.MoveNext
69
+
70
+ Loop
71
+
72
+ If Not RecordExist Then
73
+
74
+ rs.AddNew
75
+
76
+ rs.Fields("PART_NUMBER").Value = CellsRange(i, 2)
77
+ rs.Fields("ID").Value = CellsRange(i, 5)
78
+ rs.Fields("DELIVERY_DATE").Value = ShipmentDate
79
+
80
+ rs.Update
81
+
82
+ End If
83
+
84
+ Next
85
+
86
+ rs.Close
87
+
88
+ Set rs = Nothing
89
+
90
+ db.Close
91
+
92
+ Set db = Nothing
93
+
94
+ ExcelWorkbook.Close False
95
+
96
+ FilePath = Dir()
97
+
98
+ Loop
99
+
100
+ MsgBox "処理が完了しました"
101
+
102
+ ExcelApp.Quit
103
+
104
+ Set ExcelApp = Nothing
105
+
106
+ End Sub
107
+
22
108
  ```
23
109
 
24
110
  ### 試したこと