Excel VBA 質問スレッド №2164 (未解決)
ByRef引数の型が一致しません。とコンパイルエラーになってしまいます。どう修正したら良いでしょうか。
投稿者 : こうど 投稿日時 : 2026/01/09(Fri) 16:31:08 OS : 未指定 EXCEL : 未指定
Sub test_2()
Dim srcSheets As Variant
Dim destSheets As Variant
Dim wsSrc As Worksheet, wsDest As Worksheet, wsTotal As Worksheet
Dim r As Long, c As Long, i As Integer
Dim cellVal As String, floorNum As String
Dim srcTime As Double
Dim destCol As Integer, startRow As Long, endRow As Long
Dim targetRow As Long, found As Boolean
' --- 設定エリア ---
' 読み取り対象のシート名をここに追加してください
srcSheets = Array("A1006", "B1007")
' 書き込み先のシート名
Set wsTotal = Sheets("全フロア表")
' 曜日ごとのソース列(C=月, E=火, G=水, I=木, K=金)
Dim daysSrc: daysSrc = Array(3, 5, 7, 9, 11)
' 曜日ごとの書き込み列(C=月, F=火, I=水, L=木, O=金) ※3列おきと想定
Dim daysDest: daysDest = Array(3, 6, 9, 12, 15)
' ------------------
' 転記先シートのデータ部分をクリア(任意)
On Error Resume Next
For i = 2 To 4
Sheets(i & "階フロア表").Range("C2:Q50").ClearContents
Next i
wsTotal.Range("C2:Q50").ClearContents
On Error GoTo 0
' 各ソースシートをループ
For Each sName In srcSheets
Set wsSrc = Sheets(sName)
' 各行をループ (2行目から21行目)
For r = 2 To 21
srcTime = wsSrc.Cells(r, 1).Value ' A列の時間
' 時間帯によって書き込み行の範囲を決定
Select Case srcTime
Case Is < TimeValue("09:50"): startRow = 2: endRow = 6 ' 9:10-9:50枠
Case Is < TimeValue("12:59"): startRow = 9: endRow = 11 ' 9:51-12:00枠
Case Is < TimeValue("15:01"): startRow = 12: endRow = 18 ' 13:00-15:00枠
Case Else: startRow = 19: endRow = 25 ' 15:01-17:00枠
End Select
' 各曜日(月~金)をループ
For i = 0 To 4
cellVal = wsSrc.Cells(r, daysSrc(i)).Value
If cellVal <> "" Then
' ( )内の階層数字を抽出
floorNum = ""
If InStr(cellVal, "(2)") > 0 Then floorNum = "2"
If InStr(cellVal, "(3)") > 0 Then floorNum = "3"
If InStr(cellVal, "(4)") > 0 Then floorNum = "4"
If floorNum <> "" Then
' 1. 各階のシートへ転記
Set wsDest = Sheets(floorNum & "階フロア表")
Call WriteToEmptyCell(wsDest, cellVal, daysDest(i), startRow, endRow)
' 2. 全フロア表へ転記
Call WriteToEmptyCell(wsTotal, cellVal, daysDest(i), startRow, endRow)
End If
End If
Next i
Next r
Next sName
MsgBox "転記が完了しました。"
End Sub
Sub WriteToEmptyCell(ws As Worksheet, cellValue As String, targetCol As Integer, startRow As Long, endRow As Long)
Dim targetRow As Long
targetRow = startRow
' 指定列の範囲内で空いている最初のセルを探す
Do While ws.Cells(targetRow, targetCol).Value <> "" And targetRow <= endRow
targetRow = targetRow + 1
Loop
' 空いているセルが見つかれば書き込み
If targetRow <= endRow Then
ws.Cells(targetRow, targetCol).Value = cellValue
End If
End Sub
スポンサーリンク
[返信 1] Re : ByRef引数の型が一致しません。とコンパイルエラーになってしまいます。どう修正したら良いでしょうか。
投稿者 : ごんぼほり 投稿日時 : 2026/01/09(Fri) 17:21:36
Dim daysDest: daysDest = Array(3, 6, 9, 12, 15)
と定義しているので、daysDestはVariant型
Call WriteToEmptyCell(wsDest, cellVal, daysDest(i), startRow, endRow)
とするとき daysDest(i) は Variant/Integer型
Sub WriteToEmptyCell(ws As Worksheet, cellValue As String, targetCol As Integer, startRow As Long, endRow As Long)
で、targetCol はInteger型なので、型が異なります。
修正の方針はいくつかあって、
(1) ByRefである必要がない場合
(1-1) 引数をByVal にする 引数がコピーされるときに自動で型変換されます
Sub WriteToEmptyCell(ws As Worksheet, cellValue As String, ByVal targetCol As Integer, startRow As Long, endRow As Long)
(1-2) Call するときに明示的に型変換する
Call WriteToEmptyCell(wsDest, cellVal, Int(daysDest(i)), startRow, endRow)
(2) ByRefである必要がある場合
(2-1) Subプロシジャ側でVariant型でうける
Sub WriteToEmptyCell(ws As Worksheet, cellValue As String, targetCol As Variant, startRow As Long, endRow As Long)
(2-2) Callする側の変数の型宣言をInteger型にする
Dim daysDest(0 to 4) as Intger : daysDest(0)=3:daysDest(1)=6:daysDest(2)=9:daysDest(3)=12:daysDest(4)=15
(2-3) daysDest(i)を別のInteger型変数に代入して、その変数を使ってCALLする
Dim daysDest: daysDest = Array(3, 6, 9, 12, 15)
と定義しているので、daysDestはVariant型
Call WriteToEmptyCell(wsDest, cellVal, daysDest(i), startRow, endRow)
とするとき daysDest(i) は Variant/Integer型
Sub WriteToEmptyCell(ws As Worksheet, cellValue As String, targetCol As Integer, startRow As Long, endRow As Long)
で、targetCol はInteger型なので、型が異なります。
修正の方針はいくつかあって、
(1) ByRefである必要がない場合
(1-1) 引数をByVal にする 引数がコピーされるときに自動で型変換されます
Sub WriteToEmptyCell(ws As Worksheet, cellValue As String, ByVal targetCol As Integer, startRow As Long, endRow As Long)
(1-2) Call するときに明示的に型変換する
Call WriteToEmptyCell(wsDest, cellVal, Int(daysDest(i)), startRow, endRow)
(2) ByRefである必要がある場合
(2-1) Subプロシジャ側でVariant型でうける
Sub WriteToEmptyCell(ws As Worksheet, cellValue As String, targetCol As Variant, startRow As Long, endRow As Long)
(2-2) Callする側の変数の型宣言をInteger型にする
Dim daysDest(0 to 4) as Intger : daysDest(0)=3:daysDest(1)=6:daysDest(2)=9:daysDest(3)=12:daysDest(4)=15
(2-3) daysDest(i)を別のInteger型変数に代入して、その変数を使ってCALLする
当掲示板について
- Excel VBA に関する掲示板です。Excel VBA に関する質問や疑問、それに対する解決方法など気軽に投稿してください。
- 記事内ではHTMLのタグは使用できません。
- 記事は一度投稿すると修正できません。内容を訂正したい場合は返信で対応してください。
- Sub〜End Sub、Function〜End Function は自動的にプログラムコードとみなし、枠で囲って見やすくします。
- Excel VBA とは関係ないことや、他人が不快に思うようなことなど、管理人が適当でないと判断した記事は削除する場合があります。
スポンサーリンク
返信入力フォーム
( 処理日時 : 2026-01-11 18:42:48 )