Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel VBA Copy Paste Question

Status
Not open for further replies.

baycolor

MIS
Mar 23, 2005
148
0
0
US
Hi, I'm really new to Excel VBA programming and have the question below. I'm using Excel 2003 on a laptop.

I have the following procedure that is passed an array of worksheet names:

Sub EmptyWorksheets(IndWrkShtArr As Variant)
For i = 1 To UBound(IndWrkShtArr)
Sheets(i).Select
Cells.Select
Selection.ClearContents
Range("A1").Select
RawData.Range("A:A").Copy Destination:=Sheets(i).Range("A1")
Next i
End Sub

Seventh line is all on one line in the Excel editor window with the end looking like...
:=RawData.Range("A2:A3")

RawData is a tab in my workbook.

Each time the code works on a sheet it selects all cells, clears them puts the focus on cell A1 and then attempts to copy the firs row of header data from my raw data tab to the active sheet. The mesage I get is:

Run-time error '424':
Object required

When I click debug the editor highlights line seven of the procedure.

I copied this copy/paste example from a site that said it is more efficient then copy paste via the clipboard (something like):
RawData.Range("A:A").Copy
Sheets(i).Range("A1").paste

I've attempted to change Sheets(i) to IndWrkShtArr(i)

Also attempted to copy and paste into the same worksheet with
RawData.Range("A:A").Copy Destination:=RawData.Range("A2")

I'm not all that familiar with the Excel/Office VBA object model and I don't have time to learn it all right now. Any help is appreciated.

Thanks
 
Try changing this

RawData.Range("A:A").Copy
or
RawData.Range("A:A").Copy Destination:=Sheets(i).Range("A1")

to

sheets("rawdata").range("A:A").copy
and
sheets("RawData").Range("A:A").Copy Destination:=Sheets(i).Range("A1")

ck1999
 


Hi,

Try this...
Code:
Sub EmptyWorksheets(IndWrkShtArr As Variant)
    For i = [b]LBound(IndWrkShtArr)[/b] To UBound(IndWrkShtArr)
      With Sheets([b]IndWrkShtArr(i)[/b])
        .Cells.ClearContents
        RawData.Range("A:A").Copy Destination:=.Range("A1")
      End With
    Next i
End Sub
use the Sheet Name rather that the INDEX, which may not be correct.

Skip,
[sub]
[glasses]I'll be dressed to the nines this week, as I go to have my prostatectomy...
Because, if I'm gonna BE impotent, I want to LOOK impotent![tongue][/sub]
 
Thanks for the help. The following worked and it didn't activate all the sheets unnecessarily as I was doing. Did make one change as I intended to copy a row not a column:

Code:
Sub EmptyWorksheets(IndWrkShtArr As Variant)
    For i = 1 To UBound(IndWrkShtArr)
        With Sheets(IndWrkShtArr(i))
            .Cells.ClearContents
            Sheets("RawData").Range("1:1").Copy Destination:=.Range("A1")
        End With
    Next i
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top