Plot a pie chart when the number of columns within the range varies - vba

Month Organic Direct
Dec-15 £3,112 £0
I recorded VBA code that plots a pie chart for the above data. The data I am exporting can have up to 6 headings (the above example shows two headings). The code is set up to select a range that is 6 columns wide. Charts need to be adjusted manually when less than 6 headings have been exported.
What do I need so the range used fits only the data e.g. when there are only 2 headings rather than 6?

I don't know how much help you need, since you posted no code.
In the simplest case, in Excel 2013 and 2016 you can use
ActiveSheet.Shapes.AddChart2(251, xlPie).Chart.SetSourceData _
Source:=ActiveSheet.Range("A1").CurrentRegion, PlotBy:=xlRows
In Excel 2007 and 2010 (and 2013 and 2016, but it doesn't have the nicer default style applied):
ActiveSheet.Shapes.AddChart(xlPie).Chart.SetSourceData _
Source:=ActiveSheet.Range("A1").CurrentRegion, PlotBy:=xlRows
This assumes the data starts in cell A1 and the data being plotted does not touch any other data (that is, there is a blank column to the right of the last column of data, and a blank row below the two rows you've shown with labels and data). CurrentRegion finds the data-containing region surrounding the reference range (the reference range in this case is cell A1), up to blank rows and columns.

Does this all take place in the same workbook?
i would start by looking at dynmamic ranges,
Heres a good starting point:
https://www.thespreadsheetguru.com/blog/5-ways-to-create-a-dynamic-auto-adjusting-vba-range
and also look here:
Using VBA to select a dynamic range of cells and create a chart

Related

Excel - Can not compare two spreadsheets

I have gone through V-look-up guides online but I can not find one that explains what I am looking for. I am trying to avoid manual checking.
What I have:
Two sheets, Sheet 1 and Sheet 2. Both sheets have the same column names (A1:G1) with multiple rows.
Sheet 1 contains my spreadsheet where I update daily. Sheet 2 is the same spreadsheet that is imported from a application (but has hourly updates). Data can change in each row (for some columns) along with additional added/deleted rows. The data is text, dates and numbers (mixture of both too).
I want to run a formula to highlight the changes on sheet 1 (grabbing the updates from sheet 2. Once I find out the formula works correctly, I would like to know how to replace the Sheet 2 updates onto my spreadsheet (Sheet 1).
I am looking for a formula outside of creating a macro (worst case scenario).
Currently I have the following vlook up formula:
=VLOOKUP(A1,sheet2!$A:$A,1,FALSE)
When I run this in another column (lets say in H1 in Sheet 1), it will display "N/A" if that column (A1) in Sheet 2 is not the same. If it is the same, it will write out the column name.
When I use the following formula highlighting all the cells in Sheet 1, I get a values error:
=VLOOKUP(A1:G33,Sheet2!$A:$G,1,FALSE)
How could I apply that formula to the whole spreadsheet (I guess it would apply to both sheets) and have it highlight records in my spreadsheet (Sheet1). Could it also highlight rows that are missing or added?
The data in Column 1 and 2 would never change (they are ticket numbers). Only change that can apply is if ticket is closed, so when I import the updated spreadsheet that row isn't there anymore. If you think there might be a better way to tackle this down, I would like to hear.
Please let me know if I am not clear.
Here are some example screenshots:
Just in case, the formula for the totals are (adjusting the columns for each):
=SUBTOTAL(3,INDEX(C:C,2):INDEX(C:C,ROW()-1))
Sheet 2 is setup very similar. When I import it into excel, the columns are the exact same as Sheet 1, the only difference can be more/less rows (along with the updates for each row).
You can use conditional formatting.

Similar data sets, need to iterate through rows with VBA [duplicate]

This question already has answers here:
Find last used cell in Excel VBA
(14 answers)
Closed 7 years ago.
Title may not make complete sense. But description below should aid. I have similar data sets that come from one of our test machines that are no different unless by the number of samples given to the machine changes.
There is a macro now that assumes 6 samples are to be measured but if more or less than 6 are done, it screws up the selection of data and then we get false numbers.
So I would like to edit it such that the VBA script intelligently iterates through a given range of cells defined by the first cell where the header for data is contained. This ideally returns the position of the last row and then the VBA script has knowledge of where to look for the specific numbers I want. i.e. cell E12 contains the header for the data I'm concerned with, and goes until E?? has no data in that cell
TL;DR - I need help with some code to give me the position of the last row that has data in it, starting from a certain cell.
I would recommend this:
Dim LastRow As Long
With Sheets("Sheet1")
LastRow = .Range("E" & .Rows.Count).End(xlUp).Row
End With
But bare in mind that:
this would start reading cells from the rows count of the sheet and go upwards until it bums into data filled cell only then it would return the index of that row.
this would return the last cell of data based on the column E. So if
you have multiple columns and you're processing data for an entire sheet, make sure to choose that one column with the most
data.
This would yield an error if you there are merged cells in the column.
You can then construct your intelligent data range.
it would be something like:
Dim r As Range
Set r = Range("A1:B" & LastRow)

Excel formulas have stopped working

I have excel sheet which has basic + Array excel formulas, earlier it used to work properly but for the last 1 week it is not give me desired result.
I have tried the following things but nothing seems to work.
Change the formula setting from manual to automatic.
Change the format of the cell from text to General.
No space before the equal sign.
Note: I am using 2010 excel and rest in my office uses 2013 and we shared everything on common folder. Is this can be one of the reason ?
Regarding the ANZ formula, the rows have to match. Someone inserted 12 rows into part of Trades worksheet and this expanded the range to U1:U65548 and Z1:Z65548. The AW column remains at AW1:AW65536. While the ranges can be offset from each other, they have to be the same size.
This shows up on your formula for the FINEX as well.

Wordwrap to multiple cells in excel

I am currently working on a excel VBA macro that auto-fills a form using data from another sheet.
because this form needs to print on 1 page (higher ups in my company say so), I cannot have excel expand a cell to fit the text with word wrap.
what i need to figure out is how to make excel wordwrap text across 3-4 rows.
I've tried using formulas to mimic word wrap, but it doesn't come out very well (text appears missing or cut off)
EDIT: I have 4 rows with cells spanning different numbers of columns for each row. Example: the first cell spans 5 columns, I'd like it to over flow to the next row which has a cell spanning 10 columns, and that cell would overflow to the cell in the next row, and so on
You can highlight the cells that you need, right-click, select 'Merge Cells', then select wrap text.
This will create one larger cell in which you can type.

Setup an Excel template so calculations are not dependant on a specific number of columns / rows

Problem Statement:
I'm creating a template for multi tiered complicated calculations in MS Excel that depend on a few input "n x 3" matrices.
It is really difficult to redesign the 15 sheets or so (200 ~ 300 lines each) every time I have a different "n" where "n" ranges from 3 to 900.
Goals:
I'd like to input the number of "n" in a cell subsquently changing all the other sheets in the workbook accordingly.
Avoid VBA as much as possible
How can I achieve these goals?
Note: I'm willing to answer any questions or comments concerning my issue
EDIT
"n" represents the number of columns / rows, if n = 3, all calculations will be for a 3 x 3 matrix. If n = 500, all calculations will be for a 500 x 3 matrix.
I want Excel to do the expansions / contractions of the rows automatically, so i do't have to do them myself accross hundreds of tables
In Excel 2007 turn your data matrices into tables.
This can be done by clicking on a matrix and then on the Insert tab select Table. The keyboard shortcut for this functionality is Ctrl-L or Ctrl-T. Multiple tables can exist on the same worksheet.
Once your data is marked as a table, the table will dynamically expand when new data is added.
Each table is automatically given a name, starting with Table1. The name can be change via the Table tools - Design tab.
In formulas each table can be referenced by it's name.
=SUM(Table1)
Each column heading in the table is also usable in formulas.
=SUM(Table1[Column1])
In versions of Excel prior to 2007, 'Dynamic named ranges' can be used.
These can be created via the Insert - Name - Define menu.
Give the 'Dynamic named range' a name (e.g. Table1) and enter a formula similar to the following assuming your matirx starts in cell A1:
=OFFSET(Sheet1!$A$1,1,0,COUNTA(Sheet1!A:A)-1,3)
If your matrix starts in cell D10 the formula would look like this
=OFFSET(Sheet1!$D$10,1,0,COUNTA(Sheet1!D:D)-1,3)
This formula excludes any column heading in the matrix. It selects data on a 'n x 3' basis.
In formulas each 'Dynamic named range' can be referenced by it's name.
=SUM(Table1)
You will need to review the layout of your worksheet as the dynamic named range works out it's number of rows by counting all items that appear in the first column of data.
If you have cells populated above and/or below your matrix they will be included in the calculation and the 'Dynamic named range' will include rows below your data matrix.
To see which cells are included in a 'Dynamic named range' or table press F5 and type in it's name, then click on OK.
Create defined names (Insert - Name - Define) that use OFFSET and COUNTA to make dynamic ranges. Instead of
=SUM(A1:A300)
use
=SUM(MyRange)
where MyRange is
=OFFSET($A$1,0,0,COUNTA($A:$A),1)
See also
Instead of calculating against A1:A300, you can calculate against A:A, which is the entire column.
You may have to bear in mind that you don't want other stuff in that column when you design your layout.
In more recent version of Excel, you can select sets of data and format them as a table, in which case you can use the table column, for example Table1[Column2].