質問

These lines are intended to copy a range of one line cells from a sheet (source) to another (target) on the same workbook.

The following code raises error 1004 - Application-defined or object-defined :

currBook.Sheets(strNomOnglet).Range(Cells(firstrow, 2), Cells(firstrow, 8)).Copy _Destination:=currBook.Sheets(strOngletCibl).Range("B1:H1")

firstrow variable is a seeking sequence result.

This one is ok but too static (not relative referencing):

currBook.Sheets(strNomOnglet).Range("B26:H26").Copy _Destination:=currBook.Sheets(strOngletCibl).Range("B1:H1")

I'd like to avoid using a loop.

Any tips or explanation for that issue?

役に立ちましたか?

解決

Use this one instead:

With currBook.Sheets(strNomOnglet)
    .Range(.Cells(firstrow, 2), .Cells(firstrow, 8)).Copy _
        Destination:=currBook.Sheets(strOngletCibl).Range("B1:H1")
End With

note that I've changed Cells(firstrow, 2) to .Cells(firstrow, 2) (to fully qualify your cells, i.e. specify to which sheet they are belongs)

Also if you want to copy only values (without formatting), it's better to use following code:

currBook.Sheets(strOngletCibl).Range("B1:H1").Value = _
    currBook.Sheets(strNomOnglet).Cells(firstrow, 2).Resize(, 7).Value

where .Cells(firstrow, 2).Resize(, 7) is shorter version of .Range(.Cells(firstrow, 2), .Cells(firstrow, 8))

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top