Chart range based on the value in a cell - excel

I need to create a line chart that selects a range of data based on the value in a cell. For instance, in cell C1 I write A1:B4, this means the chart is a display of the data in cells A1 to B4. If I simply change the value in cell C1 to A1:B9, I want the chart to display the data of this range - you get the point. This shouldn't be too hard, but i'm not getting it right (and for some reason the web is full of the same examples that do not apply for my)
I've tried using a Named Range function. I still think this is the way to go, but I need some help.

There is no VBA needed for this.
Let's start having the following worksheet named Sheet1:
Now we need three named ranges. One for the whole range which we get indirect form C1, one for the categories which is the left column of the whole range and one for the values which is the right column of the whole range.
So in name manager we create following named ranges:
Note all named ranges are in scope of the sheet Sheet1 and not in workbook scope. So while creating the named ranges, always choose scope Sheet1 instead of Workbook
Name myRange refers to =INDIRECT(Sheet1!$C$1). So it gets it's range from that cell value.
Name myCategories refers to =INDEX(Sheet1!myRange,,1). That gets all rows (since no special row is given) from column 1 of myRange.
Name myValues refers to =INDEX(Sheet1!myRange,,2). That gets all rows (since no special row is given) from column 2 of myRange.
Now we can insert a chart (a pie chart for example).
Then we right-click the chart, and then choose Select Data.
First we delete all present series on left side below Legend Entries (Series), if any. Then we add a new series. In Series values: we put the formula =Sheet1!myValues, OK.
On right side below Horizontal (Category) Axis Labels we click Edit and put in the formula =Sheet1!myCategories, OK.
Then OK for the whole select-data-dialog.
Now if we change the cell value of C1 into something what INDIRECT can interpret as a cell range, then the chart will change too.
To give a VBA solution also:
Let's have the same sheet as above. Data in A1:B8 and range address in C1.
Now create the wanted chart. It must be the one and only chart object in that sheet.
Now put the following code in sheet module of Sheet1 (right click on the sheet tab and click View Code):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim oChartObject As ChartObject
Dim oChart As Chart
If Target.Row = 1 And Target.Column = 3 Then
On Error Resume Next
Set oChartObject = Me.ChartObjects(1)
Set oChart = oChartObject.Chart
oChart.SetSourceData Source:=Me.Range(Target.Value)
On Error GoTo 0
End If
End Sub
This code leads to changing of source data of the chart if the value of C1 (row 1, column 3) changes.

Related

How to set cell color based on a manually selected color (not based on cell value) in a cell in another spreadsheet

I found the below link on how to copy a color based on a cell in the same sheet, however my issue is that the cells which colors have to be matched will be on cells spread out through the workbook.
Link: How to set cell color based on another cell color
My home page (1st sheet) contains a summary of information provided in the following sheets. All the relevant cells have been formatted to reflect the value of their corresponding cells in the other sheets.
So for example, My homepage cell F7 is already formatted so that it copies the value of the corresponding cell in the source sheet "Quarter 1":
=quarter1!B15
Now, what I really want is for the reproduced summary cells on my home page to also match the colors I manually select for their corresponding (source) cell the other sheets. Is this possible using VBA coding?
The source cells in question will contain dates which will be manually set - not automatically s based on value, in 3 different colors (red = uncertain; yellow = tentative; green = ok)
I got the below code from another site but it only works if the cells are on the same sheet:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Me.Range("C1").Interior.Color = Me.Range("A1").Interior.Color
End Sub
Is there a way I can tweak this code to refer to a cell color (that is manually selected and not dependent on value) on another sheet in the same workbook?
Thank you so much for your help!
You just need to put the code in "Thisworkbook' with below code and need to change sheet reference instead of me. When you will change the sheet then changes will reflect in other sheet.
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Sheet2.Range("C1").Interior.Color = Sheet1.Range("A1").Interior.Color
End Sub

excel countif entire row in a table

