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

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

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

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

Get the Nth index of an array in VBA

I am a noob in VBA and can't find a way to get the element of an array at a given index... It might be easy for you, though.
I have an excel file with 2 columns, "Emails" and "Categories", and I want to filter out all emails for a given category.
I ended up so far with the following code:
Sub filterEmails()
Dim tbl As ListObject
Dim emails As Variant
Dim email As String
Dim categories As Variant
Dim category As String
Dim i As Integer
Set tbl = ActiveWorkbook.Worksheets("Feuil1").ListObjects("Tableau1")
emails = tbl.ListColumns("EMAILS").DataBodyRange.Value
categories = tbl.ListColumns("SERVICES").DataBodyRange.Value
i = 1
For Each email In emails
category = ???
If category = "some service" Then
MsgBox email
End If
i = i + 1
Next email
End Sub
I tried many ways to get the ith item from the categories array, like categories(i) but didn't succeed. It might be because I wasn't able to initialize variables with the right type.
I would do it this way:
Sub filterEmails()
Dim tbl As ListObject
Dim emails As Variant
Dim email As String
Dim categories As Variant
Dim category As String
Dim i As Long '<< always best to prefer Long over Integer
Set tbl = ActiveWorkbook.Worksheets("Feuil1").ListObjects("Tableau1")
'Both "emails" and "categories" will be 2-D arrays
emails = tbl.ListColumns("EMAILS").DataBodyRange.Value
categories = tbl.ListColumns("SERVICES").DataBodyRange.Value
For i = lbound(emails,1) to ubound(emails, 1)
category = categories(i, 1)
If category = "some service" Then
MsgBox email
End If
Next i
End Sub
Here's your code, changed it a little, It should work now:
Option Explicit
Sub filterEmails()
Dim tbl As ListObject
Dim emails As Variant
Dim email As Variant
Dim categories As Variant
Dim category As String
Dim i As Integer
Set tbl = ActiveWorkbook.Worksheets("Feuil1").ListObjects("Tableau1")
emails = tbl.ListColumns("EMAILS").DataBodyRange.Value
categories = Application.Transpose(tbl.ListColumns("SERVICES").DataBodyRange.Value)
i = 1
For Each email In emails
category = categories(i)
If category = "some service" Then
MsgBox email
End If
i = i + 1
Next email
End Sub
Comments:
categories(i)
That command wont work because categories is a 2 dimension array, I store it as 1 dimensional array using Application.transpose command.

How to write to excel from vba

I am trying to create a button on my Access form that takes the current entry and writes the information to an Excel doc
Public Sub ExportExcel()
Dim ObjEx As Object
Dim WhatIsThisVariableFor As Object
Dim Selec As Object
Dim Column As Integer
Dim Row As Integer
Dim CustName As String
Dim Org As String
Dim Contact As String
Dim Product As String
Dim Quantity As Integer
Dim rst As DAO.Recordset
Set ObjEx = CreateObject("EXCEL.APPLICATION")
Set WhatIsThisVariableFor = ObjEx.Workbooks.Add
'Set rst = CurrentDb.OpenRecordset("Select") <---- This will be used when I get the code working
Column = 1
Row = 1
CustName = "Test" '<---- This is only used for the test
Cells(Row, Column) = CustName
ObjEx.Visible = True
Set Selec = ObjEx.Selection
End Sub
This code creates the Excel doc, but leaves it blank. I think that the Cells(Row, Column) command isn't working because it would have to be called from within excel? I'm not sure (I am very new to VBA)
How can I write to the spreadsheet cells from within Access?
You need to qualify your Cells() function to Excel's application object.
ObjEx.ActiveSheet.Cells(Row, Column).Value = CustName
I would recommend that you also choose the worksheet object explicitly:
Dim ws As object
Set ws = ObjEx.Worksheets("Sheet1")
ws.Cells(row, column).value = CustName

Resources