SQLにて取り出した値をExcelに入力したいのですが、その方法がわかりません。
データは取り出せてます。
Excelのセルの1つ前の行がSQLから取り出したWri(RowCnt, 0)と違うのならそれを入力し、更にWri(RowCnt, 1)と同じ値の列を探してWri(RowCnt, 4)の値を入力したいです。
For i = 9 To UBound(Wri)のとこまでは行くのですが、そのあとがオブジェクトが必要ですというエラーで前に進めません。
ご教示お願いします。
VBA
1Public Function Output_202(s() As String) As String 2 3 Dim outputfile As String 4 5 Dim sSQL As String 6 Dim rst As ADODB.Recordset 7 8 Dim sNen As String 9 Dim sSin As String 10 Dim sSgyo As String 11 Dim sHday As String 12 13 Dim Rec() As Variant 14 Dim RowCnt As Long 15 Dim ColCnt As Long 16 17 Dim Wri() As Variant 18 19 Dim wbk As Workbook 20 Dim wks As Worksheet 21 22 Dim i As Long 23 Dim j As Long 24 Dim c As Long 25 26 '引数のセット 27 sNen = Mid$(s(1), 7, 2) '年度 28 sSin = Mid$(s(1), 6, 1) '新規・再送 29 sSgyo = Mid$(s(1), 12, 4) '作業年月 30 sHday = s(2) '発送日 31 32 '出力ファイル準備 33 outputfile = sNen & "年度" 34 If sSin = "2" Then outputfile = outputfile & "_再送" 35 outputfile = outputfile & "_件数表~コース別・金庫別発送件数_" 36 outputfile = outputfile & Format$(Date$, "yyyymmdd") & Format$(Time$, "hhmmss") & ".xlsx" 37 38 If file_copy("Z202.xlsx", sFolderFormatFile, outputfile, sFolderSaveFile) = 9 Then 39 'ファイルコピーエラー 40 Output_202 = "ひな形ファイル Z202.xlsx を取得できません" 41 Exit Function 42 End If 43 44 Application.ScreenUpdating = False 45 Set wbk = Workbooks.Open(sFolderSaveFile & outputfile) 46 Application.ScreenUpdating = True 47 48 'データ読込 49'On Error GoTo ERR_RTN: 50 51 'DBからデータ取得 52 If access_mySQL_Server = False Then 53 GoTo ERR_RTN 54 End If 55 56 With cmd 57 58 sSQL = "SELECT concat(wfz_bkcode,':',wfz_bkname),concat(wfz_kzcode,cast(wfz_h_flg as char)) as code,wfz_count,wfz_test,count(wfz_bkcode) " 59 sSQL = sSQL & " from " & s(1) & ", kzz" 60 sSQL = sSQL & " where wfz_kzcode = kzz_kzcode" 61 sSQL = sSQL & " group by wfz_bkcode,code" 62 63 .ActiveConnection = cnn 64 .CommandText = sSQL 65 Set rst = .Execute 66 sSQL = "" 67 68 ColCnt = rst.Fields.Count 69 RowCnt = rst.RecordCount 70 71 If RowCnt > 0 Then 72 ReDim Rec(ColCnt - 1, RowCnt - 1) 73 Rec = rst.GetRows 74 End If 75 76 ReDim Wri(RowCnt, ColCnt) 77 78 Set wks = wbk.Worksheets(1) 79 80 For RowCnt = 0 To UBound(Rec, 2) 81 82 Wri(RowCnt, 0) = CStr(Rec(0, RowCnt)) 83 Wri(RowCnt, 1) = CStr(Rec(1, RowCnt)) 84 If Len(Wri(RowCnt, 2)) = 1 Then 85 Wri(RowCnt, 2) = "第" & StrConv(Wri(RowCnt, 2), vbWide) & "分冊" 86 ElseIf Len(Wri(RowCnt, 2)) = 2 Then 87 Wri(RowCnt, 2) = StrConv(Mid$(Wri(RowCnt, 2), 1, 1), vbWide) & "/" & StrConv(Mid$(Wri(RowCnt, 2), 2, 1), vbWide) & "分冊" 88 End If 89 If Wri(RowCnt, 3) <> "" Then 90 Wri(RowCnt, 3) = "課題" & CStr(Rec(3, RowCnt)) & "回" 91 Else 92 Wri(RowCnt, 3) = "×" 93 End If 94 Wri(RowCnt, 4) = Rec(4, RowCnt) 95 96 Next 97 98 For i = 9 To UBound(Wri) 99 If wks.Cells(i - 1, 2).Value <> Wri(RowCnt, 0).Value Then wks.Cells(i, 2).Value = Wri(RowCnt, 0) 100 For c = 4 To 38 101 If wks.Cells(5, c) = Wri(RowCnt, 1) Then 102 j = wks.Cells(5, c).colomn 103 Exit For 104 End If 105 Next 106 107 wks.Cells(i, j).Value = Wri(RowCnt, 4).Value 108 Next 109 110 wks.Cells(1, 4).Value = Format$(Date$, "yyyy/m/d") 111 wks.Cells(3, 3).Value = "20" & Format$(sSgyo, "##年##月") 112 113 wks.Select 114 wks.Cells(1, 1).Select 115 116 End With 117 118 If close_mySQL_Server = False Then 119 GoTo ERR_RTN 120 End If 121 122 Application.DisplayAlerts = False 123 wbk.Save 124 Application.DisplayAlerts = True 125 126 wbk.Close 127 Set wks = Nothing 128 Set wbk = Nothing 129 130 Output_202 = outputfile 131 132Exit Function 133ERR_RTN: 134 If Len(sErr(0)) = 0 Then 135 sErr(0) = CStr(Err.Number) 136 sErr(1) = Err.Description 137 sErr(2) = "Output_202" 138 sErr(3) = sSQL 139 End If 140End Function 141 142
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/11/21 02:12 編集
2019/11/21 02:13