Option Explicit

'===========================================================
' ■部品_転記_セル単位 / TransferCell_WithMap
'   ・マッピング表の各行に従い、転記元のセルの値を、転記先のセルへ転記する（セル単位・値のみ）
'     → Transfers the value of each source cell to its destination cell according to each row of the
'        mapping table (cell by cell, values only).
'
'   ・マッピング表は、範囲設定部品（SetRange_VariableRows など）で作った Range を渡す
'     表の中の、どの列に何が書いてあるかは、引数（列の位置）で指定する（表を自由にデザインできる）
'     → Pass the mapping table as a Range made by a range-setting component; which column holds what is
'        specified by arguments (column positions), so the table can be designed freely.
'
'   ・名前に「転記（Transfer）」が付く部品は、マッピング表（転記元・転記先を記入した表）を使う
'     → Components named "転記 (Transfer)" use a mapping table (a table listing sources and destinations).
'       範囲を直接指定してコピーする部品は「コピー（Copy）」、1つのセルに「＝」で代入する部品は
'       「代入（Assign）」で、別の部品である
'       → Components that copy a directly specified range are "Copy", and components that assign to a
'          single cell with "=" are "Assign"; they are separate.
'
'   ★ この部品は Sub（プロシージャ）ではなく「Function（関数）」で、成功／失敗を Boolean で返す
'     → ★ This component is a Function (not a Sub) that returns success/failure as a Boolean.
'===========================================================

'===========================================================
' ■区分 / Classification
'   ・基礎区分：応用 ／ 用途区分：処理 ／ 操作対象区分：セル
'     → Base: application / Purpose: process / Target: cell
'
' ■バージョン / Version
'   ・Version 1.0（2026-10-29）
'===========================================================

'===========================================================
' ■URL（WordPress部品ページ）
'   https://feelcodelab.com/vba_parts/transfercell_withmap/
'===========================================================

