0

あるワークブックから別のワークブックに指名された怒りを貼り付けようとしています。指名された範囲は SourceRange と呼ばれ、その宛先は TargetRange です。

ただし、TargetRange ワークブックの列 c2 にデータが存在する場合、SourceRange を TargetRange の次の Available 列に貼り付ける必要があります。

次のコードは現在、c2 にデータが存在しない場合は SourceRange を黄色でコピーし、データがある場合は緑にします。緑のインスタンスを c2 の次に使用可能な列に貼り付ける必要があるため、d2.

オフセット機能については知っていますが、どこで使用すればよいかわかりません。

 Select Case MasterWorkbook.ActiveSheet.Range("c2") = ""


Case True
' The opened file automatically becomes the new active workbook and active worksheet.

Set SourceRange = ActiveSheet.Range("c2:c26")
Set TargetRange = MasterWorkbook.ActiveSheet.Range("c2:c29")
' Copy cell values one at a time from the source range to the target range.


For Row = 2 To 29
    TargetRange.Cells(Row, 1).Value = SourceRange.Cells(Row, 1).Value
Next

ActiveWorkbook.Close

' Set background colour of target range.
TargetRange.Select
With Selection.Interior
    .ColorIndex = 6
    .Pattern = xlSolid

End With

Case False

' The opened file automatically becomes the new active workbook and active worksheet.

Set SourceRange = ActiveSheet.Range("c2:c26")
Set TargetRange = MasterWorkbook.ActiveSheet.Range("c2:c29")
' Copy cell values one at a time from the source range to the target range.

'Sheets.Add.Name = "workbookname"
For Row = 2 To 29
    TargetRange.Cells(Row, 1).Value = SourceRange.Cells(Row, 1).Value



Next

ActiveWorkbook.Close


' Set background colour of target range.
TargetRange.Select
With Selection.Interior
    .ColorIndex = 10
    .Pattern = xlSolid

End With
End Select
4

1 に答える 1

0

これを試して

Sub test()
     Dim SourceRange As Range
     Dim TargetRange As Range
     ' The opened file automatically becomes the new active workbook and active worksheet.

     Set SourceRange = ActiveSheet.Range("c2:c26")
     Set TargetRange = MasterWorkbook.ActiveSheet.Range("b3")
     If TargetRange.Text <> "" then
         Set TargetRange = TargetRange.End(xlToRight).Offset(0, 1)
     End If
     Set TargetRange = MasterWorkbook.ActiveSheet.Range(TargetRange, TargetRange.Offset(25, 0))
     ' Copy cell values ALL AT ONCE from the source range to the target range.

     SourceRange.Copy
     TargetRange.PasteSpecial xlPasteValues

     ActiveWorkbook.Close

     ' Set background colour of target range.

     With TargetRange.Interior
         .ColorIndex = IIf(TargetRange.Column = 3, 6, 10)
         .Pattern = xlSolid
     End With
End Sub

何が起こっているのかを理解できるように、1 行ずつ見ていくことをお勧めします。

于 2012-12-05T23:18:07.837 に答える