VBA Code to Activate and Write to Sheet in Another Workbook

Gos-C

Active Member
Joined
Apr 11, 2005
Messages
258
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi all,

I have the following code in a common workbook, TestMacro, (i.e., one used by several users) which I need to use in different workbooks:

Code:
Sub TotalPaidClaims()
Dim cRange As Range, pClaims As Long
    Set cRange = Range("EE2:EE" & Cells(Rows.Count, 1).End(xlUp).Row)
    pClaims = Application.WorksheetFunction.CountIf(cRange, "Paid")
    Range("EH2") = pClaims
    Range("EH1") = "Paid Claims"
    Range("EG1") = "Select Type"
    Range("EK1") = "Risk Based Selected"
    Range("EJ1") = "Risk Based Minimum"
    Range("EI1") = "SVS"
    Range("EL1") = "Random Selected"
End Sub

The above ranges are in worksheet 01Apr2011, which is in a workbook (named April2011) - and is opened.

However, when I run the macro, it is reading and writing to Sheet1 of the TestMacro workbook instead of 01Apr2011 of the April2011 workbook.

Can any help me with getting the 01Apr2011 sheet to be the active sheet, please.

Thank you,
Gos-C
 
What exactly do you mean by "so that it executes in another workbook"?

If you use the SelectionChange event, the code has to be in the code module of the sheet that you want to trigger the code, but the data itself can be anywhere as long as you specify which workbook and worksheet you are trying to manipulate in the code.
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,224,505
Messages
6,179,152
Members
452,891
Latest member
JUSTOUTOFMYREACH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top