'===========================================================
' ■前提条件（AI誤動作防止の最重要項目） / Preconditions
'   ★【最重要】この部品は Function（関数）であり、戻り値（Boolean）を返す
'     → 転記できた場合は True、実行時にできなかった場合は False（この場合は、エラーでは止まらない）
'     → 呼び出し側は、必ず戻り値を確認してから後続処理を行い、メッセージも呼び出し側で表示する
'       （False のときは、MsgBox のあとに Exit Sub まで書く）
'     → ★ [MOST IMPORTANT] This component is a Function that returns a Boolean: True if transferred, False if
'        it failed at run time (no error is raised then). The caller must check the return value before the
'        following processing and show any message itself (write Exit Sub after the MsgBox).
'
'   ★【最重要】転記元のブック（vSrcWb）と、転記先のブック（vDstWb）は、必ず引数で指定する（省略できない）
'     → マッピング表に書いたシート名が「どのブックのシートか」は、この2つの引数で決まる
'     → ファイルを開く部品の後は、ActiveWorkbook が、開いたファイルに切り替わる。
'       開いた直後に、ActiveWorkbook を変数へ控えて、その変数を渡す
'     → ★ [MOST IMPORTANT] The source workbook (vSrcWb) and the destination workbook (vDstWb) must always be
'        specified (they cannot be omitted); they decide which workbook each sheet name in the mapping table
'        belongs to. After the "open file" component runs, ActiveWorkbook switches to the opened file, so keep
'        it in a variable right after opening and pass that variable.
'
'   ★【最重要】マッピング表の範囲（vMapRng）は、見出し行を含めず、データの行だけの範囲を渡す
'     → 範囲は、範囲設定_行可変などで作る。最終行は、必ずデータが入っている列（転記元シートの列など）で判定する
'     → ★ [MOST IMPORTANT] Pass only the data rows of the mapping table (without the header row), made by a
'        range-setting component; decide the last row by a column that always has data.
'
'   ★【最重要】列の位置（vColSrcSheet 〜 vColDstCol）は、マッピング表の範囲の「左端の列」を 1 とした番号で指定する
'     → 例：範囲が A 列から始まる場合、B 列は 2。範囲が B 列から始まる場合、B 列は 1
'     → ★ [MOST IMPORTANT] Column positions are numbers counted with the left-most column of the mapping
'        range as 1 (if the range starts at column A, column B is 2; if it starts at column B, column B is 1).
'
'   ・マッピング表の例（A 列から H 列。範囲は A 列から始まる）
'     → Mapping table example (columns A to H; the range starts at column A):
'
'        名称   | 転記元シート | 行 | 列 | 転記先シート | 行 | 列 | 備考
'        顧客名 | 入力         | 6  | 2  | 帳票         | 3  | 1  |
'
'        この場合の列の位置は、転記元シート＝2、転記元の行＝3、転記元の列＝4、
'        転記先シート＝5、転記先の行＝6、転記先の列＝7 になる
'        → Here the positions are: source sheet = 2, source row = 3, source column = 4,
'           destination sheet = 5, destination row = 6, destination column = 7.
'
'   ・マッピング表の行・列は、1 以上の数字で書く（"B6" のような A1 形式ではない）
'     → Rows and columns in the mapping table are numbers of 1 or more (not A1-style such as "B6").
'
'   ・シート名に "ActiveSheet" と書くと、その時点で、指定したブック（vSrcWb／vDstWb）の、
'     アクティブなシートを指す（テンプレートのシートをコピーした直後など、シート名が決まらないときに使う）
'     → "ActiveSheet" は、実行時の状態に依存するので、シートをコピーした直後など、状態が確実なときだけ使う
'     → 転記を始める前に、1 回だけシート名に置き換える（途中でシートが変わっても、同じシートへ転記する）
'     → Entering "ActiveSheet" as a sheet name refers to the active sheet of the specified workbook
'        (vSrcWb / vDstWb) at that moment; it is resolved to a sheet name once before transferring. It depends on
'        the run-time state, so use it only when the state is certain (e.g., right after copying a template sheet).
'
'   ・転記するのは「値のみ」（書式・数式は転記しない。数式は、結果の値になる）
'     → Only values are transferred (formatting and formulas are not; a formula becomes its result value).
'
'   ・転記を始める前に、マッピング表の全行を確認する
'     → 1行でも誤りがあれば、1行も転記せずに、行番号を示すエラーで止まる
'     → Before transferring, all rows of the mapping table are checked; if any row is wrong, nothing is
'        transferred and it stops with an error naming the row.
'
'   ・転記の途中で、実行時の失敗（保護されたシートなど）が起きた場合、それまでに転記した分は、元に戻らない
'     （False を返したあとも、書き込み済みの値は残る）
'     → If a run-time failure happens midway, what was already transferred is not rolled back.
'
'   ・設定の誤りは、False ではなく、原因を示すエラーで止まる（Err.Raise）
'       - 転記元ブック（vSrcWb）が Nothing（vbObjectError + 1001）
'       - 転記先ブック（vDstWb）が Nothing（vbObjectError + 1002）
'       - マッピング表の範囲（vMapRng）が Nothing（vbObjectError + 1003）
'       - マッピング表のシート名が、指定したブックに存在しない（vbObjectError + 1004：転記元／1005：転記先）
'       - マッピング表の行・列が、1 以上の数字でない（vbObjectError + 1007）
'       - マッピング表の行・列が、シートの上限を超えている（vbObjectError + 1009）
'       - 列の位置の指定が、マッピング表の範囲の列数の外（vbObjectError + 1015）
'     → Setting mistakes stop with an error naming the cause instead of returning False.
'
'   ・Offset / Find / Resize / With は禁止
'     → Offset, Find, Resize, and With statements are prohibited.
'===========================================================

