
Sh1(入力シート)のI25セルに枚数、K25セルに金額等のデータをSh4(A9引換帳)の
G列、Q列等に転記するコードですが、Sh4に表示されません。次のコードのどこに原因があるか教えて下さい。なお、都合上 画像を添付できない旨ご理解ください。
Sub A9引換帳()
Dim mRow As Long
If Not IsDate(Range("I8").Value) Then Exit Sub ' セルI8の値が日付でない場合、サブルーチンを終了する
Select Case Month(Range("I8").Value) ' セルI8の値が日付である場合、その月を取得する
' 以下に各月ごとの処理を記述する
Case 4: mRow = 1 ' 4月の場合の処理をする、以下同じ
Case 5: mRow = 38
Case 6: mRow = 95
Case 7: mRow = 139
Case 8: mRow = 176
Case 9: mRow = 218
Case 10: mRow = 265
Case 11: mRow = 307
Case 12: mRow = 349
Case 1: mRow = 393
Case 2: mRow = 433
Case 3: mRow = 475
End Select
Call Giftbook_main(mRow)
End Sub
Public Sub Giftbook_main(mRow As Long)
Dim sh4 As Worksheet
Dim sh1 As Worksheet
Dim maxrow As Long
Dim Row As Long
Dim dicT As Object
Dim key1 As Variant
Dim key2 As Variant
Dim mRow As Long
Dim lastRow As Long ' 最後の記録行を追跡する変数
Set sh4 = Worksheets("A9引換帳")
Set sh1 = Worksheets("入力シート")
Set dicT = CreateObject("Scripting.Dictionary")
maxrow = sh1.Cells(Rows.Count, "E").End(xlUp).Row 'E伝票番号(一連番号)
For Row = 8 To maxrow '8行から最終までの行
key1 = sh4.Cells(Row, "E").Value 'A9引換帳の伝票番号"E"の値の行
dicT(key1) = Row
Next
'伝票番号を記憶
key2 = sh1.Cells(8, "E").Value
dicT(key2) = Row
If dicT.exists(key2) = False Then
Exit Sub
End If
If sh1.Cells(8, "L").Value = "" Then
MsgBox ("年が未表示です")
End If
Row = dicT(key1)
If sh1.Cells(25, "I").Value = "0" Then 'A9セルの枚数が"0"ならば
sh4.Cells(Row + mRow, "F").EntireRow.Delete 'A9引換帳のF列の(Row + mRow)行目を削除する
End If
'A9引換帳に転記する。
If sh1.Cells(25, "I").Value <> "0" And sh1.Cells(25, "I").Value <> "0" Then '25行目の"I"列のセルの値が"0"でない場合
' 最後の記録行を更新
Do While sh4.Cells(lastRow + mRow + 1, "F").Value <> "" '(lastRow + mRow + 1)行目の"F"列のセルの値が空でない間、
lastRow = lastRow + 1 'lastRowの値を1ずつ増やす処理を繰り返す
Loop
lastRow = lastRow + 1
sh4.Cells(lastRow + mRow, "F").Value = sh1.Cells(12, "E").Value '名前
sh4.Cells(lastRow + mRow, "G").Value = sh1.Cells(25, "I").Value '枚数
sh4.Cells(lastRow + mRow, "D").Value = sh1.Cells(8, "N").Value '日
sh4.Cells(lastRow + mRow, "C").Value = sh1.Cells(8, "M").Value '月
sh4.Cells(lastRow + mRow, "E").Value = sh1.Cells(8, "E").Value '伝票番号
sh4.Cells(lastRow + mRow, "Q").Value = sh1.Cells(25, "K").Value '金額
End If
' 重複チェック
Dim exists As Boolean
exists = False
Dim i As Long
For i = 8 To sh4.Cells(Rows.Count, "E").End(xlUp).Row
If sh4.Cells(i, "E").Value = sh1.Cells(8, "E").Value Then
exists = True
Exit For
End If
Next i
If Not exists Then
' データを転記する
sh4.Cells(lastRow + 1, "F").Value = sh1.Cells(12, "E").Value '名前
sh4.Cells(lastRow + 1, "G").Value = sh1.Cells(25, "I").Value '枚数
sh4.Cells(lastRow + 1, "D").Value = sh1.Cells(8, "N").Value '日
sh4.Cells(lastRow + 1, "C").Value = sh1.Cells(8, "M").Value '月
sh4.Cells(lastRow + 1, "E").Value = sh1.Cells(8, "E").Value '伝票番号
sh4.Cells(lastRow + 1, "Q").Value = sh1.Cells(25, "K").Value '金額
End If
End Sub
No.1ベストアンサー
- 回答日時:
Public Sub Giftbook_main(mRow As Long) 内の
この宣言を削除。
Dim mRow As Long
ここは、andで綴っているのが同じ条件なので、条件指定間違いでなければ1つでいい。
If sh1.Cells(25, "I").Value <> "0" And sh1.Cells(25, "I").Value <> "0" Then '25行目の"I"列のセルの値が"0"でない場合
ざっくりと動作するであろう範囲でブックを作成して実行したところでは、「A9引換帳」で設定したmRow+1行目に転記されました。
実際にうまく動作しない状態のサンプルデータが検証しやすいのですが…。
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
ExcelVBAを使って、値...
-
任意フォルダから画像をすべて...
-
Excelで指定した日付から過去の...
-
i=cells(Rows.Count, 1)とi=cel...
-
TODAY()で設定したセルの日付...
-
特定のセルが空白だったら、そ...
-
Excel vbaで特定の文字以外が入...
-
Excelのプルダウンで2列分の情...
-
スプレッドシートの数値列に対...
-
VBA実行後に元のセルに戻りたい
-
エクセルvbaで、別シートの最下...
-
セル色なしの行一括削除
-
【VBA】シート上の複数のチェッ...
-
DataGridViewのセルのItem設定...
-
【VBA】指定したセルと同じ値で...
-
CountIf で","(カンマ)の数が...
-
VLOOKUP関数で別ファイルを指定...
-
DataGridViewで列、行、セルの選択
-
【Excel VBA】指定行以降をクリ...
-
連続する複数のセル値がすべて0...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
ExcelVBAを使って、値...
-
特定のセルが空白だったら、そ...
-
Excelで指定した日付から過去の...
-
i=cells(Rows.Count, 1)とi=cel...
-
エクセルvbaで、別シートの最下...
-
任意フォルダから画像をすべて...
-
VBA実行後に元のセルに戻りたい
-
Application.Matchで特定行の検索
-
Excel vbaで特定の文字以外が入...
-
【Excel VBA】指定行以降をクリ...
-
”戻り値”が変化したときに、マ...
-
【Excel】指定したセルの名前で...
-
VBAでセル同士を比較して色付け
-
Excelのプルダウンで2列分の情...
-
VBAでセルをクリックする回...
-
Excel VBAで、 ヘッダーへのセ...
-
DataGridViewのセル編集完了後...
-
VBA初心者です。結合セルを保持...
-
【VBA】シート上の複数のチェッ...
-
ExcelのVBAで数字と文字列をマ...
おすすめ情報