PC Review


Reply
Thread Tools Rate Thread

Code not working as expected

 
 
Wes_A
Guest
Posts: n/a
 
      20th May 2010
Excel 2007, XP Pro.
Once agin I request your much appreciated assistance with an Excel "funny":

The following code appears to run and the rest of the macro runs without any
errors. However the data is NOT pasted into the cells D14 and D8 respectively.
If I step through using F8 then it works, but when the macro is run using
the control button it does not. No error message, just does not paste the
data.

Help!!!

Windows("SCHOOL_MAIN_MENU.xlsm").Activate
Range("K33:L33").Select
Application.CutCopyMode = False
Selection.Copy
Windows("SCHOOL_ID.xlsm").Activate
Sheets("Sheet1").Activate
Range("D14").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Windows("SCHOOL_MAIN_MENU.xlsm").Activate
Range("M41").Select
Application.CutCopyMode = False
Selection.Copy
Windows("SCHOOL_ID.xlsm").Activate
Sheets("Sheet1").Activate
Range("D8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
 
Reply With Quote
 
 
 
 
Mike H
Guest
Posts: n/a
 
      20th May 2010
Hi,

I suspect the issue is that your not qualifying the ranges correctly in the
source workbook (SCHOOL_MAIN_MENU.xlsm) by not specifying the sheet to copy
from. Try this ammended and simplified code that assumes sheet1 in the source
workbook.

Windows("SCHOOL_MAIN_MENU.xlsm").Activate
Sheets("Sheet1").Range("K33:L33").Copy
Windows("SCHOOL_ID.xlsm").Activate
Sheets("Sheet1").Range("D14").PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("SCHOOL_MAIN_MENU.xlsm").Activate
Sheets("Sheet1").Range("M41").Copy
Windows("SCHOOL_ID.xlsm").Activate
Sheets("Sheet1").Range("D8").PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"Wes_A" wrote:

> Excel 2007, XP Pro.
> Once agin I request your much appreciated assistance with an Excel "funny":
>
> The following code appears to run and the rest of the macro runs without any
> errors. However the data is NOT pasted into the cells D14 and D8 respectively.
> If I step through using F8 then it works, but when the macro is run using
> the control button it does not. No error message, just does not paste the
> data.
>
> Help!!!
>
> Windows("SCHOOL_MAIN_MENU.xlsm").Activate
> Range("K33:L33").Select
> Application.CutCopyMode = False
> Selection.Copy
> Windows("SCHOOL_ID.xlsm").Activate
> Sheets("Sheet1").Activate
> Range("D14").Select
> Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
> SkipBlanks _
> :=False, Transpose:=False
> Windows("SCHOOL_MAIN_MENU.xlsm").Activate
> Range("M41").Select
> Application.CutCopyMode = False
> Selection.Copy
> Windows("SCHOOL_ID.xlsm").Activate
> Sheets("Sheet1").Activate
> Range("D8").Select
> Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
> SkipBlanks _
> :=False, Transpose:=False

 
Reply With Quote
 
Jacob Skaria
Guest
Posts: n/a
 
      20th May 2010
Try this instead which uses the worksheet object


Sub Macro()
Dim ws1 As Worksheet, ws2 As Worksheet

Set ws1 = Workbooks("SCHOOL_MAIN_MENU.xlsm").ActiveSheet
Set ws2 = Workbooks("SCHOOL_ID.xlsm").Sheets("Sheet1")

ws2.Range("D14").Resize(, 2) = Range("K33:L33").Value
ws2.Range("D8") = ws1.Range("M41").Value

End Sub

OR

'using the copy>PasteSpecial
ws1.Range("K33:L33").Copy
ws2.Range("D14").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False

--
Jacob (MVP - Excel)


"Wes_A" wrote:

> Excel 2007, XP Pro.
> Once agin I request your much appreciated assistance with an Excel "funny":
>
> The following code appears to run and the rest of the macro runs without any
> errors. However the data is NOT pasted into the cells D14 and D8 respectively.
> If I step through using F8 then it works, but when the macro is run using
> the control button it does not. No error message, just does not paste the
> data.
>
> Help!!!
>
> Windows("SCHOOL_MAIN_MENU.xlsm").Activate
> Range("K33:L33").Select
> Application.CutCopyMode = False
> Selection.Copy
> Windows("SCHOOL_ID.xlsm").Activate
> Sheets("Sheet1").Activate
> Range("D14").Select
> Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
> SkipBlanks _
> :=False, Transpose:=False
> Windows("SCHOOL_MAIN_MENU.xlsm").Activate
> Range("M41").Select
> Application.CutCopyMode = False
> Selection.Copy
> Windows("SCHOOL_ID.xlsm").Activate
> Sheets("Sheet1").Activate
> Range("D8").Select
> Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
> SkipBlanks _
> :=False, Transpose:=False

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code not working as expected. Ayo Microsoft Excel Misc 2 19th May 2008 07:08 PM
Help !!! My code is not working as expected. =?Utf-8?B?QXlv?= Microsoft Excel Misc 3 30th Aug 2007 10:39 PM
Avoiding page breaks across merged cells - Code not working as expected Alan Microsoft Excel Programming 2 26th Sep 2005 03:55 AM
IIf not working as expected DEVOURU Microsoft Access Reports 1 18th Mar 2004 05:56 PM
= and <> not working as expected Bill Lentz Microsoft Access Getting Started 3 19th Jan 2004 07:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:16 AM.