'===========================================================
' ■依存関係（部品同士） / Component Dependencies
'   ・f_SheetExists（部品_シート存在チェック）が必要
'     → Requires f_SheetExists (SheetExists component).
'       ・マッピング表のシート名が、指定したブックに存在するかの確認に使用
'         → Used to check that the sheet names in the mapping table exist in the specified workbooks.
'
'   ・f_AssignCell（部品_代入_セル）が必要
'     → Requires f_AssignCell (AssignCell component).
'       ・1行ごとの転記（1セルの値の代入）に使用
'         → Used for the transfer of each row (assigning the value of one cell).
'
'   ・組み合わせ：f_SetRange_VariableRows → f_TransferCell_WithMap
'     （範囲設定_行可変で、マッピング表の範囲を作ってから、渡す）
'     → Typical combination: [SetRange_VariableRows] -> [TransferCell_WithMap]
'        (make the range of the mapping table, then pass it).
'
'   ・組み合わせ：f_OpenFile_ByPath → f_TransferCell_WithMap
'     （転記先のファイルを開いてから転記する場合。開いた直後に ActiveWorkbook を変数へ控える）
'     → Typical combination: [OpenFile_ByPath] -> [TransferCell_WithMap]
'        (keep ActiveWorkbook in a variable right after opening).
'===========================================================

'===========================================================
' ■依存関係（シート・表） / Sheet Dependencies
'   ・マッピング表（ユーザーが、事前に記入する）
'     → The mapping table (entered by the user beforehand):
'        転記元シート名・転記元の行・転記元の列・転記先シート名・転記先の行・転記先の列
'        / source sheet name, source row, source column, destination sheet name, destination row, destination column
'
'   ・列の並びは、自由（名称や備考の列があってもよい）。引数の列の位置で、どの列が何かを指定する
'     → The column order is free (name and note columns may exist); the column-position arguments say which
'        column is what.
'===========================================================

'===========================================================
' ■技術解説 / Technical Notes
'
'   ・マッピング表方式にする理由
'       → 転記元・転記先の対応をコードに書かず表で管理することで、
'          転記内容を変更するたびにコードを修正する必要がなくなる。
'       → Managing source/destination pairs in a table instead of in code
'          removes the need to edit the code whenever the transfer targets change.
'
'   ・マッピング表を、Range と列の位置の引数で受け取る理由
'       → 旧版のように、表の開始行や列の位置を、部品の中の定数に持たせると、表のレイアウトを変えたときに、
'          部品を書き換える必要がある（隠れた前提になる）。範囲と列の位置を、呼び出し側が渡す形にすると、
'          表を自由にデザインでき、部品は書き換えずに済む。
'       → Keeping the start row and column positions as constants inside the component (as in the old version)
'          hides a precondition and needs editing when the layout changes; receiving the range and the column
'          positions from the caller lets the table be designed freely without editing the component.
'
'   ・行・列を、A1 形式（"B6"）ではなく、数字で書く理由
'       → 代入_セルが、行番号・列番号で受け取るため、変換が不要になり、書き間違いも確認しやすい。
'       → AssignCell receives row and column numbers, so no conversion is needed and mistakes are easy to check.
'
'   ・ブックを、省略できない引数にしている理由
'       → シート名だけでは、どのブックのシートか決まらず、Worksheets("名前") は、そのときの
'          ActiveWorkbook のシートを指してしまう。ファイルを開くと ActiveWorkbook が切り替わるため、
'          取り違えが起きやすい。ブックを必ず引数で渡す形にすると、呼び出しの行に
'          「どのブックからどのブックへ」が書かれ、読むだけで意図が分かる。
'       → A sheet name alone does not identify the workbook, and Worksheets("name") would refer to the current
'          ActiveWorkbook, which switches when a file is opened. Always passing the workbooks makes the call
'          itself say "from which workbook to which".
'
'   ・"ActiveSheet" を、転記の前に 1 回だけ、シート名に置き換える理由
'       → 転記の途中で、アクティブなシートが変わっても、すべての行が、同じシートへ転記されるようにするため。
'          また、置き換えたシート名で、存在確認と、代入_セルの呼び出しが、同じ形で行える。
'       → So that all rows go to the same sheet even if the active sheet changes midway, and the same existence
'          check and AssignCell call work with the resolved name.
'
'   ・転記の前に、マッピング表の全行を、まとめて確認する理由
'       → シート名の打ち間違いや、行・列の書き間違いは、起きやすい誤りである。そのまま転記を始めると、
'          途中まで転記された状態で止まる。そこで、先に全行を確認し、誤りがあれば、行番号を示すエラーで止める。
'       → Typos in sheet names and wrong rows/columns are common; transferring directly would stop with the
'          data only half transferred, so all rows are checked first and a mistake stops with the row number.
'
'   ・確認を On Error GoTo より前に置く理由
'       → On Error GoTo ErrHandler の有効な範囲でエラーを出すと、False に変えられてしまい、原因が伝わらない。
'          確認を前に置くことで、Err.Raise が呼び出し側へ届く。
'       → Raising inside the range where On Error GoTo is active would turn it into False and hide the cause;
'          the check is placed before it so that Err.Raise reaches the caller.
'
'   ・転記できなかった場合に、False を返す理由
'       → 保護されたシートなど、実行時の失敗は、事前には分からない。
'          呼び出し側が、戻り値で確認できるようにして、メッセージやエラー処理は、呼び出し側で調整できるようにしている。
'          原因は、イミディエイトウィンドウ（Ctrl+G）に、Debug.Print で出る（どの行で止まったか、
'          中の部品が出したエラーは、発生元の部品名も出る）。
'       → Run-time failures such as a protected sheet cannot be known beforehand. The Boolean return value lets
'          the caller check the result; the cause (and the stopped row) is printed to the Immediate window.
'
'   ・Value 同士を代入する（値のみ転記する）理由
'       → Copy メソッドと異なり、書式や入力規則を上書きせず、
'          クリップボードも使わないため高速で安全に動作する。
'       → Unlike the Copy method, assigning Value does not overwrite formatting or validation
'          and does not use the clipboard, making it faster and safer.
'
'   ・Offset / Find / Resize / With を禁止する理由
'       → 生成AIが誤って不正な範囲操作を行う可能性が高く、行数可変の処理で誤動作を引き起こしやすいため。
'       → These statements often cause misaligned or ambiguous range operations when generated by AI.
'
'   ・この部品の位置づけ
'       → マッピング表で指定する「転記（Transfer）」のうち、セル単位の部品。
'          迷ったときは、この部品を標準として選ぶ。
'       → The cell-based component among the mapping-table "Transfer" components;
'          choose it as the standard when unsure.
'
'   ・本部品が「応用」である理由
'       → 土台の部品（シート存在チェック・代入_セル）を参照するため。
'       → It references foundation components (SheetExists and AssignCell).
'===========================================================

