2

あきらめる。このマクロが機能しない理由を理解するために4時間を費やしました。For指定されたソース範囲を取得し、ループを使用して循環し、値を別の列にコピーする必要があります。

指定された宛先セルから開始して、

  • 値を入力してください
  • 挿入を使用して新しい行を作成します(その列に挿入するだけでなく、行全体。既存の行セット内にデータを収めたい)
  • 宛先列の終点を指定するマーカーをオーバーライドしないでください。その下に保持する必要のあるデータがあります。

理由がわからない

  • プロシージャの1つのインスタンスでは、値を入力しますが、次の行を挿入するときにデータを消去します。
  • 2番目の例では、行をスキップして列の終わりマーカーを消去します

私は問題に対する巧妙でエレガントな解決策を探しているのではないことに注意してくださいパラダイムを自分自身に教えるために、物事を本当に基本的なものに保ちたいと思っています。基本を理解するのが上手になったので、いくつかの高度なトリックを試してみます。

TIA

Sub Macro1()
Dim firstRow As Range, lastRow As Range
Dim source As Range, destination As Range
Dim readCell As Range

Set firstRow = Range("F2")
Set lastRow = Range("F20")
Set destination = Range("A21")


Set source = Range(firstRow.Address(False, False) & ":" & lastRow.Address(False, False))

For Each readCell In source
    destination.Select
    destination.Value = readCell.Value

    If (readCell.Address(False, False) = lastRow.Offset(1, 0)) Then
        Exit For
    Else
       'destination.Select
    End If

    'MsgBox (destination.Value)
    destination.EntireRow.Insert Shift:=xlUp
    Set destination = destination.Offset(1, 0)
Next
End Sub
4

2 に答える 2

1

ここにいくつかのヒントがあります:

それとが単一のセルであることを考えると、ものは必要ありfirstRowません。使用するlastRowAddress

Set source = Range(firstRow,  lastRow)

では、行全体destination.EntireRow.Insert Shift:=xlUpに適用しているため、違いはありません。使用するInsertShift

destination.EntireRow.Insert

挿入された行は上に配置されdestinationdestination下にシフトされます。したがって、forループの最初の反復はこれを行います

  1. destinationに設定A21
  2. 行を挿入し、にシフトdestinationしますA22
  3. desination1行を設定します。A23

次の反復では、元々のデータが上書きされますA22A23

私はあなたが欲しいと思います

Sub Macro1()
    Dim firstRow As Range, lastRow As Range
    Dim destination As Range
    Dim readCell As Range

    Set firstRow = Range("F2")
    Set lastRow = Range("F20")
    Set destination = Range("A21")

    For Each readCell In Range(firstRow, lastRow)
        destination.Value = readCell.Value
        destination.EntireRow.Offset(1, 0).Insert
        Set destination = destination.Offset(1, 0)
    Next
End Sub
于 2012-09-18T08:28:04.230 に答える
1

あなたが理解し、解決したい非常に称賛に値する

固定の宛先からの増分よりも行カウンターを使用する方が簡単です。このマイナーな調整

  • 回避するSelect
  • カウンター、、を使用してlngRow、新しい行と新しい値を制御します

    code

    Sub Macro1()
    Dim readCell As Range
    Dim lngRow As Long
    For Each readCell In Range("F2:F20")
        [a21].Offset(lngRow, 0).EntireRow.Insert Shift:=xlUp
        [a21].Offset(lngRow, 0).Value = readCell.Value
        lngRow = lngRow + 1
    Next
    End Sub
    
于 2012-09-18T08:28:52.773 に答える