I want to insert the following formula into the third column of my Excel Table "Transactions". If I enter it manually, it works, but if I try to enter it using a subroutine I get the Error 1004. Not sure how to solve this. Here is a code snippet:
Sub test3()
Dim ws As Worksheet
Dim lo As ListObject
Dim lCol As ListColumn
Set ws = ThisWorkbook.Worksheets("Transactions")
Set lo = ws.ListObjects(1)
Set lColName = lo.ListColumns(3)
lColName.DataBodyRange.Formula = "=IFERROR(INDEX(Staff[CREDENTIALS],MATCH([#[Staff, Last Name]],LastName,0)),"")"
End Sub
All I really need to do is to put the formula into the first row of the table (row 2) in the third column "Staff, Credentials". It is pulling from another Excel Table "Staff".
I believe the issue is coming from your formula (I know you mentioned it works when you manually enter it however when I try I'm getting an error), try changing [#[Staff, Last Name]] to Staff[Last Name]
Also, you'll need to escape the "" in the IFERROR formula, the below code should work:
Sub test3()
Dim ws As Worksheet
Dim lo As ListObject
Dim lCol As ListColumn
Set ws = ThisWorkbook.Worksheets("Transactions")
Set lo = ws.ListObjects(1)
Set lColName = lo.ListColumns(3)
lColName.DataBodyRange.Formula = "=IFERROR(INDEX(Staff[CREDENTIALS],MATCH(Staff[Last Name],LastName,0)),"""")"
End Sub
Related
I'm trying to put together a very simple VBA script to clear cell contents within a specified table column range ([Front Straddle]:[Front Option]), of all tables within a specified worksheet. This script will only live within the "VolJump" worksheet, which contains an arbitrary number of identically formatted, differently named tables. Because of this, I felt the best approach was to reference the tables by the index number.
This is where I'm running into issues with the proper referencing/nesting within the 'Range' function, shown below. Any help is greatly appreciated.
Sub ClearCells()
Dim i As Long
Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("VolJump")
If sh.ListObjects.Count > 0 Then
For i = 1 To sh.ListObjects.Count
Range("Activesheet.ListObjects(1)[[Front Straddle]:[Front Option]]").Select
Selection.ClearContents
Next i
End If
End Sub
Clear Contents of Table Columns Range
Option Explicit
Sub ClearCells()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("VolJump")
Dim tbl As ListObject
For Each tbl In ws.ListObjects
ws.Range(tbl.Name & "[[Front Straddle]:[Front Option]]").ClearContents
Next tbl
End Sub
Just using the ListObjects:
Sub ClearColumns()
Dim lo As ListObject
Dim ColNum1 As Long, ColNum2 As Long
For Each lo In ThisWorkbook.Worksheets("Sheet1").ListObjects
'Get the index numbers of the start and end columns.
ColNum1 = lo.ListColumns("Front Straddle").Index
ColNum2 = lo.ListColumns("Front Option").Index
'Resize the range from the start column to the end column and clear it.
lo.DataBodyRange.Columns(ColNum1).Resize(, ColNum2 - ColNum1 + 1).ClearContents
Next lo
End Sub
I hope someone can help me with this as it's driving me up the wall!
There are 5 non-contiguous cells in a worksheet that I want to copy to the next empty row on another worksheet whilst retaining the number formatting (which varies). I have this so far but am struggling working out how to retain formatting. Can anyone please help? Thanks I anticipation.
`With wsCalc
For bRun = 1 To 4
bData(bRun) = Application.Choose(bRun, .Range("g2"), .Range("b2"), .Range("R2"), .Range("Q14"))
Next bRun
End With
wSResults.Cells(Rows.Count, "a").End(xlUp).Offset(1).Resize(, 4).Value = bData
`
Here's a possible solution, using your hard-coded cell addresses. You will have to set wsCalc and wsResults to their proper worksheets. Slightly more elegant would be to define a "non-contiguous" range on your wsCalc sheet (select the 1st cell, keep Ctrl pressed and select the next one etc, then type a name in the drop-down box just to the left of the formula bar).
Option Explicit
Sub CopyWithFormat()
Dim wsCalc As Worksheet
Set wsCalc = ActiveSheet 'Or whatever your calc sheet is
Dim rngSource As Range
Set rngSource = wsCalc.[G2,B2,R2,Q14]
Dim wsResults As Worksheet
Set wsResults = ActiveSheet 'Or whatever your result sheet is
Dim clDest As Range
Set clDest = wsResults.Cells(Rows.Count, "a").End(xlUp).Offset(1)
Dim cl As Range
For Each cl In rngSource.Cells
clDest.Value = cl.Value
clDest.NumberFormat = cl.NumberFormat
Set clDest = clDest.Offset(1)
Next cl
End Sub
Instead of using .Value, try .Text. It retains formatting. See below.
Gary's Student is right, text is read only, it should be used for the input not the output.
bData(bRun) = Application.Choose(bRun, .Range("g2").Text, .Range("b2").Text, .Range("R2").Text, .Range("Q14").Text)
I also agree with other answer the entire code could be set up more straight forward.
I am adding a new row to a table but want to then add the data to that row that I just added. I am thinking something like this, but not sure how to add each columns data to that new row. My table has 4 columns named "store" "emp#" "date" & "amt". I have specific data that I will put in each column. I simplified the code, as there is a whole lot more to the macro, but just stuck on this part. Thank you for you help.
Dim rt_ws As Worksheet
Dim rt_tbl As ListObject
Set rt_ws = ThisWorkbook.Worksheets("RT Clock Hours")
Set rt_tbl = rt_ws.ListObjects("rt_hours")
With rt_table.ListRows.Add
. `this is where I am not sure what to do`
.
.
End Sub
Try this code
Sub Test()
Dim ws As Worksheet, tbl As ListObject
Set ws = ThisWorkbook.Worksheets("RT Clock Hours")
Set tbl = ws.ListObjects("rt_hours")
With tbl.ListRows.Add
.Range = Array("Store1", "1530", "05/03/2020", "Amt1")
End With
End Sub
I'm trying to construct a macro to find the same part name as I have highlighted in my "Tests" sheet in my "Part Catalog" sheet.
From here, I would like to copy the date when the part was manufactured (which is one column to the right of the part name) from the "Part Catalog" sheet, and paste it in a cell one column to the right of the part name in the "Tests" sheet.
I get an error saying that "Object doesn't support this property or method."
The code below is taken and slightly modified from this link: (http://www.ozgrid.com/forum/showthread.php?t=158840&p=578982#post578982). Previous attempts included me using for loops, but the majority of people seem to agree that the .find function works the best for things like this.
Any help would be appreciated! Thank you!
Sub Get_Date()
Dim Partname As String
Dim sh As Worksheet
Dim ws As Worksheet
Set sh = Sheets("Tests")
Set ws = Sheets("Part Catalog")
Partname = ActiveCell.Value
ws.Cells.Find(Partname).Offset(0, 1).Copy
sh.Cells.Find(Partname).Offset(0, 1).Paste
End Sub
For your follow on question:
Assumes you have the entire list of part names selected first:
Dim sh As Worksheet
Dim ws As Worksheet
Dim c, rng As Range
Set sh = Sheets("Tests")
Set ws = Sheets("Part Catalog")
Set rng = Selection
For Each c In rng
ws.Cells.Find(c.Value).Offset(0, 1).Copy Destination:=c.Offset(0, 1)
Next c
This uses a loop to go through each part (cell) in your selection.
Create a VBA loop to re-size all of the tables in an Excel project.
A list of table names is stored in the "Tlist" named range
This code all works if the table names are written in.
The code works in this loop for the column count, but on the second reference of x , the re-size, I get an error of 'subscript out of range'
Hovering over the second x on debug, Excel seems to read it in right, but I can't get it to re-size the table Something to do with the list object function not supporting the text in the loop? Or am I doing this loop wrong and need to define x differently? Any help greatly appreciated.
Sub RSizeTables()
Dim rr As Integer
Dim cc As Integer
Dim x As Range
Dim £Table As Range
Set £Table = Range("Tlist")
For Each x In £Table
rr = 2
cc = Range(x).Columns.Count
With Sheets("Data").ListObjects(x)
.Resize .Range.Resize(rr, cc)
End With
Next x
End Sub
This code will resize all tables to two rows:
Sub ResizeAll()
Dim ws As Worksheet
Dim lo As ListObject
'If you only have tables in one sheet, use this
Set ws = Sheets("Data")
'Else this:
'For Each ws in Sheets
For Each lo In ws.ListObjects
lo.Resize lo.Range.Resize(2)
Next lo
'Next ws
End Sub
If you need to loop only the tables in TList, this will do the job:
Sub ResizeTList()
Dim varTableName As Variant
For Each varTableName In Range("TList")
With Sheets("Data").ListObjects(varTableName)
.Resize .Range.Resize(2)
End With
Next varTableName
End Sub