Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 275
Default VB Complie error - can You Help

I have this code which copies data input from 'Adhoc' worksheets cells G9:G15
and copies them onto next available row in 'database' worksheet, column A

Sub copydata()
Application.ScreenUpdating = False
Sheets("Adhoc").Range("G9:G15").Copy
Sheets("database").Select
Range("A65536").End(xlUp).Activate
PasteSpecial = xlValues
Sheets("Adhoc").Activate
Range("G9:G15").ClearContents
Range("G9").Select
MsgBox "Copied to Database Sheet"
Application.ScreenUpdating = True
End Sub

However when I try it, the following bit is highlighted yellow

PasteSpecial = xlValues

and a 'compile error' - Expected function or variable

as I'm the novice (but getting better) anybody fix it for me ??

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default VB Complie error - can You Help

If you record a macro when you do Edit|paste special|values, you'll see code
like:


Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False

And your line doesn't have that range in front of the pastespecial command.

But I think you may have a problem.

You have:
Range("A65536").End(xlUp).Activate
This takes you up to the last used cell in column A. That means you could be
overwriting anything that was in that cell/row.

You could do something like:

Option Explicit

Sub copydata2()
Application.ScreenUpdating = False
Sheets("Adhoc").Range("G9:G15").Copy
Sheets("database").Select
With Range("A65536").End(xlUp).Offset(1, 0)
.Activate
.PasteSpecial = xlValues
End With
Sheets("Adhoc").Activate
Range("G9:G15").ClearContents
Range("G9").Select
MsgBox "Copied to Database Sheet"
Application.ScreenUpdating = True
End Sub

But since you're just pasting values, you could avoid the .select's and
..activate's.

Option Explicit
Sub copydata3()

Dim RngToCopy As Range
Dim DestCell As Range

Application.ScreenUpdating = False

Set RngToCopy = Worksheets("adhoc").Range("G9:G15")

With Worksheets("database")
Set DestCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
End With

RngToCopy.Copy _
Destination:=DestCell

RngToCopy.ClearContents

MsgBox "Copied to Database Sheet"

Application.ScreenUpdating = True
End Sub

Another way is to just assign the values to that destination range:

Option Explicit
Sub copydata3A()

Dim RngToCopy As Range
Dim DestCell As Range

Application.ScreenUpdating = False

Set RngToCopy = Worksheets("adhoc").Range("G9:G15")

With Worksheets("database")
Set DestCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
End With

DestCell.Resize(RngToCopy.Rows.Count, RngToCopy.Columns.Count).Value _
= RngToCopy.Value

RngToCopy.ClearContents

MsgBox "Copied to Database Sheet"

Application.ScreenUpdating = True

End Sub

Anthony wrote:

I have this code which copies data input from 'Adhoc' worksheets cells G9:G15
and copies them onto next available row in 'database' worksheet, column A

Sub copydata()
Application.ScreenUpdating = False
Sheets("Adhoc").Range("G9:G15").Copy
Sheets("database").Select
Range("A65536").End(xlUp).Activate
PasteSpecial = xlValues
Sheets("Adhoc").Activate
Range("G9:G15").ClearContents
Range("G9").Select
MsgBox "Copied to Database Sheet"
Application.ScreenUpdating = True
End Sub

However when I try it, the following bit is highlighted yellow

PasteSpecial = xlValues

and a 'compile error' - Expected function or variable

as I'm the novice (but getting better) anybody fix it for me ??

Thanks


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 275
Default VB Complie error - can You Help

Thanks Dave,
I'll give them a try

"Dave Peterson" wrote:

If you record a macro when you do Edit|paste special|values, you'll see code
like:


Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False

And your line doesn't have that range in front of the pastespecial command.

But I think you may have a problem.

You have:
Range("A65536").End(xlUp).Activate
This takes you up to the last used cell in column A. That means you could be
overwriting anything that was in that cell/row.

You could do something like:

Option Explicit

Sub copydata2()
Application.ScreenUpdating = False
Sheets("Adhoc").Range("G9:G15").Copy
Sheets("database").Select
With Range("A65536").End(xlUp).Offset(1, 0)
.Activate
.PasteSpecial = xlValues
End With
Sheets("Adhoc").Activate
Range("G9:G15").ClearContents
Range("G9").Select
MsgBox "Copied to Database Sheet"
Application.ScreenUpdating = True
End Sub

But since you're just pasting values, you could avoid the .select's and
..activate's.

Option Explicit
Sub copydata3()

Dim RngToCopy As Range
Dim DestCell As Range

Application.ScreenUpdating = False

Set RngToCopy = Worksheets("adhoc").Range("G9:G15")

With Worksheets("database")
Set DestCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
End With

RngToCopy.Copy _
Destination:=DestCell

RngToCopy.ClearContents

MsgBox "Copied to Database Sheet"

Application.ScreenUpdating = True
End Sub

Another way is to just assign the values to that destination range:

Option Explicit
Sub copydata3A()

Dim RngToCopy As Range
Dim DestCell As Range

Application.ScreenUpdating = False

Set RngToCopy = Worksheets("adhoc").Range("G9:G15")

With Worksheets("database")
Set DestCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
End With

DestCell.Resize(RngToCopy.Rows.Count, RngToCopy.Columns.Count).Value _
= RngToCopy.Value

RngToCopy.ClearContents

MsgBox "Copied to Database Sheet"

Application.ScreenUpdating = True

End Sub

Anthony wrote:

I have this code which copies data input from 'Adhoc' worksheets cells G9:G15
and copies them onto next available row in 'database' worksheet, column A

Sub copydata()
Application.ScreenUpdating = False
Sheets("Adhoc").Range("G9:G15").Copy
Sheets("database").Select
Range("A65536").End(xlUp).Activate
PasteSpecial = xlValues
Sheets("Adhoc").Activate
Range("G9:G15").ClearContents
Range("G9").Select
MsgBox "Copied to Database Sheet"
Application.ScreenUpdating = True
End Sub

However when I try it, the following bit is highlighted yellow

PasteSpecial = xlValues

and a 'compile error' - Expected function or variable

as I'm the novice (but getting better) anybody fix it for me ??

Thanks


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Complie Error- Argument not optional Roberta Excel Programming 5 April 4th 05 02:31 PM
Complie Error Help JMay Excel Programming 4 January 24th 05 04:33 AM
complie error brian Excel Programming 2 December 13th 04 06:51 PM
Complie Error chris huber Excel Programming 3 January 14th 04 09:57 AM
Variable produces a complie error Btinker Excel Programming 1 November 26th 03 07:53 PM


All times are GMT +1. The time now is 02:32 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"