'---------------------------------------------
' ■引数（型＋意味） / Arguments
'   vSrcWb        : 転記元のブック（Workbook）※必須（省略できない）
'                   → Source workbook (Workbook); required.
'
'   vDstWb        : 転記先のブック（Workbook）※必須（省略できない）
'                   → Destination workbook (Workbook); required.
'
'   vMapRng       : マッピング表の範囲（Range）。見出し行を含まない、データの行だけ
'                   → Range of the mapping table (Range); data rows only, without the header row.
'
'   vColSrcSheet  : 転記元のシート名が入っている列の位置（Long）。範囲の左端を 1 とした番号
'                   → Position of the column holding the source sheet name (Long); left-most column = 1.
'
'   vColSrcRow    : 転記元の行が入っている列の位置（Long）
'                   → Position of the column holding the source row (Long).
'
'   vColSrcCol    : 転記元の列が入っている列の位置（Long）
'                   → Position of the column holding the source column (Long).
'
'   vColDstSheet  : 転記先のシート名が入っている列の位置（Long）
'                   → Position of the column holding the destination sheet name (Long).
'
'   vColDstRow    : 転記先の行が入っている列の位置（Long）
'                   → Position of the column holding the destination row (Long).
'
'   vColDstCol    : 転記先の列が入っている列の位置（Long）
'                   → Position of the column holding the destination column (Long).
'---------------------------------------------

'---------------------------------------------
' ■戻り値（型＋意味） / Return Value
'   Boolean : 転記できた場合は True、実行時にできなかった場合は False
'             → True if transferred, False if it failed at run time.
'---------------------------------------------