I have a structured table in excel 2016.
I want to have a cell to count the number of cells across the entire row within the table if it matches my criteria.
I have tried putting this formula in column A on each row =COUNTIF(Table[#],"my criteria") but that does not count properly.
However, this works: =COUNTIF(Table[#[ColB]:[ColH]],"my criteria"). But since my table will expand, I don't want to specify the column name [ColB], I want to refer to the entre row in the table.
A header
countif
colC
colD
colE
First
formula
A
C
Second
formula
B
C
formula = =COUNTIF(Table[#],"A") does not work
formula = =COUNTIF(Table[#[colC]:[colE]],"A") works
My table will expand both horizontally and vertically.
Please Note: This solution is only available if you chose to use VBA. VBA does not work in web versions of Excel. Please ensure that you resave your workbook as a macro-enabled workbook before proceeding.
You can choose to use VBA and create your own custom worksheet formula. Since this question didn't start out as a VBA issue, I will be a bit more detailed on the process of setting this up as opposed to just throwing you some code and you having to figure out what to do with it.
After you've re-saved your workbook as a macro-enabled workbook, open the VBA Editor (VBE) by pressing Alt and F11 simultaneously.
In the VBE, click Insert > Module. You should now see Module1 highlighted on the left side bar (The project Explorer).
Copy and paste the following code in the blank area of the module:
Option Explicit
Public Function GetMyRowCount(Criteria As Variant) As Long
Dim ws As Worksheet
Dim tblRng As Range, RowRng As Range
With Application.Caller
Set ws = .Worksheet
Set tblRng = ws.Range(.ListObject.Name)
Set RowRng = ws.Range(ws.Cells(.Row, .Column + 1), ws.Cells(.Row, tblRng.Columns.Count))
End With
GetMyRowCount = Application.WorksheetFunction.CountIf(RowRng, Criteria)
End Function
Now use this UDF (User Designed Function) in your worksheet. In the column you would like the calculation to be in, simply type =GetMyRowCount("My Criteria") and it should calculate.
To point out how this code works in more detail:
Application.Caller is referring to the cell that this function is located in. Because we now know the location of the cell, VBA can use it's location to obtain the row data from it (which is why you don't need an argument for the row #).
RowRng is getting the starting point of the column within the ws.Range(...) function with the first ws.Cells(...) function. .Row is the row # from the GetMyRowCount function (using Application.Caller.Row method), and the 3 is simply the static column C.
The way we grab the last column we need is by counting the total # of columns within the table: ws.Cells(.Row, tblRng.Columns.Count)
Using the information we obtained from bullets 2 and 3, we can establish the entire range of the lookup we need, and then place this range into your CountIf() function, along with the criteria you passed with the function's argument:
GetMyRowCount = Application.WorksheetFunction.CountIf(RowRng, Criteria)
As you can see in the following example, I wanted to count the number of times in the row the number 1 occurred:
Another example showing it works with text as well by using "Apple" as the criteria:
Try this: =COUNTIF(B:B,"my citeria"), so if your Column is A, range would be A:A, for B it is B:B.
Let me know if this helps.

How to do a picture lookup using Formula in Excel

I am wondering if there is a solution to load a picture using formula (not VBA) from a list of pictures in Excel
For example,
=IF(TODAY()-B9<8,G6,"puste")
Let's say I have a picture in cell G6, that I want the formula to return if the condition is true.
In brief, the solution can be summarized in 2 steps:
Create a linked picture cell using PasteSpecial method of Excel.
Modify the formula of linked cell to "Named Range" formula for making it dynamic.
(Optional) - If there are many cells, and one find it tiresome to manually change the address of each linked image then use the below VBA macro to assign Named Range formula to all cells.
Sub Set_Formula_Linked_Cell()
Dim rngDest As Range
Dim objPic As Object
For Each rngDest In Range("F5:O18").Cells
rngDest.Select
Set objPic = ActiveSheet.Pictures.Paste(Link:=True)
objPic.Formula = "=Country_Flag"
Set objPic = Nothing
Next
End Sub
In Detail, let's follow through a similar situation:
Let's assume we have a list of Country and their adjacent flags.
Next step is to copy the Cell (any cell which contain the flag, do not copy picture but the Cell/Range) and paste as Linked Picture in the destination cell.
Now, a careful observation in the address bar reveals that current cell which displays a flag is linked to another cell. We need to change this formula. Unfortunately, we cannot change the formula here. Either it could be a direct reference or a named range but not a formula.
We will create a "Named Range" with the name "Country_Flag" and Formula as:
=INDEX(Sheet1!$B$2:$B$6,MATCH(Sheet1!$F$3,Sheet1!$A$2:$A$6,0))
In the last Step, we will assign this named range to the linked cell.

create name for both header and column but type once

I am trying to set up a collection of spreadsheets for others to use. I am putting labels on the first row for each column, I think of them as headers in that case. And I know how to name a column in Excel (at least 2010 lets you do this).
If I have columns with the headers "higher" and "lower", and the columns have those names also, then the formula "=higher+lower" in a given row would use the values from those columns in that row to calculate the result.
I would like to end up with the descriptive column name being the same as the header value. I'd like a way to either create the headers from the column names, or create the column names from the headers, so I don't have to enter them twice. I have a lot of columns, and multiple spreadsheets to do this with; I'm just trying to save typing them all twice, and both initially and to keep them updated as they change.
Manually: Select the desired columns and go Formulas Ribbon > Create from Selection, tick "Top row" and hit OK. Repeat when you've changed a value in row 1.
With VBA: Use this code in the worksheet
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("1:1"), Target) Is Nothing Then
For Each cel In Target
Debug.Print cel.Column
Columns(cel.Column).Select
Selection.CreateNames Top:=True, Left:=False, Bottom:=False, Right:=False
cel.Select
Next cel
End If
End Sub
If you copy and paste the same value into multiple cells, Excel will create the first name and then prompt for the other cells if you want to replace the existing name.
Changing a cell in row 1 will create a new range name. The old range name will still remain in place.
If you have values that can be interpreted as a cell address, like A1, Excel will add a _ sign to the range name, like A1_. If you enter numbers into row 1, Excel won't create range names.

Autofit Method of Range Class Failed (Run Time Error 1004)

This is just part of my code. the value from the textbox here already gets copied to the specific cell in the Bank Certification worksheet. I need to make sure that cell C5 is specifically fitted regardless of the length of the text i inputted in the textbox. I tried interchanging range with cells to no avail. This problem seems so simple but I don't know why it doesn't work...
Dim counterparty As String
counterparty = Sheet1.txt1.Text
Range("C5").Value = counterparty
Sheets("Bank Certification").Select
Range("C5").Select
Selection.AutoFit
Try
Dim counterparty As String
counterparty = Sheet1.txt1.Text
Range("C5").Value = counterparty
Sheets("Bank Certification").Select
Columns("C:C").Autofit
Other answers correctly state that AutoFit must be used with a column, not just a cell. However, there are some nuances to using AutoFit that I didn't understand until I started experimenting.
Either of the first two statements below will use all values in column C to AutoFit the width of the column. That means if there is a value in some other cell in column C (for example C10) that is wider than the value in C5, it will fit the column to the widest cell in column C (for example C10).
Range("C5").EntireColumn.AutoFit ' Will fit to widest cell in column
Range("C:C").AutoFit ' Will fit to widest cell in column
If you want to just fit the column on 1 cell (or a certain range of cells, but not the whole column or columns), use a statement like this:
Range("C5").Columns.AutoFit ' Will fit column C to width of cell C5
And of course, it's always better form to write code like this when you can:
Fully qualify the range unless you're absolutely sure you'll only be working with one worksheet
Use Named Ranges or Range objects.
For example:
Workbooks("MyWorkbook.xlsm").Sheets("Sheet1").Range("MyData").Columns.AutoFit
' or
Set AutoFitRange = Workbooks("MyWorkbook.xlsm").Sheets("Sheet1").Range("C5")
AutoFitRange.Columns.AutoFit

Resources