transposing a named range in a VBA range object - vba

I have a named range "CompletedSheetYears" which is a one column table in my DataTables sheet. The years are sorted vertically in chronological order from top to bottom, newest year at top.
This causes a problem in my charts as when I grab the range of years to use in the range of dates for the chart, it plots backwards. Yes, I know I can transpose the axis in the chart but that moves not only the dates to the right order but it moves the scale to the right of the chart which I don't want.
I know I can loop in reverse to create the right range but there must be a simpler way to do it, my VBA is rusty not having used it for some time now.
Is there a simpler way? i.e.
Set rangeReverseDates = Range("CompletedSheetYears").Transpose
or something to that effect. (Obviously, there is no Transpose method for range, or none intellisense showed me)

Something like this
Range("d1").Resize(1, Range("TTT").Cells.Count) = WorksheetFunction.Transpose(Range("TTT"))

Related

Color a specific range of cells in the same row

Similar to a Gantt Chart but not quite the same, I'm trying to color a variable range of cells on the same row.
As you can see I have a variable number of tasks and a variable start and end for each task
I've created a simple time series where the "Machine 1", which is in the row number 6, is making all the tasks. The time series is in the row number 3.
So basically, I need to color the ranges given by the tasks table in the Machine 1 row, using that time series as a reference to start coloring the cells. Each task has to have a different color.
As I'm a beginner in VBA, I've been trying to do this using a formula:
=IF(AND($C4<=AK$3;$D4>=AK$3);1;0)
Being C4 the start of the task, AK3 the place of the time series right now, and D4 the end of the task. Then I would fill the whole Machine 1 row. This would give a 1 in the range of the task and a 0 before and after the task, then I could format the row and color the cell by the given value in each cell. (1 color and 0 blank)
The problem is that this only works for one task and I really don't know how to change the formula to add the other tasks. I'm pretty sure this can be done in VBA but, like I said, I'm still a beginner. Please help me
The final answer should look like this. The color doesn't matter, it's how to color the variable ranges automatically the problem
If I understand what you want correctly, this can be done with conditional formatting:
Use four conditions, one for each of the following formulas:
=COLUMN(AK6)<=$D$7+37
=COLUMN(AK6)<=$D$6+37
=COLUMN(AK6)<=$D$5+37
=COLUMN(AK6)<=$D$4+37
and apply them in that order to $AK$6:$BZ$6

Using offset to create a dynamic range

I currently have a column, which starts at B28 (The title) and from B29 Values.
I add data to the column using a macro, and calculate the mean and standard deviation of the profit figures. Since i add data, I can't just select for example B29:B10000 as it will skew any mean and standard deviation, as it will also take into account blank cells. Therefore, I need to create a range, which will update when data is added and will only range from the first to last numerical figure, no blank cells.
The current formula I use which does not work:
=OFFSET('Panel'!$B$28,1,0,COUNTA('Panel'!$B28:$B999)-1,1)
(also tried selecting the whole column $B:$B but did not work)
Is there any solution for this?
Cheers
yes. Convert the range to table.
Use normal formula e.g. from average(B2:B9) to average(tablename[Profit])
Before Year 8 is added
After Year 8 is added - auto update

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

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

Programmatically creating multilevel axis chart with VBA on Excel

I have some code that retrieves data from a SAS Table on a server. I'm trying to chart this data using VBA, without copying it to an Worksheet first (Feeding the recordset directly into the charts)
I've had success so far with this. The problem is, my charts need to have multilevel categorical X axis, and i'm having trouble automating this only with VBA
What I'm trying to get is something like this
http://imgur.com/ElwHwjE (I can't post the image due to lack of reputation)
But so far all my attempts lead to this
http://imgur.com/id7Vua0
I know I can do this if I first make a Pivot Table from my recordset and then build a chart on top of it, but as stated before I'm trying to do this without copying the data into a Sheet.
Also tried messing with TickLabel options (Multilevel and Depth), but so far to no avail.
I've tried everything I could think of, and I have not been able to create a multiple level categorical axis without having the axis data in a worksheet. I've tried various 1D and 2D arrays, with array elements that include various LF, CR, and CRLF characters. I've tried using the worksheet range, then converting XValues to an array, and of course, this broke the axis without providing a usable array.
Bottom line: Excel charts were designed to plot worksheet data, and you can't always get around this requirement. Most of the time I think you shouldn't waste time trying to get around it.

How to loop over a multi-row/column range per column using Excel 2007 VBA?

I'm looping to loop over a range with multiple rows and columns using Excel 2007 VBA.
I'm specifically trying to loop over a user selected range, and perform a calculation on each column in the range, and output the calculation two rows below each column.
You can retrieve the currently selected range by using
Application.Selection.Address
This will give you a range value (the Selection property returns a Range object) that will look something like "$B$4:$J$20".
Given you now have a range to work with, you can iterate across each column using something like:
For Each col In userSelectedRange.Columns
...
Next
The Columns property again returns a Range object that you can either iterate over further or perform other calculations on (your exact needs aren't too clear from your question).
To post the calculated result two rows above each column (e.g. a subtotal or similar), try using the Offset function:
Cells.Offset(-2, 0)
If you're able to provide more specifics around the sort of calculation you want, I may be able to add more detail into how you achieve it.