0

I'm new to Visual Basic on Excel and I've been struggling trying to copy one cell to another on a different sheet. For example, if Sheet1 has the following:

Animal    Owner
Dog       John
Cat       Gabe

And Sheet2 is simply blank, assuming Animal and Owner are in different columns and are in columns A and B respectively, I just wanted to copy Dog into A2 (Animal is in cell A1) onto sheet two, for example.

I tried to look things up online and tried:

Dim dataSheet As Worksheet
Set dataSheet = ThisWorkbook.Sheets("Sheet1")
Dim DestinationSheet As Worksheet
Set DestinationSheet = ThisWorkbook.Sheets("Sheet2")

dataSheet.Range("A2").Copy DestinationSheet.Range("A2")

But I keep getting an error saying that:

Run-time error '1004': Method 'Range' of object '_Worksheet' failed.

I just want to copy from one cell to another onto a different worksheet. If anyone has any idea to do so, that would be great! Thanks!

4

2 に答える 2

0

これは、ループの必要性を取り除く例です。これは、より信頼性が高く、より高速に実行する必要があります。

Sub Sample()

Dim lngSh1LastARow As Long

'Get last row in Sheet1 Column A
lngSh1LastARow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

'Select the same range but on sheet 2
With Sheets("Sheet5").Range("A1:A" & lngSh1LastARow)

    'Set sheet2's A column = to Sheet 1 A column
    .FormulaR1C1 = "=Sheet1!RC"

    'Remove all formulas from Sheet2 column A and retain just the value
    'Note: This step is optional If sheet1 Column A has formulas and you would
    'like to retain those formulas on sheet2 as well simply delete or 
    'Comment out the next line
    .Value = .Value

End With

End Sub
于 2013-07-03T21:52:02.413 に答える