I'm trying to create a dynamic sourcedata on a chart in VBA as this can vary depending on the data being pulled in via a macro. So far I have:
Set Rng = Sheets("Mapping Tables").Range("J13", Sheets("Mapping Tables").Range("J13").End(xlDown).End(xlToRight)).Select
Worksheets("Vintage").Charts("Vintage_1").SetSourceData Rng, PlotBy:=xlColumns
However, when I step through the code in the set range command, my cells are being selected. When I run through the SetSourceData step nothing happens. When I hover over the Rng variable it says = nothing.
I've never really done dynamic charts before but I cannot understand why my range equals nothing when my range is being selected.
Thanks in advance.
Try this.
Sub test()
Dim Rng As Range
Dim obj As ChartObject
Set Rng = Sheets("Mapping Tables").Range("J13", Sheets("Mapping Tables").Range("J13").End(xlDown).End(xlToRight))
Set obj = Worksheets("Vintage").ChartObjects("Vintage_1")
With obj.Chart
.SetSourceData Rng, PlotBy:=xlColumns
End With
End Sub
actually the first line should give you an error, if I'm not mistaken. You cannot set a range to a ".select" statement.
Try deleting the ".select" at the end
Edit: when you're defining a continuous range (e.g. a table), you can use something cleaner:
set Rng = Sheets("Mapping Tables").Range("J13").CurrentRegion
Related
I have already defined the named ranges in my workbook. I want to use the range for placing my pie chart. I am trying to write a code which sets range to variable and move the chart to the specific location.
Dim Rng As Range
Dim ChtObj As ChartObject
Set Rng = ThisWorkbook.Name("BT_GATE1").RefersTo
Set ChtObj = ActiveChart.Parent
ChtObj.Top = Rng.Top
I think I am missing something or using a worng method. Can some one help me assigning a range to variable 'Rng'?
A named range is either one cell or a collection of more cells which have been given a name. Ultimately it is a range. So in your case you can directly set it to a range as shown below
Dim Rng As Range
Set Rng = Range("BT_GATE1")
Debug.Print Rng.Address
Debug.Print Rng.Top
Debug.Print Rng.Parent.Name
I am using this code to set a range of filtered table to visible cells only.
Set rngMyRange = Selection.SpecialCells(xlCellTypeVisible)
However, there is a strange bug if only one cell is selected, that is, the code selects all used range in the filtered table.
If the selection is greater than one cell, then the code works as expected. Is there a way around this?
Usually using "Selection" is not a good practice; I am guessing you just need to get the range of the visible cells for that you can easily use this:
Sub GetVisibleRangeOnly()
Dim tbl As ListObject
Dim rng As Range
'change the name of the table and worksheet as you need
Set tbl = Worksheets("Sheet1").ListObjects("Table1")
'Note: if there is no visible cell after filtraton rng IS NOTHING will be TRUE
Set rng = tbl.DataBodyRange.SpecialCells(xlCellTypeVisible)
End Sub
I what to set a range into a variable to use in:
Private Sub Worksheet_Change(ByVal Target As Range)
I want to refer to a cell which is 3 cells of top of a particular column of a list object.
this way:
set rOverlap = intersect(SRTbl.ListColumns(firstConceptColumn).DataBodyRange.iTem(1).offset(-3, 0).resize(0,3),target)
where firstconceptcolumn=9 (or whatever)
This would not work.
I do it in this way because If I ever decide to add columns at the beginning of the table the code will not need to be chage.
The first row of the listobject is not row1 but row5.
The first 5 rows trigger subroutines when the values change (upon entering data an pressing return)
I also tried it using headerRowRange with:
Set rOverlap = Intersect(SRTbl.HeaderRowRange(firstConceptColumn).offsset(-3, 0).Resize(, 4), Target)
which would not work neither.
Actually as a more fundamental question I am thinking what kind of data is:
SRTbl.HeaderRowRange(firstConceptColumn)
Is that a range?
why cant you use the following:?
SRTbl.HeaderRowRange("name")
Thanks a lot
PS: Everything what I say on stackoverflow is to resize the listobject itself.
PS2: I dont understand neither if there is a difference between these two guys:
.Resize Range("A1:B10")
and
.Resize(1,10)
Jose Ferro
Here is one way:
Option Explicit
Public Sub test()
Dim SRTbl As ListObject, header As Range, ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet2")
Set SRTbl = ws.ListObjects(1)
Set header = SRTbl.ListColumns("Name").Range(1, 1)
Debug.Print header.Offset(-3).Resize(1, 2).Address
End Sub
Here is another:
Option Explicit
Public Sub test()
Dim SRTbl As ListObject, header As Range, ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet2")
Set SRTbl = ws.ListObjects(1)
Set header = SRTbl.HeaderRowRange.Find("Name")
If Not header Is Nothing Then
If header.Row > 3 Then
header.Offset(-3) = "Tada"
End If
End If
End Sub
To refer to whole header width with offset of -3 rows:
SRTbl.HeaderRowRange.Offset(-3)
To simply resize found cell:
header.Offset(-3).Resize(1,2)
To resize to end of table from found cell
Debug.Print header.Resize(1, ws.Range(Split(SRTbl.HeaderRowRange.Address, ":")(1)).Column - header.Column + 1).Address
So I am trying to use the autofill method in vba right now over a range that is set to a variable. I know that the range of cells you are autofilling from must be included in the destination. So, I do just that. However, and much to my surprise, all the cells in the range are being set to nothing.
Here is the code:
Dim table2Range As Range
Dim table2Range2 As Range
Dim table2Range3 As Range
Dim tableholder As Range
Set table2Range2 = Range("Y54").End(xlToRight).Offset(0, 1)
Set table2Range3 = Range("Y77").End(xlToRight).Offset(0, 1)
Set table2Range = Range(table2Range2, table2Range3)
Set tableholder = Range("y54", table2Range3)
tableholder.Select
table2Range.AutoFill Destination:=Selection 'This is setting all my cells to nothing for some reason
Here is the before & after screenshots:Before, After
Any help is hugely appreciated!
Your AutoFill line of code should be:
SourceRange.AutoFill Destination:FillRange
It seems like your "table2Range " is the source, which overlap with your Fill Range. I.e:
.End(xlToRight).Offset(0, 1) will set range from Y54 to the rightmost cell of the same row.
You can edit the source range with hardcode first (i.e. Range("Y54:Z77") and see if that works for you, then work from there
For example (try on a new workbook):
Sub example()
Set SourceRange = Worksheets("Sheet1").Range("A1:B1")
Set fillRange = Worksheets("Sheet1").Range("A1:G1")
SourceRange.AutoFill Destination:=fillRange
End Sub
Enter "1" in cell(A1) and "2" in Cell(B1) and run the code.
I am new to the world of VB but I would like to copy data from one tabs on a spreadsheet called Ilog and past this into another tab on the same spredshhet on a tab called Journal.
When the data is pasted to the new tab I'd normally filter is so Blanks are ommited so I would like to be able to get the VB code to do this automatically.
Any help would be greatly appreciated
I am not certain what you are asking, but the following code will copy data from a range on sheet llog and paste in journal. Then loop through and delete cells that are blank.
Sub test()
Dim rng As Range
Set rng = Worksheets("llog").Range("A1:A8")
rng.Copy
Set rng = Worksheets("journal").Range("A1:A8")
rng.PasteSpecial
For Each c In Range("A1:A8")
If c.Value = "" Then
c.Delete
End If
Next c
Set rng = Nothing
End Sub
Depending on the complexity of the range being copied, you could also go with:
Sub test()
Dim rng As Range
Set rng = Worksheets("llog").Range("A1:A8")
rng.Copy
Set rng = Worksheets("journal").Range("A1:A8")
rng.PasteSpecial
rng.SpecialCells(xlCellTypeBlanks).Delete
End Sub
which avoids any looping. If you have a relatively complex range, you may want to look in to using the autofilter and then coping over just the visible rows.