Help with calling SubTotal function for another Worksheet

jjolk

New Member
Joined
Jul 25, 2018
Messages
1
I'd like some assistance with calling the subtotal function on a worksheet(Sheet3) from another one(Sheet1). When I pass the spreadsheet onto my user the following message occurs: Run-time error '1004': Subtotal method of Range class failed.

Let me start by stating that I don't know VBA. I'm trying to help a friend with a request to pull data from SQL (which I know) into Excel. I've cobbled together examples of VBA and it works at times and then throws errors at other times. I'm working on a solution for Excel 2010. The goal of the entire setup is to allow the user to select a name from a combo box which pulls data from SQL(into a hidden Sheet2) for the list. Enter a begin and end date and called a Stored Procedure in SQL with the 3 parameters and return a set of data. I have that working. Next it is desired the that resulting data be subtotaled by the date field in column A and that a blank line be inserted after each subtotal. Even though the code looks a mess, it is all working except that the subtotal call throws an occasional error. I've tried several methods of calling the subtotal, this last one seemed the most stable. Here's the code in which I remove existing Subtotals, Clear the Data from the Worksheet (3), Copy the table from the data retrieval from SQL on Sheet 1, Convert to a Range and then try to subtotal the data.


Private Sub CommandButton2_Click()


Application.ScreenUpdating = False


Worksheets(2).Activate
Cells.RemoveSubtotal

Sheets("Sheet3").UsedRange.ClearContents

Worksheets(1).ListObjects("Table__2GuyTest").Range.Copy _
Destination:=Worksheets(2).Range("A1")

Dim rList As Range

With Worksheets(2).ListObjects(1)
Set rList = .Range
.Unlist ' convert the table back to a range
End With


With Worksheets(2)
.Range("A1", "A50000").NumberFormat = "m/d/yyyy"
End With


Dim ws As Worksheet
For Each ws In Worksheets(Array("Sheet3"))
With ws.Range("A1")
.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(6), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
End With
Next ws


Columns("A:A").EntireColumn.AutoFit
Columns("B:B").EntireColumn.AutoFit
Columns("C:C").EntireColumn.AutoFit
Columns("D:D").EntireColumn.AutoFit
Columns("E:E").EntireColumn.AutoFit
Columns("F:F").EntireColumn.AutoFit


Call InsertBlankRow
Call RemoveRowColors

Application.ScreenUpdating = True


End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Forum statistics

Threads
1,214,819
Messages
6,121,739
Members
449,050
Latest member
excelknuckles

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