Naming a listobject with a string variable - object

Sub ShowHideTable()
Dim table As ListObject
Dim tblTrngl As Shape
Dim tblName As String
Call Shape_Clicked(tblTrngl)
Let tblName = tblTrngl.Name
tblName = Replace(tblName, "Trngl", "")
Let table.Name = tblName
I've debugged it and the tblName is exactly what I want it to be, but when I try to set the name of the listobject I get an error.
The last line gives an error no matter how I set it. What am I doing wrong?

Set table = ThisWorkbook.ActiveSheet.ListObjects(tblName)

Related

VBA resizes table causes a copy of data validation un the entire workbook

I'm trying to make a resize table VBA and that one worked but when I use it it copies also the data validation in another part of the file. These are not in a table. And are copied along with along.
I think that is has someting to do with the fact that 1 table has a data validation in it.
How can I prevent this from happening?
Sub Resize_Table()
Dim aSheetname As Worksheet
Dim bSheetname As Worksheet
Dim cSheetname As Worksheet
Dim aaSheetname As Worksheet
Dim aTableName As String
Dim bTableName As String
Dim cTableName As String
Dim aaTableName As String
Dim aTable As ListObject
Dim bTable As ListObject
Dim cTable As ListObject
Dim aaTable As ListObject
Dim aRows As Integer
Dim bRows As Integer
Dim cRows As Integer
Dim aaRows As Integer
'Define
aTableName = "PS_Calc"
bTableName = "Prices"
cTableName = "Handling"
aaTableName = "Testing"
'Werkblad
Set aSheetname = Sheets("PS Calc")
Set bSheetname = Sheets("Prices")
Set cSheetname = Sheets("Handling")
Set aaSheetname = Sheets("Tests")
'Define tabel
Set aTable = aSheetname.ListObjects(aTableName)
Set bTable = bSheetname.ListObjects(bTableName)
Set cTable = cSheetname.ListObjects(cTableName)
Set aaTable = aaSheetname.ListObjects(aaTableName)
'Looking up rows
aRows = aTable.Range.Rows.Count
bRows = bTable.Range.Rows.Count
cRows = cTable.Range.Rows.Count
aaRows = aaTable.Range.Rows.Count
'Add how many rows
aNewRows = Worksheets("PS Calc").Range("N20")
'Resize
aTable.Resize aTable.Range.Resize(aRows + aNewRows)
bTable.Resize bTable.Range.Resize(bRows + aNewRows)
cTable.Resize cTable.Range.Resize(cRows + aNewRows)
aaTable.Resize aaTable.Range.Resize(aaRows + aNewRows)
End Sub

Reading Date from Excel file in VBA Error Message

I have a function that gets the date from a certain cell in an excel sheet, splits the string that was in that cell using a space as a delimiter and keeps the first object in the array to store at as the date. However, I keep getting the following error message
"run-time error '91': Object variable or With block variable not set".
Any ideas of what might be wrong?
Public Function getDate(ws As Excel.Worksheet, row As Integer, col As Integer) As String
Dim inspDateTime As Variant
Dim dateArr() As String
Dim inspDateFinal As String
Set inspDateTime = ws.Cells(row, col)
dateArr = Split(inspDateTime, " ")
inspDateFinal = dateArr(0)
getDate = inspDateFinal
End Function
The following code is how I call the function:
Private Sub UploadData_Click()
Dim myWorkbook As Excel.Workbook
Dim myWorksheet As Excel.Worksheet
Dim inspDate As String
inspDate = getDate(myWorksheet, 10, 14)
' setting the workbook as the directory that was printed to the text box
Set myWorkbook = Workbooks.Open(Me.FilePathTxtBox)
' setting the first worksheet in the array of files entered
Set myWorksheet = myWorkbook.Worksheets(1)
End Sub

Excel VBA Referencing Range.Row Object Variable