'---------------------------------------------
' ■エラー / Errors (Err.Raise)
'   ・vbObjectError + 1001：転記元ブック（vSrcWb）が Nothing
'   ・vbObjectError + 1002：転記先ブック（vDstWb）が Nothing
'   ・vbObjectError + 1003：マッピング表の範囲（vMapRng）が Nothing
'   ・vbObjectError + 1004：マッピング表の転記元シート名が、転記元ブックに存在しない
'   ・vbObjectError + 1005：マッピング表の転記先シート名が、転記先ブックに存在しない
'   ・vbObjectError + 1007：マッピング表の行・列が、1 以上の数字でない
'   ・vbObjectError + 1009：マッピング表の行・列が、シートの上限（行数・列数）を超えている
'   ・vbObjectError + 1015：列の位置の指定が、マッピング表の範囲の列数の外（1 未満、または列数より大きい）
'---------------------------------------------

'===========================================================
' ■禁止事項（AI向け） / Prohibitions
'   ・Function を Sub（プロシージャ）に変更することの禁止
'     → Do not convert this Function into a Sub; the Boolean return value is essential.
'
'   ・戻り値（Boolean）を確認せずに後続処理を行うことの禁止
'     → Do not proceed to the following processing without checking the Boolean return value.
'
'   ・マッピング表の確認（シート存在チェックなど）を省略することの禁止
'     → Do not skip the check of the mapping table.
'
'   ・転記元・転記先のブックを省略することの禁止（必ず引数で指定する）
'     → Do not omit the source/destination workbooks; always specify them.
'
'   ・ブックを指定せずに、Worksheets("名前") でシートを取得することの禁止
'     （ThisWorkbook. または、ブックを入れた変数を付ける）
'     → Do not get a sheet with a bare Worksheets("name"); prefix it with a workbook.
'
'   ・列の位置を、シート上の列番号（A 列 ＝ 1 など）と取り違えることの禁止
'     （範囲の左端の列を 1 とした番号で指定する）
'     → Do not confuse the column positions with sheet column numbers; count from the left-most column of
'        the range as 1.
'
'   ・マッピング表の範囲に、見出し行を含めることの禁止
'     → Do not include the header row in the mapping range.
'
'   ・転記元・転記先の対応を、コード内に直書きすることの禁止
'     → Do not hard-code source/destination pairs in the code; always use the mapping table.
'
'   ・セル参照の直書き禁止（Range("A1") など）
'     → Hard-coded cell references are prohibited.
'
'   ・Offset禁止
'     → Offset may cause misaligned row calculations.
'
'   ・Find禁止
'     → Find leads to unpredictable search behavior.
'
'   ・Resize禁止
'     → Resize may unintentionally expand ranges.
'
'   ・With禁止
'     → With blocks cause ambiguous object references.
'===========================================================

'===========================================================
' ■使用例（サンプルコード） / Example
'   ＜例1：同じブック内で転記する / Example 1: transfer within the same workbook＞
'   Dim map As Range
'
'   Set map = 部品_範囲設定_行可変(ThisWorkbook, "マッピング表", 3, 1, 8, 2)   '3行目から、A-H列。最終行は2列目（転記元シート）で判定
'   If map Is Nothing Then
'       MsgBox "マッピング表にデータがありません"
'       Exit Sub
'   End If
'
'   If 部品_転記_セル単位(ThisWorkbook, ThisWorkbook, map, 2, 3, 4, 5, 6, 7) = False Then
'       MsgBox "転記に失敗しました（シートの保護などを確認してください）"
'       Exit Sub
'   End If
'
'   ＜例2：開いたファイルへ転記する / Example 2: transfer into an opened file＞
'   Dim wbDst As Workbook
'
'   If 部品_ファイルを開く("E:\TEST\テンプレート.xlsx") = False Then Exit Sub
'   Set wbDst = ActiveWorkbook                               '開いた直後に控える / keep it right after opening
'
'   Set map = 部品_範囲設定_行可変(ThisWorkbook, "マッピング表", 3, 1, 8, 2)
'   If map Is Nothing Then MsgBox "マッピング表にデータがありません": Exit Sub
'   If 部品_転記_セル単位(ThisWorkbook, wbDst, map, 2, 3, 4, 5, 6, 7) = False Then MsgBox "転記に失敗しました": Exit Sub
'
'   ＜例3：コピーした直後のシートへ転記する（シート名を "ActiveSheet" と書く）/ Example 3: transfer into a just-copied sheet＞
'   wbDst.Worksheets("雛形").Copy After:=wbDst.Worksheets(wbDst.Worksheets.Count)   'コピーしたシートが、アクティブになる
'   If 部品_転記_セル単位(ThisWorkbook, wbDst, map, 2, 3, 4, 5, 6, 7) = False Then MsgBox "転記に失敗しました": Exit Sub
'   （マッピング表の、転記先シートの列に、ActiveSheet と書いておく）
'
'   ＜マッピング表の例 / Mapping table example＞
'   名称     | 転記元シート | 行 | 列 | 転記先シート | 行 | 列 | 備考
'   顧客名   | 入力         | 6  | 2  | 帳票         | 3  | 1  |
'   日付     | 入力         | 4  | 2  | ActiveSheet  | 1  | 3  |
'
'   ※ 設定の誤り（Nothing、シート名の誤り、行・列の誤り、列の位置の誤り）の場合は、False ではなく、
'      原因を示すエラーで止まる（起点の On Error で受けて、Err.Description を表示する）
'===========================================================

