Adding SUM function to VBA Macro including Sheet Name

3LydonsOnAShirt

New Member
Joined
May 28, 2012
Messages
10
Hi there,

In a workbook that has multiple worksheets, I have an existing macro that selects data from specific cells in a specific worksheet. I'd like to add to this, and have the macro SUM a selection of cells within the specified worksheet.

I have tried adding:
Range("K" & tempCount).Value = bkTempPack.Sheets("1.Current_Month_Payment_YTDa").Application.WorksheetFunction.Sum(Range("W50:AS50"))

However, it didn't return the results I expected, which I think might be because it is not picking up the Worksheet, and selecting the range from the first sheet in the workbook.

Any ideas how I can adjust this??? My macro (excluding my SUM line) is below:
Sub Extract_Solution_Consultants()
'***********************************************************************************************************************************
' PURPOSE:
' INPUTS:
' OUTPUTS:
'
'***********************************************************************************************************************************


'HOUSEKEEPING ************************************************************************************************************************
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'************************************************************************************************************************************


'MAPPING ***************************************************************************************************************************
'Read & write paths
Const importPath = "I:\Everyone\Enterprise CSO\Commissions\Statements Macro\Annie's Statements\"
'*************************************************************************************************************************************


'EXCTRACT DATA FROM PACK(S) ***********************************************************************************************************************
Dim tempBookName As String
Dim tempCount As Long

Dim tempReportRecordCount As Long
Dim tempPackRecordCount As Long
Dim tempHeader As String
Dim refColumn As String


tempCount = 2
tempBookName = Dir(importPath)

Do While tempBookName <> ""

'Open book
Set bkTempPack = Workbooks.Open(Filename:=importPath & tempBookName, UpdateLinks:=0)

'Grab data
ThisWorkbook.Sheets("Extraction - SME SCs").Activate
Range("A" & tempCount).Value = bkTempPack.Sheets("1.Current_Month_Payment_YTDa").Range("C3").Value
Range("B" & tempCount).Value = bkTempPack.Sheets("1.Current_Month_Payment_YTDa").Range("C4").Value
Range("C" & tempCount).Value = bkTempPack.Sheets("1.Current_Month_Payment_YTDa").Range("C5").Value
Range("D" & tempCount).Value = bkTempPack.Sheets("1.Current_Month_Payment_YTDa").Range("C6").Value
Range("E" & tempCount).Value = bkTempPack.Sheets("1.Current_Month_Payment_YTDa").Range("B17").Value
Range("F" & tempCount).Value = bkTempPack.Sheets("1.Current_Month_Payment_YTDa").Range("E15").Value
Range("G" & tempCount).Value = bkTempPack.Sheets("1.Current_Month_Payment_YTDa").Range("E16").Value
Range("H" & tempCount).Value = bkTempPack.Sheets("1.Current_Month_Payment_YTDa").Range("G16").Value
Range("I" & tempCount).Value = bkTempPack.Sheets("1.Current_Month_Payment_YTDa").Range("L16").Value
Range("J" & tempCount).Value = bkTempPack.Sheets("1.Current_Month_Payment_YTDa").Range("O47").Value
Range("K" & tempCount).Value = tempBookName

'Close pack
bkTempPack.Close SaveChanges:=False

'Move to next file
tempBookName = Dir

tempCount = tempCount + 1
Loop
'************************************************************************************************************************************


'HOUSEKEEPING **************************************************************************************************************************
MsgBox ("Done!")
'****************************************************************************************************************************************

End Sub

 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
try:
Code:
Range("K" & tempCount).Value = WorksheetFunction.Sum(Sheets("1.Current_Month_Payment_YTDa").Range("W50:AS50"))
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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