I'm currently creating an Excel workbook that uses vba to display info from a table stored in the same workbook. I keep getting the error "Object variable or with block variable not set" when trying to run my code. The error comes when I try to read my found line # into my rownum long value. Probably a simple fix along with other unrelated issues but any help is appreciated.
Dim findID As Long
Dim ws As Worksheet
Dim tbl As ListObject
Dim loc As Range
Dim foundLast As String
Dim foundFirst As String
Dim rownum As Long
Dim nameCol As Range
findID = HoursID.Value
Set ws = ActiveWorkbook.Worksheets("Totals")
Set tbl = ws.ListObjects("Totals")
Set nameCol = ws.Range("A2:A500")
Set loc = nameCol.Find(findID, SearchOrder:=xlByRows)
rownum = loc.Row <----------------

Trying to count the number of matching items in a table column

I am trying to determine the number of items in a column that match a specific string. I want to be able to use this number to size an array later.
I have been trying various ways to use the countif function.
Sub testMatrix()
Dim nm1 As String
Dim nm2 As String
Dim tbl As ListObject
Dim nm1Count As Double
Dim nm2Count As Integer
nm1 = "teleport 1"
nm2 = "user2"
Set tbl = ActiveSheet.ListObject("Table1")
nm1Count = Application.WorksheetFunction.CountIf(Range("Table1[username]"), nm1)
End Sub
I would like to end up with the variable nm1Count being equal to the number of times string "teleport 1" occurs in the username column of my table. So for my specific sheet it should say 4.
Currently, when it gets to the nm1Count = line it errors out and says
Object doesn't support this property or method
I dumbly forgot the s at the end of ListObjects. Here is the corrected code.
Sub testMatrix()
Dim nm1 As String
Dim nm2 As String
Dim tbl As ListObject
Dim nm1Count As Long
Dim nm2Count As Long
nm1 = "teleport 1"
nm2 = "user2"
ActiveSheet.Activate
Set tbl = ActiveSheet.ListObjects("Table1")
nm1Count = Application.WorksheetFunction.CountIf(tbl.DataBodyRange.Columns(1), nm1)
nm2Count = Application.WorksheetFunction.CountIf(tbl.DataBodyRange.Columns(1), nm2)
End Sub

Object.Namespace path error

I wrote this code to return some properties from file:
Dim strMTitle As String
Dim objMshell As Object
Dim objMfolder As Object
Dim objMFolderItem As Object
Dim strMpath As String
strMpath = "C:\Users\User1\Desktop\Test4\"
Set objMshell = CreateObject("shell.application")
Set objMfolder = objMshell.Namespace(strMpath)
Set objMFolderItem = objMfolder.ParseName("test2.xlsm")
strMTitle = objMfolder.GetDetailsOf(objMFolderItem, 21)
Debug.Print strMTitle
The problem is that it keeps returning run time error 91 - Object variable with block variable not set. Weirdest thing is that when I "Hardcode" objMfolder with path like this:
Set objMfolder = objMshell.Namespace("C:\Users\User1\Desktop\Test4\") the code works perferct.
I use this path in multiple places in my macro so I would really like to "store" it in strMpath and use it like this:
Set objMfolder = objMshell.Namespace(strMpath)
Please help!
The code seems to work with the string variable if you use early-binding and the Shell32.Shell as below. Also, .GetDetailsOf with a column argument of 21 returns nothing, but 0 returns the file name.
Option Explicit
'Set Reference to Microsoft Shell Controls and Automation
Sub dural()
Dim strMTitle As String
Dim objMshell As Shell32.Shell
Dim objMfolder As Folder
Dim objMFolderItem As FolderItem
Dim strMpath As String
strMpath = "C:\Users\Ron\Desktop\"
Set objMshell = New Shell32.Shell
Set objMfolder = objMshell.Namespace(strMpath)
Set objMFolderItem = objMfolder.ParseName("20161104.csv")
strMTitle = objMfolder.GetDetailsOf(objMFolderItem, 0)
Debug.Print strMTitle
End Sub

Resources