Referencing Active Table in VBA - excel

I'm creating a macro to manipulate a table in excel. I've been able to create code to do what I need, the problem is, my table names might not always be the same.
As an example:
Range("DATA_INPUT4[MFG Catalog]").Copy
This code copies the MFG Catalog column in my table just fine, but the table isn't always going to be named "DATA_INPUT4".
I've played around with a few things to try to get it to work with the active table and I feel like I'm close, but I can't get any progress.
I've tryed:
Sub Copy_Active_Table()
Dim activeTable As String
activeTable = ActiveSheet.ListObjects(1).Name
MsgBox activeTable 'To make sure it's pulling the correct table name
Range("activeTable[MFG Catalog]").Copy
End Sub
This is probably completely wrong, but you can see where I'm going with it.
I have a feeling that I have to Dim activeTable As ListObject but I haven't been able to figure that out either.
This seemingly simple problem is driving me nuts, any help would be awesome.
Thanks,
Brian

For those who look for a solution to reference active table, the solution is:
ActiveCell.ListObject.Name
Other solutions fix problem in the question (using variable to refer to the table) - but the script in the question does NOT refer to the active table - but rather the first table in the sheet (which might be the same as active table).

Answered in the comments,
Yes indeed, you are very close. Try this: Range(activeTable & "[MFG Catalog]").Copy
Ralph
Thanks, Ralph.

Just had a similar problem and ended up here.
Along the lines of #choukkos's answer, you can also use the ListObject's properties (which is even a bit faster as it doesn't need to do the name resolution twice)
ActiveCell.ListObject.ListColumns("MFG Catalog").DataBodyRange.Copy
in the same manner other table data can also be extracted...
Set t = ActiveCell.ListObject
'copy line 3 of the table
t.ListRows(3).Range.Copy
'select current line
t.ListRows(Selection.Row - t.Range.Row).Range.Select

Related

How to insert a column in Excel using VBA?

Please help me with the code to insert a column in excel using vba. Here is what I am doing -
Sheet1.Range("A:A").EntireColumn.insert
That code works fine for me.
Sheet1.Range("A:A").Insert works also. Range("A:A") is already referencing an EntireColumn.
There are a few things to check if it's not working for you:
You're referencing an object called Sheet1. Is that definitely the codename of the worksheet you want to change? Make sure you understand the difference between sheet name and codename. You could try referencing it by the sheet's name instead: Worksheets("Tabname").Range("A:A")...
Is the worksheet protected? That would give you an error if it is.
Is there any data in the right-most column of the spreadsheet? That would also cause an error as Excel doesn't know what to do with it. If you're not 100% sure, select the entire right-most column of the sheet and hit delete to remove anything that might cause an issue.
Lastly, can you insert a column manually? i.e. select left most column, [right-click] and [Insert]?
I think you got the Sheets property wrong, this works for me :
Sheets(1).Range("A:A").EntireColumn.Insert
You should clearly mention what you are trying to do; what type of problem you are facing.
However, you should try out the below code. hope this will help
Sheet1.Range("A:A").EntireColumn.Insert Shift:=xlToRight
while inserting a new row or column make sure to refer to the entire row or column. Check if there are any marge cells at the right of column A. That can possibly be causing the problem.
Sub TryMe()
Columns(1).Insert
End Sub
Also, this is a very generic question. If you can Google something and figure it out in a fraction of the time that it takes to craft a question and post it on SO, just Google it. If your question is very unique, and after hitting multiple dead ends using Google, then ask the SO community. I found the 3 links below using a Google search that took around 1 second.
https://www.automateexcel.com/vba/insert-row-column/
https://excelchamps.com/vba/insert-column/
https://www.educba.com/vba-insert-column/

vba Userform macro edits wrong sheet