'===========================================================
' ■ラッパー関数（日本語名） / Wrapper Function
'   ・辞書の日本語名で呼び出すための窓口
'     → Entry point callable by the Japanese dictionary name.
'
'   ・内部では英語名の本体処理を呼び出し、結果（Boolean）をそのまま返す
'     → Internally calls the English-named core function and returns its result (Boolean) as it is.
'===========================================================
Function 部品_転記_セル単位( _
        ByVal vSrcWb As Workbook, _
        ByVal vDstWb As Workbook, _
        ByVal vMapRng As Range, _
        ByVal vColSrcSheet As Long, _
        ByVal vColSrcRow As Long, _
        ByVal vColSrcCol As Long, _
        ByVal vColDstSheet As Long, _
        ByVal vColDstRow As Long, _
        ByVal vColDstCol As Long) As Boolean

    '本体処理（英語名）を呼び出し、結果をそのまま返す / Call core function and return its result
    部品_転記_セル単位 = f_TransferCell_WithMap(vSrcWb, vDstWb, vMapRng, _
                                              vColSrcSheet, vColSrcRow, vColSrcCol, _
                                              vColDstSheet, vColDstRow, vColDstCol)

End Function

'===========================================================
' ■本体処理（英語名） / Core Function
'   ・マッピング表の各行に従い、転記元セルの値を転記先セルへ転記する
'     → Transfers the value of each source cell to its destination cell according to each row of the
'        mapping table.
'
'   ・転記できた場合：True、実行時にできなかった場合：False を返す
'     → Returns True if transferred and False if it failed at run time.
'===========================================================
Function f_TransferCell_WithMap( _
        ByVal vSrcWb As Workbook, _
        ByVal vDstWb As Workbook, _
        ByVal vMapRng As Range, _
        ByVal vColSrcSheet As Long, _
        ByVal vColSrcRow As Long, _
        ByVal vColSrcCol As Long, _
        ByVal vColDstSheet As Long, _
        ByVal vColDstRow As Long, _
        ByVal vColDstCol As Long) As Boolean

    Const cActiveSheet As String = "ActiveSheet"

    Dim i As Long
    Dim mapRowCount As Long
    Dim mapRowNo As Long
    Dim srcActiveName As String
    Dim dstActiveName As String
    Dim srcSheetName As String
    Dim dstSheetName As String
    Dim srcRow As Long
    Dim srcCol As Long
    Dim dstRow As Long
    Dim dstCol As Long

    f_TransferCell_WithMap = False

    '---------------------------------------------
    ' ■ブック・マッピング表の範囲が設定されているかを確認（Nothing の場合はエラーで止める）
    '   ※ On Error GoTo より前に置く（Err.Raise を False に変えず、呼び出し側へ届けるため）
    '   / Check that the workbooks and the mapping range are set (stop with an error if Nothing);
    '     placed before On Error GoTo so that Err.Raise reaches the caller
    '---------------------------------------------
    If vSrcWb Is Nothing Then
        Err.Raise vbObjectError + 1001, "f_TransferCell_WithMap", _
                  "転記元のブック（vSrcWb）が設定されていません（Nothing）。"
    End If

    If vDstWb Is Nothing Then
        Err.Raise vbObjectError + 1002, "f_TransferCell_WithMap", _
                  "転記先のブック（vDstWb）が設定されていません（Nothing）。"
    End If

    If vMapRng Is Nothing Then
        Err.Raise vbObjectError + 1003, "f_TransferCell_WithMap", _
                  "マッピング表の範囲（vMapRng）が設定されていません（Nothing）。"
    End If

    '---------------------------------------------
    ' ■列の位置が、マッピング表の範囲の中にあるかを確認（外の場合はエラーで止める）
    '   / Check that the column positions are inside the mapping range (stop with an error if not)
    '---------------------------------------------
    If vColSrcSheet < 1 Or vColSrcSheet > vMapRng.Columns.Count Or _
       vColSrcRow < 1 Or vColSrcRow > vMapRng.Columns.Count Or _
       vColSrcCol < 1 Or vColSrcCol > vMapRng.Columns.Count Or _
       vColDstSheet < 1 Or vColDstSheet > vMapRng.Columns.Count Or _
       vColDstRow < 1 Or vColDstRow > vMapRng.Columns.Count Or _
       vColDstCol < 1 Or vColDstCol > vMapRng.Columns.Count Then
        Err.Raise vbObjectError + 1015, "f_TransferCell_WithMap", _
                  "列の位置は、マッピング表の範囲の列数（" & vMapRng.Columns.Count & "列）の中で、" & _
                  "左端を 1 として指定してください" & _
                  "（転記元シート=" & vColSrcSheet & "、転記元の行=" & vColSrcRow & "、転記元の列=" & vColSrcCol & _
                  "、転記先シート=" & vColDstSheet & "、転記先の行=" & vColDstRow & "、転記先の列=" & vColDstCol & "）。"
    End If

    '---------------------------------------------
    ' ■"ActiveSheet" を、シート名に置き換えるための名前を取得（転記の前に 1 回だけ）
    '   / Get the sheet names that "ActiveSheet" stands for (once, before transferring)
    '---------------------------------------------
    srcActiveName = vSrcWb.ActiveSheet.Name
    dstActiveName = vDstWb.ActiveSheet.Name

    mapRowCount = vMapRng.Rows.Count

    '---------------------------------------------
    ' ■転記を始める前に、マッピング表の全行を確認する
    '   1行でも誤りがあれば、1行も転記せずに、行番号を示すエラーで止める
    '   / Before transferring, check ALL mapping rows; if any row is wrong, stop with an error naming the
    '     row without transferring anything
    '---------------------------------------------
    For i = 1 To mapRowCount

        mapRowNo = vMapRng.Row + i - 1                          'マッピング表の、シート上の行番号 / row number on the sheet

        srcSheetName = CStr(vMapRng.Cells(i, vColSrcSheet).Value)
        dstSheetName = CStr(vMapRng.Cells(i, vColDstSheet).Value)
        If StrComp(srcSheetName, cActiveSheet, vbTextCompare) = 0 Then srcSheetName = srcActiveName
        If StrComp(dstSheetName, cActiveSheet, vbTextCompare) = 0 Then dstSheetName = dstActiveName

        If f_SheetExists(vSrcWb, srcSheetName) = False Then
            Err.Raise vbObjectError + 1004, "f_TransferCell_WithMap", _
                      "マッピング表の" & mapRowNo & "行目：転記元シート「" & srcSheetName & _
                      "」が、ブック「" & vSrcWb.Name & "」に存在しません。"
        End If

        If f_SheetExists(vDstWb, dstSheetName) = False Then
            Err.Raise vbObjectError + 1005, "f_TransferCell_WithMap", _
                      "マッピング表の" & mapRowNo & "行目：転記先シート「" & dstSheetName & _
                      "」が、ブック「" & vDstWb.Name & "」に存在しません。"
        End If

        If IsNumeric(vMapRng.Cells(i, vColSrcRow).Value) = False Or _
           IsNumeric(vMapRng.Cells(i, vColSrcCol).Value) = False Or _
           IsNumeric(vMapRng.Cells(i, vColDstRow).Value) = False Or _
           IsNumeric(vMapRng.Cells(i, vColDstCol).Value) = False Then
            Err.Raise vbObjectError + 1007, "f_TransferCell_WithMap", _
                      "マッピング表の" & mapRowNo & "行目：行・列は、1 以上の数字で書いてください。"
        End If

        srcRow = CLng(vMapRng.Cells(i, vColSrcRow).Value)
        srcCol = CLng(vMapRng.Cells(i, vColSrcCol).Value)
        dstRow = CLng(vMapRng.Cells(i, vColDstRow).Value)
        dstCol = CLng(vMapRng.Cells(i, vColDstCol).Value)

        If srcRow < 1 Or srcCol < 1 Or dstRow < 1 Or dstCol < 1 Then
            Err.Raise vbObjectError + 1007, "f_TransferCell_WithMap", _
                      "マッピング表の" & mapRowNo & "行目：行・列は、1 以上の数字で書いてください" & _
                      "（転記元の行=" & srcRow & "、列=" & srcCol & "、転記先の行=" & dstRow & "、列=" & dstCol & "）。"
        End If

        If srcRow > vSrcWb.Worksheets(srcSheetName).Rows.Count Or srcCol > vSrcWb.Worksheets(srcSheetName).Columns.Count Or _
           dstRow > vDstWb.Worksheets(dstSheetName).Rows.Count Or dstCol > vDstWb.Worksheets(dstSheetName).Columns.Count Then
            Err.Raise vbObjectError + 1009, "f_TransferCell_WithMap", _
                      "マッピング表の" & mapRowNo & "行目：行・列が、シートの上限を超えています" & _
                      "（転記元の行=" & srcRow & "、列=" & srcCol & "、転記先の行=" & dstRow & "、列=" & dstCol & "）。"
        End If

    Next i

    '---------------------------------------------
    ' ■エラー時は ErrHandler へ（False を返す） / On error, jump to ErrHandler (returns False)
    '---------------------------------------------
    On Error GoTo ErrHandler

    '---------------------------------------------
    ' ■マッピング表の行ループ（1行ごとに、代入_セルで転記） / Mapping row loop (transfer each row with AssignCell)
    '---------------------------------------------
    For i = 1 To mapRowCount

        mapRowNo = vMapRng.Row + i - 1

        srcSheetName = CStr(vMapRng.Cells(i, vColSrcSheet).Value)
        dstSheetName = CStr(vMapRng.Cells(i, vColDstSheet).Value)
        If StrComp(srcSheetName, cActiveSheet, vbTextCompare) = 0 Then srcSheetName = srcActiveName
        If StrComp(dstSheetName, cActiveSheet, vbTextCompare) = 0 Then dstSheetName = dstActiveName

        srcRow = CLng(vMapRng.Cells(i, vColSrcRow).Value)
        srcCol = CLng(vMapRng.Cells(i, vColSrcCol).Value)
        dstRow = CLng(vMapRng.Cells(i, vColDstRow).Value)
        dstCol = CLng(vMapRng.Cells(i, vColDstCol).Value)

        If f_AssignCell(vSrcWb, srcSheetName, srcRow, srcCol, _
                        vDstWb, dstSheetName, dstRow, dstCol) = False Then
            Debug.Print "f_TransferCell_WithMap: マッピング表の" & mapRowNo & "行目で、転記に失敗したため、止めました。"
            Exit Function                                       'False のまま返す / return False as it is
        End If

    Next i

    f_TransferCell_WithMap = True
    Exit Function

ErrHandler:
    Debug.Print "f_TransferCell_WithMap: " & Err.Number & " " & Err.Description & "（発生元：" & Err.Source & "）"
    f_TransferCell_WithMap = False

End Function
