# VBAでのファイルのインポート

**URL:** <https://community.cybozu.dev/t/topic/1539>\
**Category:** kintone 開発相談\
**Created:** [2023 年 4 月 19 日午前 6:19 UTC](https://community.cybozu.dev/t/topic/1539 "2023-04-19T06:19:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Legacy\_Account311](https://avatars.discourse-cdn.com/v4/letter/l/f07891/32.png) [@Legacy\_Account311](https://community.cybozu.dev/u/Legacy_Account311)\
**Post date:** [2023 年 4 月 19 日午前 6:19 UTC](https://community.cybozu.dev/t/topic/1539/1 "2023-04-19T06:19:53Z")

</div>

Sub ImportDataToKintone()  
&nbsp; &nbsp; Dim xmlHttpRequest As New MSXML2.ServerXMLHTTP60  
&nbsp; &nbsp; Dim url As String  
&nbsp; &nbsp; Dim payload As String  
&nbsp; &nbsp; Dim lastRow As Long  
&nbsp; &nbsp; Dim i As Long  
&nbsp; &nbsp; Dim deliveryDate As String  
&nbsp; &nbsp; Dim destinationCode As String  
&nbsp; &nbsp; Dim weight As String  
&nbsp; &nbsp; Dim filePath As String  
&nbsp; &nbsp; Dim wb As Workbook  
&nbsp; &nbsp; Dim ws As Worksheet  
&nbsp; &nbsp;&nbsp;  
&nbsp; &nbsp; ’ Set your kintone subdomain, API token and App ID  
&nbsp; &nbsp; Const subDomain As String = “ドメイン”  
&nbsp; &nbsp; Const apiToken As String = “トークン”  
&nbsp; &nbsp; Const appId As String = “ＩＤ”  
&nbsp; &nbsp;&nbsp;  
&nbsp; &nbsp; ’ Set kintone API URL  
&nbsp; &nbsp; url = “https://” & subDomain & “.cybozu.com/k/v1/records.json”  
&nbsp; &nbsp;&nbsp;  
&nbsp; &nbsp; ’ Open the file selection dialog  
&nbsp; &nbsp; With Application.FileDialog(msoFileDialogFilePicker)  
&nbsp; &nbsp; &nbsp; &nbsp; .Title = “Select an Excel file”  
&nbsp; &nbsp; &nbsp; &nbsp; .Filters.Clear  
&nbsp; &nbsp; &nbsp; &nbsp; .Filters.Add “Excel Files”, “\*.xls; \*.xlsx; \*.xlsm”  
&nbsp; &nbsp; &nbsp; &nbsp; If .Show = -1 Then  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filePath = .SelectedItems(1)  
&nbsp; &nbsp; &nbsp; &nbsp; Else  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MsgBox “No file was selected. Exiting…”  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit Sub  
&nbsp; &nbsp; &nbsp; &nbsp; End If  
&nbsp; &nbsp; End With  
&nbsp; &nbsp;&nbsp;  
&nbsp; &nbsp; ’ Open the selected Excel file  
&nbsp; &nbsp; Set wb = Workbooks.Open(filePath)  
&nbsp; &nbsp; Set ws = wb.Worksheets(1)  
&nbsp; &nbsp;&nbsp;  
&nbsp; &nbsp; ’ Find the last row  
&nbsp; &nbsp; lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row  
&nbsp; &nbsp;&nbsp;  
&nbsp; &nbsp; ’ Loop through each row of data  
&nbsp; &nbsp; For i = 2 To lastRow  
&nbsp; &nbsp; &nbsp; &nbsp; ’ Read data from the worksheet  
&nbsp; &nbsp; &nbsp; &nbsp; deliveryDate = Format(ws.Cells(i, 1).Value, “yyyy-MM-dd”)  
&nbsp; &nbsp; &nbsp; &nbsp; destinationCode = ws.Cells(i, 2).Value  
&nbsp; &nbsp; &nbsp; &nbsp; weight = ws.Cells(i, 3).Value  
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;  
&nbsp; &nbsp; &nbsp; &nbsp; ’ Create the JSON payload  
&nbsp; &nbsp; &nbsp; &nbsp; payload = “{”“app”“:”“” & appId & “”“,”“record”“:{”“納品日”“:{”“value”“:”“” & deliveryDate & “”“},”“配送先番号”“:{”“value”“:”“” & destinationCode & “”“},”“重量”“:{”“value”“:”“” & weight & “”“}}}”  
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;  
&nbsp; &nbsp; &nbsp; &nbsp; ’ Set up the HTTP request  
&nbsp; &nbsp; &nbsp; &nbsp; With xmlHttpRequest  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Open “POST”, url, False  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setRequestHeader “Content-Type”, “application/json”  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setRequestHeader “X-Cybozu-API-Token”, apiToken  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .send payload  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ’ Check if the API request was successful  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If .Status \<\> 200 Then  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MsgBox "Error: " & .Status & " " & .statusText & vbCrLf & .responseText  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If  
&nbsp; &nbsp; &nbsp; &nbsp; End With  
&nbsp; &nbsp; Next i  
&nbsp; &nbsp;&nbsp;  
&nbsp; &nbsp; ’ Close the workbook  
&nbsp; &nbsp; wb.Close SaveChanges:=False  
&nbsp; &nbsp;&nbsp;  
&nbsp; &nbsp; MsgBox “Data has been imported to kintone successfully.”  
End Sub

| 納品日 | 配送先番号 | 重量 |  
| 2023/4/22 | あんず | 500 |  
| 2023/4/22 | ポン酢 | 400 |

上記の形式のエクセルファイルを自動でkintoneにインポートしたいと考えております。  
配送先番号のフィールドはルックアップフィールです。

このコードを実行すると、

Error: 400 Bad Request  
fcode” "CB\_VAOT "id"jo2qdZ1edmuZAsIxpbs7,"message”"入力内容が正しくありません。”“errors” 'records’messages”：［必須です。“  
（OK）をクリック

&nbsp;

Error: 400 Bad Request  
fcode””“CB\_VAOT”,id " ”RUM8BMYTiVZPJStYEAlP”,"message””入力内容が正しくありません。,「errors”:「records”f’messages”[必須です。"119

（OK）をクリック

Data has been imported to kintone successfully.  
と最後に表示されます。  
しかし、kintoneの方へはデータが入っていません。

私はＶＢＡもプログラミングもわかりません。  
ChatGPTで作ったコードです。  
ＧＰＴに確認してもエラーは解消されません。

原因がわかる方おられましたら教えていただきたいです。  
よろしくお願いします。

&nbsp;

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2276](https://avatars.discourse-cdn.com/v4/letter/l/cc9497/32.png) [@Legacy\_Account2276](https://community.cybozu.dev/u/Legacy_Account2276)\
**Post date:** [2023 年 4 月 20 日午後 10:02 UTC](https://community.cybozu.dev/t/topic/1539/2 "2023-04-20T22:02:03Z")

</div>

エラーメッセージからアプリにレコードを登録するための必須項目が不足しているようです。  
アプリの設定を見て必須項目を確認し、プログラムでも必須項目を入れるようにしてください。  
そのアプリの設定は管理者しかわからないのでGPTでも解決できるものではありません。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account311](https://avatars.discourse-cdn.com/v4/letter/l/f07891/32.png) [@Legacy\_Account311](https://community.cybozu.dev/u/Legacy_Account311)\
**Post date:** [2023 年 4 月 22 日午前 2:08 UTC](https://community.cybozu.dev/t/topic/1539/3 "2023-04-22T02:08:24Z")

</div>

@INO  
教えて頂きありがとうございます。  
必須項目をすべてなしにして、実行したのですが、  
エラーはでず（Date has been imported to kintone successfully）となるのですが、  
kintoneアプリを確認するとレコードが入っていません。  
原因はなにが考えられるでしょうか？

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex015/uploads/cybozu/original/1X/d5c841c3c5a6cb66eed514001c1b45147c9e1f4f.png) [@system](https://community.cybozu.dev/u/system)\
**Post date:** [2023 年 9 月 27 日午前 2:00 UTC](https://community.cybozu.dev/t/topic/1539/4 "2023-09-27T02:00:53Z")

</div>

このトピックはベストアンサーに選ばれた返信から 3 日が経過したので自動的にクローズされました。新たに返信することはできません。
