Selecting cells on 2007 tables with vba

latecnik

New Member
Joined
Sep 17, 2009
Messages
5
Hello there!,

I´m trying to use VBA to select and manipulate one cell of the active row of the current table using Excel 2007 terminology, (in order to change its value), something like this:

Application.Range("PAYMENTS[[#This Row],[LASTNAME]]").Value = "Smith"

where the table is PAYMENTS and the field is LASTNAME,

However, this way will generate runtime errors and won´t seem to work...

The only way I´ve found to achieve it is to go there (to the cell) and then change the selection´s value, like this, (which looks inefficient and time consuming to me):

Application.Goto Reference:="PAYMENTS[[#This Row],[LASTNAME]]"
Selection.Value = "Smith"

Do you know how to achive that manipulation with more efficient VBA instructions?

Thank you very much in advance for your help!

LaTecniK
 

Jobowo

Board Regular
Joined
Oct 21, 2009
Messages
242
I have a certain amount of personal distaste for Excel's new table referencing system but in a quick check it doesn't seem to have made its way into Visual Basic as far as I can tell. So the syntax of range("Table[[row][column]]") seems to be a problem.

To answer your question though, if you have a range and a row and column number, you can reference it by .cell(row,col). So if your table is indeed named "Table", it would look something like:

.range("Table").cells(r,c)="Some value" or perhaps .range("$A$2:$D500").cells(r,c)

Note that the r,c are relative to the current table; they are not the absolute row and column. If you want to do it that way, then obviously that would be

activesheet.cells(r,c)

You can use .["Table"].cells(r,c) = "Some value" if you are using named ranges.

In general, it's best to place the value in the cell without selecting the cell first. Each action actually affecting the sheet really slows things down so 1 action is better than the 2 required with select.
 

jim may

Well-known Member
Joined
Jul 4, 2004
Messages
7,453
In a Standard Module enter:

Code:
Sub EnterName()  'Use/Assign Short_Cut_Key  Ctrl+Shift+E
Dim V As String
V = "Smith"
Sheets("Payments").Cells(ActiveCell.Row, 7) = V  'Lastname is Column G
End Sub
 

Forum statistics

Threads
1,081,524
Messages
5,359,269
Members
400,523
Latest member
ExcelNewbie98

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top