VBA: Referencing a cell from a workbook into a module

mkingrey

New Member
Joined
Nov 2, 2005
Messages
49
I have a module and a workbook and need to refenerence a cell from WorkBookA in the VBA of Module1. I searched for a solution on the message board but can't find a similar question.

I need something like this:

Code:
Sub RunReport()

'This code resides in Module1 
Dim ReportDate As String

ReportDate = WorkBookA!K2.Value


End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Is the module in the same project as the workbook?

If so just refer directly to the sheetname containing the cell in question:


Code:
Sub RunReport()

'This code resides in Module1
Dim ReportDate As String

ReportDate = sheets("sheet1").range("K2").Value

' ........and to test if it's working

msgbox ReportDate

End Sub


Just change the "Sheet1" ref to the name of your worksheet containing K2.
 
Upvote 0
Workbook?

ReportDate = Workbooks("WorkBookA.xls").Worksheets("Sheet1").Range("K2").Value

or Worksheet?

ReportDate = Worksheets("WorkBookA").Range("K2").Value
 
Upvote 0

Forum statistics

Threads
1,214,854
Messages
6,121,941
Members
449,056
Latest member
denissimo

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