VBA - Formula to reference another workbook

KaaaK

New Member
Joined
Feb 21, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hey everyone,

I have a Formula code to caluclate the average of a column, ignoring all zeros that works, see below.

Now I want that formula to reference to another closed "Source workbook", not the workbook the makro is in.
I already achieved copying from that "Source workbook" and pasting into my makro workbook
But I want to immediatly apply the formula onto the data in the "Soure workbook" without needing to copy the data.



VBA Code:
How can I adress the "Source Workbook" with my Formula?


The Formula Code:

Sub Formula_Active_Row()
Dim r As Long, rr As Long, c As Long
Dim wrng As String

r = 2
rr = Cells(Rows.Count, "D").End(xlUp).Row
wrng = "$D$" & r & ":$D$" & rr
Cells(ActiveCell.Row, 4).Formula = "=AVERAGEIF(" & wrng & ","">0""," & wrng & ")"

End Sub






All help is much appreciated
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi,

You can try this :

VBA Code:
Option Explicit

Dim WB As Workbook
Dim WS As Worksheet
Dim r, rr, c As Long 'Note here, you don't need to re-declare as Long every time
Dim wrng As String

Sub Formula_Active_Row()
Set WB = Workbooks("Book2.xlsx") 'Change WB name here. Assuming the WB is in the same folder. If not, you will have to paste to path
Set WS = WB.Worksheets("Sheet1")  'Change sheet name here. 


r = 2
rr = Cells(Rows.Count, "D").End(xlUp).Row
wrng = "$D$" & r & ":$D$" & rr

WS.Cells(ActiveCell.Row, 4).Formula = "=AVERAGEIF(" & wrng & ","">0""," & wrng & ")"


End Sub

Hope it helps.
Regards,
 
Upvote 0

Forum statistics

Threads
1,215,511
Messages
6,125,250
Members
449,218
Latest member
daynle

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