PC Review


Reply
Thread Tools Rate Thread

Add-in Question (common code)

 
 
chemicals
Guest
Posts: n/a
 
      6th Nov 2008
I have 10 Workbooks that all have identical Modules and VB code functions.

I want to centralize that code in one place so that fixes don't have to be
made 10 times. I created an Add-in with a Module containing all of the
common code. The problem now is that this common code refences the worksheet
cells (which don't exist in the Addin) as ThisWorkbook. I want it to
reference what ever workbook was opened. How do I do this?

So for example the code in the Addin Module1 has:

Set wsh = ThisWorkbook.Sheets("DW")
ThisWorkbook.Sheets("DW").Activate
'Assumes data starts in column D
Set rRng = wsh.Range("D11", "AY11")

On Error GoTo Err_Handle

sContractCode = Worksheets("DW").Range("D5").Value
dStartDate = Worksheets("DW").Range("D7").Value

etc.,etc...


 
Reply With Quote
 
 
 
 
Peter T
Guest
Posts: n/a
 
      6th Nov 2008
ThisWorkbook refers to the workbook that contains the code, your addin

You will need something that's common to all workbooks that's applicable to
the code in your addin. Let's assume it's any workbook that contains a sheet
named "DW" and the code will only be called in the ActiveWorkbook


On Error Resume Next
Set wsh = Nothing ' if there's any possibility wsh is already ref'd
Set wsh = ActiveWorkbook.WorkSheets("DW") ' assuming it's a worksheet
On Error GoTo Err_Handle

If wsh is nothing then
' msgbox "Sheet DW does not exist in activeworkbook
Exit Sub
End if

wsh. Activate ' only to need to present to user

' etc

Regards,
Peter T


"chemicals" <(E-Mail Removed)> wrote in message
news:FE72C914-D78C-45D4-BD67-(E-Mail Removed)...
>I have 10 Workbooks that all have identical Modules and VB code functions.
>
> I want to centralize that code in one place so that fixes don't have to be
> made 10 times. I created an Add-in with a Module containing all of the
> common code. The problem now is that this common code refences the
> worksheet
> cells (which don't exist in the Addin) as ThisWorkbook. I want it to
> reference what ever workbook was opened. How do I do this?
>
> So for example the code in the Addin Module1 has:
>
> Set wsh = ThisWorkbook.Sheets("DW")
> ThisWorkbook.Sheets("DW").Activate
> 'Assumes data starts in column D
> Set rRng = wsh.Range("D11", "AY11")
>
> On Error GoTo Err_Handle
>
> sContractCode = Worksheets("DW").Range("D5").Value
> dStartDate = Worksheets("DW").Range("D7").Value
>
> etc.,etc...
>
>



 
Reply With Quote
 
chemicals
Guest
Posts: n/a
 
      6th Nov 2008
Thanks. I knew that "ThisWorkbook" was referring to the Add-in wb but I also
assumed that the ActiveWorkbook would be the Add-in as well.... I changed
ThisWorkbook to ActiveWorkbook everywhere in the Add-in module and now it
works great...



"Peter T" wrote:

> ThisWorkbook refers to the workbook that contains the code, your addin
>
> You will need something that's common to all workbooks that's applicable to
> the code in your addin. Let's assume it's any workbook that contains a sheet
> named "DW" and the code will only be called in the ActiveWorkbook
>
>
> On Error Resume Next
> Set wsh = Nothing ' if there's any possibility wsh is already ref'd
> Set wsh = ActiveWorkbook.WorkSheets("DW") ' assuming it's a worksheet
> On Error GoTo Err_Handle
>
> If wsh is nothing then
> ' msgbox "Sheet DW does not exist in activeworkbook
> Exit Sub
> End if
>
> wsh. Activate ' only to need to present to user
>
> ' etc
>
> Regards,
> Peter T
>
>
> "chemicals" <(E-Mail Removed)> wrote in message
> news:FE72C914-D78C-45D4-BD67-(E-Mail Removed)...
> >I have 10 Workbooks that all have identical Modules and VB code functions.
> >
> > I want to centralize that code in one place so that fixes don't have to be
> > made 10 times. I created an Add-in with a Module containing all of the
> > common code. The problem now is that this common code refences the
> > worksheet
> > cells (which don't exist in the Addin) as ThisWorkbook. I want it to
> > reference what ever workbook was opened. How do I do this?
> >
> > So for example the code in the Addin Module1 has:
> >
> > Set wsh = ThisWorkbook.Sheets("DW")
> > ThisWorkbook.Sheets("DW").Activate
> > 'Assumes data starts in column D
> > Set rRng = wsh.Range("D11", "AY11")
> >
> > On Error GoTo Err_Handle
> >
> > sContractCode = Worksheets("DW").Range("D5").Value
> > dStartDate = Worksheets("DW").Range("D7").Value
> >
> > etc.,etc...
> >
> >

>
>
>

 
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
Common code generation DBC User Microsoft C# .NET 3 19th Jul 2007 09:55 PM
Is it possible to have a common resources dll for C++ code and VB code? dev15 Microsoft Dot NET Compact Framework 0 5th Jul 2007 05:41 PM
Common Code =?Utf-8?B?U3RldmVH?= Microsoft Access Macros 5 16th Jan 2006 02:11 PM
Best Practices Question - Where do I put common code? wrecker Microsoft ASP .NET 5 14th Oct 2005 12:13 PM
Where To Store Common Code Wayne Wengert Microsoft Dot NET 2 15th Jul 2003 01:21 PM


Features
 

Advertising
 

Newsgroups
 


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