(Excel) VBA code to copy to worksheet from pivot table
Hi,
First time posting on this website! Very next to vb and having spent a good few days searching the net for an answer but to no avail. I'm am hoping that someone maybe able to assist with the following query:
I have a pivot table that has two columns and 29 rows. The essential function that i'd like the VBA code to perform is to open each row to a new worksheet and the for each (29) worksheets to be copied and transferred into new workbooks (the workbooks would then be saved a name contained in the cell). I have been able to write a code that performs this function but i cannot get the code to repeat the action for each cell. The code is attached below:
Sub Macro1()
'
' Macro1 Macro
Dim Cell As Range
Dim b As Integer
Dim a As String
Dim d As String
Dim ws As Worksheet
Re: (Excel) VBA code to copy to worksheet from pivot table
See if this gets you close:
Code:
Sub Macro1()
Dim Cell As Range
Dim b As Integer
Dim a As String
Dim d As String
Dim ws As Worksheet
Dim i As Integer '***
Dim strName As String '***
Dim wbNew As Workbook '***
Dim wsNew As Worksheet '***
For i = 2 To 3 'not exactly sure where your data is
Range("b" & i).ShowDetail
strName = Range("d" & i) 'save the value in D2 to name new workbook
Range("b" & i).EntireRow.Copy
Set wbNew = Workbooks.Add
Set wsNew = wbNew.Sheets(1)
wsNew.Range("a1").PasteSpecial
'other code
wbNew.SaveAs Filename:=strName
wbNew.Close
Set wbNew = Nothing
Next i
End Sub
Re: (Excel) VBA code to copy to worksheet from pivot table
Hey vbfbryce,
Thanks for your reply unfort it didn't work and i really don't have the ability to work the coding out at this stage! But it did give me the idea to go back to basics. So i cut all the coding back and I'm just attempting to open an new work book for each cell in the pivot and work my way through the list. The following code works and opens range (B4) then returns to the pivot and opens (B5) in a new worksheet but I don't seem to be able to loop it. The coding has been done via recording of a macro.
Re: (Excel) VBA code to copy to worksheet from pivot table
Hi,
Please accept my apologies for the delay in responding to your email. I have been on annual leave and had no access to the work files. I have slightly alter the names but the pivot table is set up as i would use it.
Re: (Excel) VBA code to copy to worksheet from pivot table
I'm having some trouble figuring out the steps you're going through. Which sheet is active when you start? Which macro do you run (I'm assuming "Macro1")?
Re: (Excel) VBA code to copy to worksheet from pivot table
I've reattached with some amendments. The pivot table is the active sheet. Normally to open the content of the pivot you would just double click the relevant total. So Macro 1 opens B5 in a new work sheet. It should then step one line down and open b6 in a new worksheet. However i cannot make it step down/loop automatically. The only way i've worked out how to do it is by manually entering each line, which is pretty pointless!