Define named range from databodyrange - excel

I want to define a named range from a table's databodyrange.
I manage to get the address of the databodyrange and can select this range
Dim acSh As Worksheet
Dim oLo As ListObject
Sub StelDatabereikIn()
Set acSh = ActiveSheet
For Each oLo In acSh.ListObjects
Application.Goto oLo.Range
MsgBox "Databodyrange is " & oLo.DataBodyRange.Address
oLo.DataBodyRange.Select
I would like to define a named range based on the databodyrange.

Not sure why, since it already is a named range, but you can use:
oLo.DataBodyRange.Name = "whatever"

Related

How to get range assigned to Named Range in excel vba?

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

vba: select table from row 3 to end for listobject

Selecting a whole sheet for a listobject is done this way
ws.ListObjects.Add(xlSrcRange, Range("$A$1").CurrentRegion, , xlYes).Name = "Cost"
But now I want the same but starting with row number 3:
Dim rng As Range
Dim crng As Range
Set crng = ws.Range("$A$1").CurrentRegion
Set rng = ws.Range(ws.Range("A3"), ws.Range(crng.Columns, crng.Rows))
ws.ListObjects.Add(xlSrcRange, rng, , xlYes).Name = "Eval"
But this also selects the whole range.
How can I choose the correct range?
There are two cases - when a table exists in range "A3" or when there is no table there.
When a table exists
I have managed to select from A3 to the end of the table with the code below:
Sub TestMe()
Dim ws As Worksheet
Set ws = Worksheets(1)
Dim myRange As Range
With ws
Set myRange = .Range(.Range("A3"), .Range("A3").End(xlToRight))
Set myRange = .Range(myRange, myRange.End(xlDown))
End With
myRange.Select
Stop
'Uncomment the line below, if there is no table.
'ws.ListObjects.Add(xlSrcRange, myRange, , xlYes).Name = "Eval40"
End Sub
However, if you uncomment the last line of the code, you will see the 1004 error, which states something like "A table cannot over lap another table", which makes sense, as the new table, named Eval40 is part of the old table which already exists.
When a table does not exist
If there is no existing table at the range, then it works ok:
You should be able to assign the range just using row(3)
Set rng = ws.Rows(3)

Get Used Range of Filtered ListObject

I am trying to copy the visible cells of a filtered table to another sheet.
I am getting an error that Range.Address is an invalid qualifier, although I read, this is how you must refer to a list object range.
Sub TestRun()
Dim strng As Range
Dim lo_b1 As ListObject
Set lo_b1 = x_bf1.ListObjects(1)
Set strng = ThisWorkbook.Names("co_st").RefersToRange
lo_b1.Range.Address.SpecialCells(xlCellTypeVisible).Copy strng
End Sub
Here is a tiny example. Starting with a ListObject in Sheet1:
This will apply a filter and copy the visible rows elsewhere:
Sub KopyTab()
Dim rng As Range
Set rng = ActiveSheet.ListObjects("Table1").Range
rng.AutoFilter Field:=1, Criteria1:="mike"
rng.Cells.SpecialCells(xlCellTypeVisible).Copy Sheets("Sheet2").Range("A1")
End Sub
The result:

How to loop through named range label automatically

