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.
Related
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.
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.
I'm using a VBA routine to copy
Source sheet: named range (multiple 'selections')
Target sheet: copy-paste formula from cells in named range, to cells with a certain offset from a cell on the target sheet (this offset is depending on a certain selection).
I have two types of named ranges on the source sheet; one from which I only want to copy the values (=rng_operationeel_input_data), one from which I would like to copy to formulas (=rng_operationeel_formules). The formulas should be copied 'relatively', to have references on the target sheet (which are part of the copied values from the other range). For that reason, I can't use "targetCell.Formula = sourceCell.Formula", as it then literally copies the absolute formula. Not relative.
That's why I'm using sourceCell.
I do this in a for each loop over all cells in the source range, as the named range is not one single range (set of ranges).
Note. 'datasetReferenceCell' is the cell on the target sheet from which the offset is taken for pasting.
The problem is that, even if I use VBA to do the copy-pasting, without using 'select' somewhere, still at the end the user is confronted with the target sheet. (Excel will move to the target sheet)
This is only happening, for the copy-paste part.
How can I prevent this from happening?
edit: note that I am already using "Application.ScreenUpdating" (at start to false, at the end to true). I also have a MsgBox at the end of the routine (for info that routine was successful). Excel is moving to the target worksheet after the MsgBox is closed.
Below the VBA code part.
' dataset for weeknr found: save data to dataset
Dim dataRange As Range, dataField As Range
' for each cell in input data range: save value in dataset
Set dataRange = Range("rng_operationeel_input_data")
For Each dataField In dataRange
datasetReferenceCell.Offset(dataField.Row, dataField.Column).Value = dataField.Value
Next dataField
' !!! Following are only saved, not loaded, as it are formula based fields
' for each cell in formula range: paste formula
Set dataRange = Range("rng_operationeel_formules")
For Each dataField In dataRange
dataField.Copy
datasetReferenceCell.Offset(dataField.Row, dataField.Column).PasteSpecial (xlPasteFormulas)
'datasetReferenceCell.Offset(dataField.Row, dataField.Column).Formula = dataField.Formula 'not working, as relative formulas are required
Next dataField
The only way I was able to solve it was to reset the active sheet:
Public Sub routine()
Dim activeWs As Worksheet
Set activeWs = ThisWorkbook.ActiveSheet
.. code including copy & PasteSpecial ...
activeWs.Activate
End Sub
I'm looking to vlookup a value from a tableArray on a different workbook and return the value and entire cells content.
This SO answer is nearly what I'm looking for.
If I amend this links copyFormatting routine (Private Sub copyFormatting(destCell As Range, srcCell As Range)) to copy the cell and paste all instead of formatting the cells font:
srcCell.Copy
destCell.PasteSpecial xlPasteAll
Then the code works as is when the return value is within the same workbook.
Any ideas how I can get the set range line -
fromCell.Parent.Range(destAddr)
to work when referencing a cell address in another workbooks sheet?
Set extractDestRange = Range(destAddr) 'fromCell.Parent
commenting out the fromCell.Parent reference fixes this.
I have a spreadsheet with a large amount of data in. About half the cells are merged horizontally with other cells and contain names e.g. John Doe.
Does anyone know how to write a macro to unmerge the cells while distributing the value of the cell to all the cells that were previously merged?
Cheers
Jack
EDIT: The reason I am doing this is to check to see if two adjacent cells are equal i.e. is A1 = A2. But I run into problems when either cell is merged. If anyone knows a way around this problem without separating the cells and copying the data that would be even better!
The idea I provide below is tested for Excel 2010 VBA Win7. However, being not sure I hope it should work as for Mac, too (as this is rather set of standard properties and methods of Range object). If this doesn't work please let me know to delete my answer.
This simple code will work for selected area however it's quite easy to change it to any other range. Some other comment inside the code below.
Sub Unmerging_Selection()
Dim tmpAddress As String
Dim Cell As Range
'change Selection below for any other range to process
For Each Cell In Selection
'check if cell is merged
If Cell.MergeCells Then
'if so- check the range merged
tmpAddress = Cell.MergeArea.Address
'umnerge
Cell.UnMerge
'put the value of the cell to
Range(tmpAddress) = Cell
End If
Next
End sub
And the picture presenting before and after result:
I was able to get the solution from KazJaw to work on a mac with one edit, changing Cell.UnMerge to
ActiveSheet.UsedRange.MergeCells = False, as provided by Ron Debruin here: http://www.rondebruin.nl/mac/mac027.htm.
Sub Unmerging_Selection()
Dim tmpAddress As String
Dim Cell As Range
'change Selection below for any other range to process
For Each Cell In Selection
'check if cell is merged
If Cell.MergeCells Then
'if so- check the range merged
tmpAddress = Cell.MergeArea.Address
'umnerge
ActiveSheet.UsedRange.MergeCells = False
'put the value of the cell to
Range(tmpAddress) = Cell
End If
Next
End sub