I have a userform that is supposed to insert a row on Sheet3 and populate some cells in that row with some values. it works great as long as I have sheet3 displayed. (The form is shown modeless to give me access to the sheets).
Anyway, I happened to have another sheet active and ran it again and was surprised to see it inserted the row not in sheet3, but in the one I had displayed... (Thank God I saved first!)
In the code, I specified a range object as follows to find the insertion point: (I'll truncate the code a bit to keep it simple)
Dim RecordRange As Range
Set RecordRange = Sheet3.Cells(RowVariable,ColumnVariable)
RecordRange.Offset(1,0).EntireRow.Insert
blah blah blah.
A workaround is to activate the sheet first:
Sheet3.Activate
That at least inserts into the correct sheet, but I'd rather not have sheet3 be displayed when I add the record, so I even surrounded that line with:
Application.ScreenUpdating = False
Sheet3.Activate
Application.ScreenUpdating = True
unfortunately, ScreenUpdating doesn't work from within a Userform Code module, so that bites...
I still don't understand why it chooses to insert the row into whatever sheet is active, when I've already specified sheet3 in the code. I have another Macro very similar that doesn't have this problem.
any ideas?
...so you figure your workaround needs a workaround? Hmm... I think that qualifies as a double-negative since a workaround is supposed to, by definition, fix problems rather than cause them...
A workaround is a bypass of a recognized problem in a system. A
workaround is typically a temporary fix that implies that a genuine
solution to the problem is needed. But workarounds are frequently as
creative as true solutions, involving outside the box thinking in
their creation.
Typically they are considered brittle in that they will not respond
well to further pressure from a system beyond the original design. In
implementing a workaround it is important to flag the change so as to
later implement a proper solution.
Placing pressure on a workaround may result in later system failures.
For example, in computer programming workarounds are often used to
address a problem or anti-pattern in a library, such as an incorrect
return value. When the library is changed, the workaround may break
the overall program functionality, effectively becoming an
anti-pattern, since it may expect the older, wrong behaviour from the
library. (Wikipedia)
Just sayin'... :-)
So instead of fixing the workaround, your original issue is (thankfully) straightforward.
You're using Sheet3 as an object, and I suspect you haven't assigned anything (like Worksheets("Sheet3") to an object called Sheet3.
Try this instead:
Dim RecordRange As Range
Set RecordRange = Worksheets("Sheet3").Cells(RowVariable,ColumnVariable)
RecordRange.Offset(1,0).EntireRow.Insert
If you indeed intended to use Sheet3 as an object then make sure it's declared as set, so for example you could instead use:
Dim RecordRange As Range
Dim Sheet3 As Worksheet
Set Sheet3 = Worksheets("Sheet3")
Set RecordRange = Sheet3.Cells(RowVariable,ColumnVariable)
RecordRange.Offset(1, 0).EntireRow.Insert
Also, just to point out: If either of these solves your issue then the problem would've "made itself known", by including one line at the top of every module (at least during development & troubleshooting):
Option Explicit
This link has a short explanation, but basically it forces you to properly declare all variables (thus helping to prevent future workarounds!)

Sheet / Range Select (combined) not working

Something has changed in my VBA that is not allowing me to complete certain routines. I have listed a very simple example below. If anyone has experienced this I would be really appreciate any support in resolving this issue.
Simple example of issue:
When I use the following code it works fine.
Sheets("Sheet1").Select
Range("B3").Select
When I combine them I get a "1004" error
Sheets("Sheet1").Range("B3").Select
I checked the reference/document library and nothing appears to have changed in here. It has to be something simple but I just can't put my finger on it.
If you absolutely must do it in a single line of code then swap the Select for an Application.GoTo which accepts both worksheet and cell range.
application.goto range("Sheet1!B3")
However, it is almost never necessary (and most often counter-productive) to use the Range .Select method to reference a cell or cells to work on. See How to avoid using Select in Excel VBA macros for methods on getting away from relying on select and activate to accomplish your goals.
You already have your answer:
first Select the worksheet
the Select a range on that worksheet
Your code will work if you happen to be on Sheet1 when it is run, but will fail if you are not on Sheet1. In my opinion VBA is kind of dumb with regard to this issue.

Excel VBA getting and using Name of Table

I got stuck at a problem with Excel VBA.
I am supposed to do the kinda easy task of copy/paste a variable range of cells from "sheet2" into the same range in "sheet1".
500 Rows like in my code is far too much, but I tried it this way, to "catch" the variable aspect.
The tricky part is, that the range in "sheet1" is a table(which gets created from TFS).
Sub CopyP()
Sheets("Sheet1").Range("B3:F500").Value = Sheets("Sheet2").Range("B3:F500").Value
SheetObject.ListObjects (ListObjectName)
Range("NAME OF TABLE[Iteration Path]").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
The [Iteration Path] is a column name of my table, i want to check in this column/with this parameter, if the "row" is empty.
I know the code is far from being good or clean but the table is giving me a hard time copying.
With this code I got another problem: the table gets created from TFS, no problem to copy into that, BUT the name of the table is variable(seems like TFS creates the name), so unless I put the name manually in the code, the "program" cant execute, because of missing range.
Didn't find a way to get a return of the table name somehow.
But I think I am just following the wrong path overall, maybe someone can be bring me on the right track.
My other Idea is to Iterate through the Rows in Sheet 2 to fetch just as much data is needed and then copy them with an iteration into the table. But i guess I would be the same problem with the table-name there.
Every information I find using google , talks about tables where the user can "name" the table. In my case I cant, so I have to work with the name TFS uses for my table.
Further to my comments below your question, I just typed this in notepad. Please amend it to suit your need.
This will give you the names of all tables in the activesheet. If there are multiple tables then you will get multiple names. (UNTESTED AS POSTING FROM PHONE)
Sub sample()
Dim objLB As ListObject, TableName As String
For Each objLB In ActiveSheet.ListObjects
TableName = objLB.Name
Exit For
Next
Range(TableName & "[Iteration Path]").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub

How can I copy a sheet and have my macros still work?

First off I want to apologize if my question has already been answered elsewhere as I did spend some time searching.
I'm using two macros in a sheet (very simple macros... they filter a table) and this sheet is a template that will be used every week. When I copy the sheet and make a new one, the macros no longer work. The error I receive is Runtime Error 9; Subscript out of range. I looked at the VBA code (which I've never really learned VBA) and see it is referencing a table title. Is there a way to fix this so it doesn't reference that specific table but rather the cells contained in that table? Example of the title name:
ActiveSheet.ListObjects("Table1619").Range.AutoFilter Field:=1
I want to access the data from a cell range of A103:A113. I tried this:
ActiveSheet.ListObjects.Range("$A$103:$A$113").AutoFilter Field:=1
This didn't work either but the error I received was different. "Run-time error '438': Object doesn't support this property or method"
What I know would work is if there was a way to have the same table name across all sheets but my limited research has seemed to point me in the direction of this not being possible.
It's impossible to reference ActiveSheet.ListObjects.Range("$A$103:$A$113") like this - either as above in your first sample, OR convert table to normal range and then use ActiveSheet.Range("$A$103:$A$113").
The problem you're facing is that you access the Range through a Table/ListObject - but the ListObject changes the name during the copy, as each Table must have a unique name. The solution is simple - instead of accessing the ListObject with its name, simply use the index in the worksheet - that will not change. Therefore, replace
ActiveSheet.ListObjects("Table1619").Range.AutoFilter Field:=1
with
ActiveSheet.ListObjects(1).Range.AutoFilter Field:=1
(assuming it is the only/first table in the worksheet.)

Resources