I have same many Named Range in my wsI with different label (list with different row number but same column number) (es. listA=Sheet1!$A$2:$E$4, listB=Sheet1!$A$5:$E$6 etc). Now I want to copy my Named Range in a wsO, this code works as I expect:
Sub CopyNamedRange()
Dim wsI As Worksheet, wsO As Worksheet
Set wsI = ThisWorkbook.Sheets("Sheet1")
Set wsO = ThisWorkbook.Sheets("Sheet2")
wsO.Range("A1")= "listA"
wsO.Range("listA").Copy wsO.Range("B1")
End Sub
The result is to copy listA cells from the Sheet1!$A$2:$E$4 to Sheet2!$B$1:$F$3 if in Sheet2!A1 was write "listA".
Now, I want to know if it's possible to create a macro that loop through all my labels of the Named Range in wsI and according to the value in Sheet2!A1, copy all the cells.
Secondly, I will introduce a second loop through the Column "A" on Sheet2 in order to find all the different "listX" (es. listA, listB, listA, listC, listB, etc.) and copy automatically the cells in the Sheet2 (obviously if A1=listA will be occupy 3 rows from 1 to 3 the next cell in column A with a "listX" will be in A4 and so on).
This is how:
Option Explicit
Sub Test()
Dim MyNAmes As Name
For Each MyNAmes In ThisWorkbook.Names
'Your code
Next MyNAmes
End Sub
Here the code for my question, if anyone is interested:
Option Explicit
Sub Protocols()
ActiveSheet.UsedRange.Select
Dim wsI As Worksheet, wsO As Worksheet
Dim cell As Range
Dim nm As Name
Set wsI = ThisWorkbook.Sheets("Sheet1")
Set wsO = ThisWorkbook.Sheets("Sheet2")
On Error Resume Next
For Each cell In Columns("A").Cells
If IsEmpty(cell) = True Then
Else
For Each nm In ThisWorkbook.Names
If cell.Value = nm.Name Then Exit For
Next nm
wsI.Range(nm.Name).Copy wsO.Cells(cell.Row, "B")
End If
Next cell
On Error GoTo 0
End Sub
Now the doubt is: it's possible to limit the nm Name searching only in the Named Range stored in Sheet1 (wsI in the example) instead of the whole workbook? I tried to replace
For Each nm In ThisWorkbook.Names
with
For Each nm In wsI.Names
But it seems not work.
Any ideas? Do you think it was possible?
P.S.: It's just to limit the research to the Named Range and avoid unecessary loop!

Transposing named range from table, into another named range

I have this issue with taking named range in Worksheet "Team overview" (which is setup to table named "tbl", column no. 2), and then pasting it transposed into another named range in another worksheet called "Knowledge matrix" (could be solved by directing it to absolute reference, but in case I change number of columns in tables/worksheet I would need to update code in VBA).
I've tried using loop through array and only switching the x,y, coordinates, but the struggle I have is with copying that selection to another named range and assigning the source named range to variable rng (which returns run-time error 5 - Invalid procedure).
The code worked for testing file without named ranges and tables with just absolute references.
Sub Transpose()
Dim rng As Range
Dim rngT As Range
Dim oarray As Variant
Dim cl As Long
Dim rw As Long
Set rng = ThisWorkbook.Sheets("Team overview").Range("Name")
Set rngT = ThisWorkbook.Sheets("Knowledge Matrix").Range("Target")
oarray = rng
For cl = 1 To UBound(oarray)
For rw = 1 To UBound(oarray, 2)
Cells(rw, cl) = oarray(cl, rw)
Next
Next
End Sub
The whole picture:
Table "tbl" is for a clear overview of team members. When the team lead adds/deletes one, I need it to be updated in all other table headers. Since headers cannot have formulas in them, I was thinking of copying the column in transposed way into named range for the other table headers.
There is also slight complication of condition the member being "Active" or "Butterfly", but that will be just cherry on the top.
So basically:
If column 1 has "Active" or "Butterfly" copy table/named range (Vertically spaced)
Transpose that array and paste it into another named array (Horizontally spaced)
I would prefer not to use .Select if possible.
Source table with named range
Target table with named range
There is already a method called Transpose.
Sub MyTranspose()
Dim rng As Range
Dim rngT As Range
Dim oarray As Variant
Set rng = ThisWorkbook.Sheets("Team overview").Range("Name")
Set rngT = ThisWorkbook.Sheets("Knowledge Matrix").Range("Target")
oarray = rng.value2
rngT.resize(ubound(oarray, 2), ubound(oarray, 1)) = _
application.transpose(oarray)
End Sub

Resources