Excel VBA Referencing Range.Row Object Variable - excel

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 <----------------

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

Excel Worksheet: Type mismatch error for worksheet added through inputbox

For a vlookup formula, I am using an inputbox option to select a couple of workbooks (OpenBook_PY and CY) for source data and creating a new workbook (Comp_Book) for main data.
The inputbox function is working successfully however I am facing problem when defining the worksheets in each of the above mentioned book.
Error is type 13 : mismatch error
The problem starts at the 3rd last line, when setting M_Sheet
Dim CY_TB As Variant
Dim OpenBook_CY As Workbook
Dim PY_TB As Variant
Dim OpenBook_PY As Workbook
Dim Comp_Book As Workbook
Set Comp_Book = Workbooks.Add
Application.ScreenUpdating = False
CY_TB = Application.GetOpenFilename(Title:="Open Current Period TBC", FileFilter:="Excel Files (*.xls*),*xls*")
Set OpenBook_CY = Application.Workbooks.Open(CY_TB)
If CY_TB <> False Then
OpenBook_CY.Activate
Worksheets(1).Select
range(range("B6"), range("B" & Rows.Count).End(xlUp)).Copy Comp_Book.Sheets(1).range("B6")
Dim CY_Rnge As range
End If
'TBC of PY
PY_TB = Application.GetOpenFilename(Title:="Open Prior Period TBC", FileFilter:="Excel Files (*.xls*),*xls*")
Set OpenBook_PY = Application.Workbooks.Open(PY_TB)
If PY_TB <> False Then
OpenBook_PY.Activate
Worksheets(1).Select
End If
Dim M_Sheet As Worksheet, CY_Sheet As Worksheet, PY_Sheet As Worksheet, N_Sheet As Worksheet
Dim M_LR As Long, CY_LR As Long, PY_LR As Long, r As Long
Dim CY_Rng As range, PY_Rng As range
Set M_Sheet = Workbooks(Comp_Book).Worksheets("Sheet1")
Set CY_Sheet = Workbooks(OpenBook_CY).Worksheets("Trial-New")
Set PY_Sheet = Workbooks(OpenBook_PY).Worksheets("Trial-New")
The problem is the Workbooks call:
Set M_Sheet = Workbooks(Comp_Book).Worksheets("Sheet1")
Comp_Book is a Workbook, so all you need is:
Set M_Sheet = Comp_Book.Worksheets("Sheet1")
... and similarly for the next two lines.

VBA code working when I step through it, but not when it's run

I have some basic VBA that is allowing a user to take a field from one table and use it to update another table. It works fine when I step through it, but nothing happens when I run it using F5. I don't get any errors, just nothing happens.
I think it could possibly be that the value hasn't been assigned to one of the variables before the next step occurs, but I've never had that problem before, and I've always assumed VBA wouldn't move to the next step until it had completed the one it's on?
My code is below:
Option Explicit
Sub acceptDateComp()
'set data type
Dim dtType As String
dtType = "opportunity"
'declare sheets
Dim wsComp As Worksheet
Set wsComp = ThisWorkbook.Sheets(dtType & "Comp")
Dim wsBCE As Worksheet
Set wsBCE = ThisWorkbook.Sheets(dtType & "Snapshot")
Dim wsOffline As Worksheet
Set wsOffline = ThisWorkbook.Sheets(dtType & "Database")
'declare tables
Dim bce As ListObject
Set bce = wsBCE.ListObjects(dtType & "Snapshot")
Dim offline As ListObject
Set offline = wsOffline.ListObjects(dtType & "Database")
Dim dateComp As ListObject
Set dateComp = wsComp.ListObjects(dtType & "DateComp")
'declare heights and areas
Dim offlineRange As Range
Set offlineRange = offline.ListColumns(1).DataBodyRange
'check for acceptance, then change values
Dim i As Long
Dim dateID As Long
Dim offlineRow As Long
Dim bceDate As String
For i = 2 To dateComp.ListRows.Count + 1
If dateComp.ListColumns(6).Range(i).Value = "Yes" Then
dateID = dateComp.ListColumns(1).Range(i).Value
offlineRow = Application.WorksheetFunction.Match(dateID, offlineRange, 0)
bceDate = dateComp.ListRows(i - 1).Range(5).Value
offline.ListRows(offlineRow).Range(12).Value = bceDate
End If
Next i
Call opportunityComp
End Sub

Does Excel VBA WorksheetFunction need to use a select to be on the proper worksheet

I have the following code, but I have to select the worksheet otherwise the WorksheetFunction will return an error:
Function Find_Allocation_Acct_Row_Number(allocation_acct As String, allocation_cc As String)
Dim WS As Worksheet
Dim CC_row As Integer
Dim search_rng As Range
Set WS = Sheets(allocation_cc)
WS.Select
Set search_rng = WS.Range("E1:E2000")
CC_row = WS.Application.WorksheetFunction.Match(allocation_acct,search_rng, 0)
Find_Allocation_Acct_Row_Number = CC_row + 4
End Function
How can I select the right range on the right worksheet without using the Select command?
To be clear, I will ALWAYS find the value. I just need to return the row so I can create a range from it.
I have tried a number of different ways to change the "search_rng" to different variables or expressions of Range but no luck.
The error I am getting is:
Run-time error '1004':
Unable to get the Match property of the WorksheetFunction class
If the match is not found it will kick out an error and stop the code. If we late bind the match and send the result to a variant we can test for the error:
Function Find_Allocation_Acct_Row_Number(allocation_acct As String, allocation_cc As String) As Long
Dim WS As Worksheet
Set WS = Worksheets(allocation_cc)
Dim search_rng As Range
Set search_rng = WS.Range("E1:E2000")
Dim CC_row As Variant
CC_row = Application.Match(allocation_acct, search_rng, 0)
If Not IsError(CC_row) Then
Find_Allocation_Acct_Row_Number = CC_row + 4
Else
Find_Allocation_Acct_Row_Number = -1
End If
End Function
Now if a match cannot be found it will return -1

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

Resources