Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 14th, 2005, 01:22 PM
Registered User
 
Join Date: Mar 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default workbook problem

HI! I've got code

Sub Transpose()
'declarations

Workbooks.Add

Set destination = ActiveWorkbook.Sheets(1)
Set difference = ActiveWorkbook.Sheets(2)

ThisWorkbook.Activate

  'Code to transpose data.it's a loop

'Then another code.

Set Target_Cell = destination.Range("C1")
Set Source_Cell = difference.Range("c1")

Const COMPANY_OFFSET = 3
Const COMPANY_COUNT = 3
For j = 1 To COMPANY_COUNT ' Loop through companies
  'Set range of Source from top to next to bottom of values
  Set Source = Range(Source_Cell, Source_Cell.End(xlDown).Offset(-1, 0))
  Set Target = Target_Cell
  Source.Select
  For Each cell In Source
    Target.Value = cell.Offset(1, 0) - cell.Value
    Set Target = Target.Offset(1, 0) ' Move target to next row
  Next
  Set Source_Cell = Source_Cell.Offset(0, COMPANY_OFFSET)
  Set Target_Cell = Target_Cell.Offset(0, COMPANY_OFFSET)
Next j

End Sub

My problem is,the 2nd code suppose to refer to the new workbook that has been added but it refer to the current activeworkbook. PLease help.

 
Old March 14th, 2005, 01:45 PM
Authorized User
 
Join Date: Jun 2003
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What do you mean by 2nd code. Which variable is giving the problem?

 
Old March 14th, 2005, 02:19 PM
Registered User
 
Join Date: Mar 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

there are 2 code there. I did not put the 1st code bcos it works fine. The one I showed above is the one giving me probs. The one I bold is giving me probs.

 
Old March 14th, 2005, 04:09 PM
Authorized User
 
Join Date: Jun 2003
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Now I understand the problem.
What is happening is that you create a new workbook and then variables that point to the workbook:

Workbooks.Add

Set destination = ActiveWorkbook.Sheets(1)
Set difference = ActiveWorkbook.Sheets(2)

Next you activate the original workbook:

ThisWorkbook.Activate

and then try to select a range in the new workbook when it is no longer active.

Source.select

You have to first activate the worksheet before doing the select

Source.Parent.Activiate
Source.select

Enjoy!

Barry


 
Old March 15th, 2005, 08:03 AM
Registered User
 
Join Date: Mar 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi, thanks barry, that solve the problem! :D






Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I call a sub in a different workbook? TomW Excel VBA 1 April 12th, 2007 10:34 AM
Workbook Open anup.bihani Excel VBA 2 November 30th, 2006 01:05 AM
Inactive Workbook halem2 Beginning VB 6 0 June 8th, 2006 04:42 PM
Shared workbook. seasider Excel VBA 0 January 20th, 2006 12:02 PM
workbook not found! timoma Access VBA 2 January 17th, 2005 08:47 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.