#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 237
Default Paste Code

Below is my code...

First the user will always copy something on a spreadsheet
and then they will click the button that has the below
code in it(stored in personal.xls).

What the code does is pastes the contents of the clipboard
in cell C5 of sheet1 on workbook P:\Stats Manager.xls. If
the workbook is open the code works perfect. If P:\Stats
Manager.xls is not open, the code then opens the file but
then I get the error "Paste method of Worksheet Class
Failed.". I have to go back to the source
workbook/worksheet and hit copy and then I run the code
again and it works (because P:\Stats Manager.xls is open)

Can anyone please help me?

Thanx

Sub ImportData()
'
' ImportData Macro
' Macro recorded 12/1/2003 by SEGeneric
'

'
Dim wb As Workbook
If MsgBox("Would you like to Import Data?", vbYesNo) =
vbNo Then
Exit Sub
End If
On Error Resume Next
Set wb = Workbooks("Stats Manager.xls")
On Error GoTo 0
If wb Is Nothing Then Set wb = Workbooks.Open("P:\Stats
Manager.xls")

With Worksheets(1)
Set rng = .Range("A4:Z100")
Set rng1 = .Range("A4:AZ4")
End With
res = Application.Match("Ext", rng1, 0)
If Not IsError(res) Then
Set rng2 = rng1(1, res)
rng.Sort Key1:=rng2, Order1:=xlAscending, _
Header:=xlYes, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom
End If


'***Start of Paste Code
'wb.Worksheets(1).Range("C5").Select
Application.Goto reference:=wb.Worksheets(1).Range("C5")
ActiveSheet.Paste Destination:=Worksheets(1).Range("C5")
'ActiveSheet.Paste


Range("B5:AZ100").Select
Range("AZ5").Activate
With Selection
.HorizontalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
End With
Range("A2:AZ3").Select

MsgBox "Data Imported Successfully!", vbOKOnly

If MsgBox("Would you like to Save Workbook?", vbYesNo) =
vbNo Then
Exit Sub
End If
ActiveWorkbook.Save

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 237
Default Paste Code

Hey guys,

I think I got it. I made the following modifications to
it and now it seems to be working great.

Thanx
Todd

Sub ImportData()
'
' ImportData Macro
' Macro recorded 12/1/2003 by SEGeneric
'

'
Dim wb As Workbook
If MsgBox("Would you like to Import Data?", vbYesNo) =
vbNo Then
Exit Sub
End If
On Error Resume Next
Set wb = Workbooks("Stats Manager.xls")
On Error GoTo 0

If wb Is Nothing Then
Set wb = Workbooks.Open("P:\Stats Manager.xls")
ActiveWindow.WindowState = xlMinimized
Selection.Copy
End If

With Worksheets(1)
Set rng = .Range("A4:Z100")
Set rng1 = .Range("A4:AZ4")
End With
res = Application.Match("Ext", rng1, 0)
If Not IsError(res) Then
Set rng2 = rng1(1, res)
rng.Sort Key1:=rng2, Order1:=xlAscending, _
Header:=xlYes, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom
End If


'***Start of Paste Code
'wb.Worksheets(1).Range("C5").Select
Application.Goto reference:=wb.Worksheets(1).Range("C5")
ActiveSheet.Paste Destination:=Worksheets(1).Range("C5")
'ActiveSheet.Paste


Range("B5:AZ100").Select
Range("AZ5").Activate
With Selection
.HorizontalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
End With
Range("A2:AZ3").Select

MsgBox "Data Imported Successfully!", vbOKOnly

If MsgBox("Would you like to Save Workbook?", vbYesNo) =
vbNo Then
Exit Sub
End If
ActiveWorkbook.Save

End Sub

-----Original Message-----
Below is my code...

First the user will always copy something on a

spreadsheet
and then they will click the button that has the below
code in it(stored in personal.xls).

What the code does is pastes the contents of the

clipboard
in cell C5 of sheet1 on workbook P:\Stats Manager.xls.

If
the workbook is open the code works perfect. If P:\Stats
Manager.xls is not open, the code then opens the file but
then I get the error "Paste method of Worksheet Class
Failed.". I have to go back to the source
workbook/worksheet and hit copy and then I run the code
again and it works (because P:\Stats Manager.xls is open)

Can anyone please help me?

Thanx

Sub ImportData()
'
' ImportData Macro
' Macro recorded 12/1/2003 by SEGeneric
'

'
Dim wb As Workbook
If MsgBox("Would you like to Import Data?", vbYesNo) =
vbNo Then
Exit Sub
End If
On Error Resume Next
Set wb = Workbooks("Stats Manager.xls")
On Error GoTo 0
If wb Is Nothing Then Set wb = Workbooks.Open("P:\Stats
Manager.xls")

With Worksheets(1)
Set rng = .Range("A4:Z100")
Set rng1 = .Range("A4:AZ4")
End With
res = Application.Match("Ext", rng1, 0)
If Not IsError(res) Then
Set rng2 = rng1(1, res)
rng.Sort Key1:=rng2, Order1:=xlAscending, _
Header:=xlYes, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom
End If


'***Start of Paste Code
'wb.Worksheets(1).Range("C5").Select
Application.Goto reference:=wb.Worksheets(1).Range("C5")
ActiveSheet.Paste Destination:=Worksheets(1).Range("C5")
'ActiveSheet.Paste


Range("B5:AZ100").Select
Range("AZ5").Activate
With Selection
.HorizontalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
End With
Range("A2:AZ3").Select

MsgBox "Data Imported Successfully!", vbOKOnly

If MsgBox("Would you like to Save Workbook?", vbYesNo) =
vbNo Then
Exit Sub
End If
ActiveWorkbook.Save

End Sub

.

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
copy and paste using vb code ASU Excel Discussion (Misc queries) 1 September 10th 06 01:41 AM
How to paste html code to xls pm Excel Discussion (Misc queries) 0 January 21st 06 08:22 PM
(Cut - Paste in Excel )code in vc++ Allen New Users to Excel 0 June 23rd 05 09:47 AM
Where do I paste this Code? Kevin Excel Programming 5 October 22nd 03 04:08 AM
Need Help with Code - Copy & Paste JStone0218 Excel Programming 10 October 11th 03 04:09 PM


All times are GMT +1. The time now is 07:06 AM.

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

About Us

"It's about Microsoft Excel"