Setting a range to another range's visible cells only - excel

Using VBA, I'm trying to set the values of a new range to those or an existing range. However the existing range has some hidden columns. Assigning the range by selecting visible cells only results in the new range's values containing the first value from the source range just repeated.
VBA extract:
Range("E1:F1").Value = Range("A1:C1").SpecialCells(xlCellTypeVisible).Value
Does anyone know how to resolve this?
Example Data with results:

Related

VBA for populating only visible cells

I have a row of cells A1:G1 on Sheet2 and I want those values on Sheet1 columns A:G but only on visible rows. I know how to do it through selecting visible cells only and pasting value from Sheet2. But I have many rows and it is incredibly slow. I tried creating an array from cells A1:G1 on Sheet2 and making it a 2D array and then selecting range on Sheet1 and set value in the worksheet but I cannot make the code to work at all. What other ways would you suggest?
Dim Arr() As Variant
Dim 2DArr() As Variant
Sheet1.range(cells(1,8),cells(last_row,8). select
activecell.formulaR1C1="=AGGREGATE(3,5,#columnA)"
Arr = Sheet2.Range("A1:G5")
for i=1 to last_row
if cell(i,8)=1 then
2DArr(i)=Arr
End If
Next i
Sheet1.range("A1:G900000")=2DArr
I admit this is an annoying one: Excel VBA does not have a Visible property for its ranges. Instead it has a Hidden property which works exactly in the opposite way, as you can see from this simple example:
' instead of if RAnge(A1).Visible, you should do:
if NOT(RAnge(A1).Hidden)

Chart range based on the value in a cell

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.

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.

vba code for filtering on multiple sheets

I have workbook having multiple sheets and same number of columns in each sheet. Everyday new sheet is added in workbook.
Can filtered data on the first sheet be transferred to succeeding sheets by running vba code ?
Regards
You can Access the filters of the first sheet and the corresponding filtered range with
Dim rng As Range
With Sheets(1)
.AutoFilter
Set rng = .AutoFilter.Range
End With
All Information on filters is stored in the filters collection of the AutoFilter object
Dim filters As Filters
Set filters = Sheets(1).AutoFilter.Filters
Every item in this collection represents one column in the filtered range. The following Statement would get you criteria1 of the first column:
filters.Item(1).Criteria1
You can use the obtained Information on the other Sheets you want to transfer them to. Do this via the AutoFilter method of the Range Object (Documentation)
If the columns are identical as you say you can get the target range easily by using the address property:
Sheets(n).Range(rng.address).AutoFilter arguments_here

Get table range in VBA (dynamic)

I have a program in VBA that I am working on that filters values from a table. I'm trying to make this a generic program that works with all tables that you give it. In my program I have to set the range of the table that it is filtering: Set rng = dataSheet.Range("A1:F78"). I was wondering if there was a way to get the Range of a table in excel that has some sort of text value in it so I don't have to specify it in every macro. I guess this is a type of dynamic range.
If there is one cell of your range which is always within your table, like A1 would be always top-left corner of the table. And if there is continuous range of cells until the end of your table you could use .CurrentRegion property in this way:
Set rng = dataSheet.Range("A1").CurrentRegion

Resources