Wonder if someone can help me with this one
Using VBA, I have set up a macro which starts by transposing a pivot table into another sheet then touches up the data automatically into a tidy little list for users to set up in a database later.
Now after building it first time for the pivot table it worked a dream and gave me the results I wanted but whenever I update the pivot table and it changes its length, I began to start having problems. Hopefully these screenshots will help:
You dont need to know the info on there but what the macro is expected to do here is transpose the entire pivot which can be done by either double clicking the cross section of the grand totals or right clicking on the cell and select show details of everything. (Highlighted in yellow)
As it stands at the moment the code for getting that cell is set to is:
Range("AA65").Select
Selection.ShowDetail = True
This is where I realized that it is fixed (At the moment to the place highlighted in red) and I have no idea how I can resolve it by making the macro adapt to varying lengths of the pivot table so that it can transpose all of the contents within it. If it helps, the grand total will always be on that particular row its just the column where it ends up that changes.
Is there a way for me to create a code where I can always pick up that Grand Total cell in the pivot no matter how many columns it stretches out to?
Thank you for reading and I hope to get feedback soon :)
Kind Regards
Zoe
for instance
With ActiveSheet.PivotTables(1).TableRange1
.Cells(.Cells.Count).ShowDetail = True
End With
Related
I have a macro that cuts cells out of a table if the entries' date is > 2 weeks old. It then Deletes these cells. However, this causes my formatted area for data entry to shorten each time, so I am trying to find a way to fix this. My idea was to write a piece to copy the formatting from the last formatted row and paste it directly below it, but I am not sure how to go about doing this in VBA. I am newer to VBA and was wondering if anyone knew how to help? Thank you!
It sounds like your goal is to basically have your data entry table ALSO be your summary table, hence wiping out all entries greater than 2 weeks old. This seems like it will end up backfiring on you at some point - for example, what if you wanted to pull a report on everything you entered over the last year?
A better route would seem to be to have a single data entry sheet where you keep EVERYTHING and then a summary display that actively just pulls the data from the last two weeks. For that, you could use VBA to filter your data set based upon the date and then copy the information over to the new sheet (just do it once manually using the filter tool from the Home tab on the ribbon, while recording your actions to a new macro, and you should be able to clean it up pretty easily).
If you MUST edit in place, and presuming you don't mind it being sorted by date, record a macro where you select your full formatted range, turn on filtering, filter by your date column, delete the contents (just use the delete key) of the cells you want to wipe out, and then sort by date on the rest.
The cause of your problem is that in using the range.delete command you are removing the cell from existence (and hence shrinking your formatted range) - you just want to wipe out the contents and then move all your data up to fill the blank space.
good luck!
My previous post seemed to have been unclear so I am posting this again and will try to explain the problem more clearly using screenshots.
https://www.dropbox.com/s/4x8wktbdo7jc21m/excel-screenshot.jpg?dl=0
Now, let me explain the screenshots.
Let's take just one of the raw materials in the table, say Rosin (Column B).
Whenever we buy more rosin, I mention the quanity bought and the date(Column A)
and I have the total quantity of rosin bought at the bottom using the formula "=sum(b80:b91)".
You will also notice that the number of rows at present is 11 i.e. 80 to 91.
But we will still be buying more rosin, even when I have entered the data in these rows. i.e. when I enter the dates and the quantity bought.
The problem is that I'll have to keep inserting more rows in there after every few days.
And if I keep doing that then the column for Rosin(as well as for the other raw materials) will become so long that I'll be scrolling forever.
So, is there a way that I could just enter the amount of rosin bought and the date without having to insert more rows.
Here's a link to the excel sheet that I created - https://www.dropbox.com/s/iod5y7jae6grmyz/brc%20goods%20received%20STOCK11.xlsx?dl=0
If you think MS Excel is not the right tool for this, then please recommend the correct tool for this.
I was learning some python and know some programming basics - just for your information.
P.S. Hope I have been able to explain the problem clearly this time.
I admit that the excel sheet looks very confusing and haphazardly done, but I wanted to start doing the calculations and improve the looks of the sheet later on.
basically, you need to use pivot tables to organize and sum your data.
here is an example sheet done for you to illustrate how it is done in your case.
therefore, you need to put each distinct data type in different sheets. In my example, I have put all of the incoming chemicals into one sheet and created a pivot table for it in another sheet. you need to repeat this for chemicals used in a separate sheet, and create a pivot table in the same/separaate sheet. And finally, in another sheet, you can make a table of total chemicals at present, simply by refering to the data in your pivots tables.
Every time you update your data sheets, come back to its pivot table and click refresh to get the latest and update your data across the table.
you can look up some help across tutorial sites on how to create a pivot table, here is one from microsoft
the best solution here would be not to keep multiple tables which you plan to extend on the same sheet, i looked at your file and it is way too confusing and clogged up, separate all the datasets you have into separate sheets, give good descriptive names to these sheets. I noticed you use total rows below each dataset, what you can do(as long as you are in Excel 2007+), create the dataset and then format it as a table from the menu, which makes your data a lot easier to read and handle, it also gives you the option of total row, more to read up on these tables you can here: Format an Excel table
I have an Excel spreadsheet which contains four data tables on the same tab. The final 3 columns in each contain formulae.
I then have a vba code which performs an sql look up within an SQL Server table and returns a set number of columns to update each of the four tables.
For three of these tables, the data is pasted in and the table automatically expands and the formulae is copied to the end of the table.
However, for the first table, the data is pasted in but the table does not expand and hence, the formulae does not get added to the rows outside of the table.
I have searched high and low for a fix but no joy. I have searched the data extraction VBA code with no luck (the code is an exact match) and no joy in the table properties. I have also checked the Options menu for Auto Format As You Type settings but all seems fine.
Does anyone have any ideas for me?
Thanks,
David.
Hi All again,
Would love to know if anyone has an answer for this but, in the mean time I have fudged the code to automatically adjust the size of the table based on the row number for the last cell of data.
Thanks,
David.
I ran into the same issue and it was driving me nuts. Found out that in a few columns I had dragged formulas down pretty far (they were IF statements to return nothing if the first column was empty) so I didn't see them.
So remove any formulas that are 'under' the last row of the table. Should clear up the issue.
The most common cause is due to differing number of, or mis-aligned columns (vertically stacked tables) or differing number of, or mis-aligned rows (horizontally stacked tables).
For simplicity, let us use vertically stacked tables to illustrate what is happening. Consider an upper table with 2 columns and a lower table with 3. If we try to insert a row to the upper table, XL attempts to push all cells beneath the table down, but ONLY the cells directly beneath the table. So in this case cells below the 2 columns will be pushed down. XL quickly realizes that if it pushes just 2 columns down, the third column in the lower table won't be pushed down resulting in skewing the lower table. XL makes no attempt to figure out how to move the lower tables. It just refuses to even try.
The same holds true for adding columns to tables on the left for which there are tables directly to the right. If adding columns to a table would result in skewing rows of a table to the right, XL refuses to help.
To keep this from being a problem we can do a few things:
Keep the largest tables on top and to the left and align all table first columns and first rows.
If only rows are added to tables, don't stack them vertically
And if only columns are added to tables, don't stack them horizontally.
Herewith is an example of enforcing expansion of a table after data was copied over to the bottom of the table. This is done by resizing the table. Originally the table wasn't auto-resized as it has additional calculation columns.
The first line of the code finds the last row of data and the second line states the range to which the table needs to be resized. The third line resizes the table to the new range.
Replace sXXX and tXXX with your sheet name and table name.
Sub ResizeTableXXX()
myLastrowOfData = sXXX.Range("A1000000").End(xlUp).Row
myNewRangeSize = "$A$5:$P$" & myLastrowOfData
sXXX.ListObjects("tXXX").Resize Range(myNewRangeSize)
End Sub
HIH
Good Day, can somebody please help me. I have 2 spreadsheets, the second spreadsheet has got the copied cells exactly like the first one. But my problem is that on the first spreadsheet I have blank rows as the invoices are being put in date order and not invoices on every day. The second sheet I use for Vat purposes and want my selection there to be in list form in order to only print 1 page for bookkeeper. Where I have invoices for eg. on 10th, 15th and 20th on first spreadsheet "what formula can I use for the 3 invoices to appear in the second spreadsheet eg. in row 1,2,3 one right underneath the other one.
I have done VBA 20 years ago and came right with everything except this I can not figure out for the life of me. Thanks in advance to anybody that can help me with this.
highlight, press F5, click "special" select either "constants" or "formulas" depending on if it is hardcoded or formulaic data, then hit ok. this will leave all blank cells out of the selection, then copy paste as normal to the other workbook. if your doing it in vba this is all available in the object model as
Selection.SpecialCells(xlCellTypeConstants, 23).Select
Selection.SpecialCells(xlCellTypeFormulas, 23).Select
UPDATE: I forgot about the bug (design flaw?) in excel that makes it error out when you try to copy a non-rectangular selection. See here (the article is older but appears to apply to 2013):
http://support.microsoft.com/kb/100715
I believe there are macros available on the internet to deal with this problem, one of them is here
http://www.extendoffice.com/documents/excel/799-excel-copy-multiple-selections.html
A quick fix would be to take the inverse approach: use the "special" selection to select blanks, and then delete them. then select a rectangle around the remaining cells.
the VBA call to select blanks is
Selection.SpecialCells(xlCellTypeBlanks, 23).Select
Thanks guys! I think I may have explained my problem a bit vague. I'm attaching two print screens in order to illustrate my problem.
Print screens: http://imageshack.us/photo/my-images/804/illustrasjon.jpg
My pivot table shows stops [minutes] due to specified reasons on a production line. The 'Totalt' column of the week (in which the number of production days vary) is what I wish to focus on in order to allocate improvement efforts to the right place. The sorting will be performed on a daily basis by others than me, and they really should have a button to press rather than sorting via the options bar.
NB! The 'Sum' column is not part of the pivot table. That's just a calculation of the 'Totalt' column to a fixed column in order to make a sector chart of the sorted result, since it's not possible to make a pivot chart from the 'Totalt' column.
The 'Mål per dag' column is just the daily (minutes) goal within which I wish to keep the production stops of a specified reason, so this should be kept out of the 'Totalt' column, but this is less important.
I have a pivot table filtered by weeks showing any number of days from one to five. The total column to the right that sums up the values of the days are therefore "moving", and this is why I cannot simply record a macro. I want to create a macro that sort the pivot table descending by the total column.
I do not speak the international language of VBA, but imagine that this is fairly easy for someone who does.
Immensely grateful to anyone willing to write the code for me!
Best regards,
Hans
Hans
To start, you should use the built in sort feature on the pivot table as illustrated in the screenshots below.
Select the column you wish to sort by:
This might be a little problematic for you because the pivot table doesn't automatically refresh when your data changes. If you want it to update in real time, then you will want to add the "Worksheet_Change" event handler to the VBA code. To do this, hit Alt+F11 to bring up VBA, and on the worksheet containing your source data enter the following:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
PivotTables("PivotTable1").RefreshTable
Application.EnableEvents = True
End Sub
Here "PivotTable1" will change to match whatever the name of your pivot table is. This name appears in the Ribbon when you have the pivot table selected.
Now, as you change your data, the table will automatically re-sort as you go.
Hans, welcome to SO. You did not specify which version of Excel in your tags, but here are some Excel 2007 solutions.
It is possible (and helpful!) to do this without VBA. You may be interested in this Microsoft solution.
I would recommend that you select the range of the pivot table with the totals, exclude the header and the grand total, and then go to the Data tab and choose the Sort Z->A. This will sort the totals in descending order (and of course, change the headers to the left).
You can also record a macro to do this if you would like to perform this sort on a regular basis, but that may be more trouble than it is worth.