Excel vba function with dynamic table column as parameter
up vote
0
down vote
favorite
I'm trying to do an excel that counts the number of times that a certain letter appears in a table column.
What I'm trying to do is to create a dynamic table, in which i can always add new lines. Because of that, i need a function that has as a parameter one of the columns, which counts the number of times that another parameter appears.
Example, count the number of "shift" in the "column" (:
Function sumColumnShifts(column As Integer, shift As range) As Integer
sumColumnShifts = sumDayShifts(ActiveSheet.ListObjects("foo").ListColumns(column).range.Select, shift)
End Function
Public Function sumDayShifts(ByVal Target As range, shift As range) As Variant
Dim res As Integer
res = 0
For Each cell In Target
If shift.cells(1, 1).Value = cell.Value Then
res = res + 1
End If
Next
sumDayShifts = res
End Function
The problem here is the function can't find the table, but the table exists.
What am I doing wrong?
Is it the ActiveSheet.ListObjects("foo").ListColumns(column).range.Select?
This is not a range? I can't access this in a function?
Thanks.
excel vba
add a comment |
up vote
0
down vote
favorite
I'm trying to do an excel that counts the number of times that a certain letter appears in a table column.
What I'm trying to do is to create a dynamic table, in which i can always add new lines. Because of that, i need a function that has as a parameter one of the columns, which counts the number of times that another parameter appears.
Example, count the number of "shift" in the "column" (:
Function sumColumnShifts(column As Integer, shift As range) As Integer
sumColumnShifts = sumDayShifts(ActiveSheet.ListObjects("foo").ListColumns(column).range.Select, shift)
End Function
Public Function sumDayShifts(ByVal Target As range, shift As range) As Variant
Dim res As Integer
res = 0
For Each cell In Target
If shift.cells(1, 1).Value = cell.Value Then
res = res + 1
End If
Next
sumDayShifts = res
End Function
The problem here is the function can't find the table, but the table exists.
What am I doing wrong?
Is it the ActiveSheet.ListObjects("foo").ListColumns(column).range.Select?
This is not a range? I can't access this in a function?
Thanks.
excel vba
Does the table have headers you can use instead?
– QHarr
Nov 10 at 17:58
The headers are the default ones when I create a table (Column1, Column2, etc)
– Enorio
Nov 10 at 18:03
So you can pass that as a parameter.
– QHarr
Nov 10 at 18:04
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to do an excel that counts the number of times that a certain letter appears in a table column.
What I'm trying to do is to create a dynamic table, in which i can always add new lines. Because of that, i need a function that has as a parameter one of the columns, which counts the number of times that another parameter appears.
Example, count the number of "shift" in the "column" (:
Function sumColumnShifts(column As Integer, shift As range) As Integer
sumColumnShifts = sumDayShifts(ActiveSheet.ListObjects("foo").ListColumns(column).range.Select, shift)
End Function
Public Function sumDayShifts(ByVal Target As range, shift As range) As Variant
Dim res As Integer
res = 0
For Each cell In Target
If shift.cells(1, 1).Value = cell.Value Then
res = res + 1
End If
Next
sumDayShifts = res
End Function
The problem here is the function can't find the table, but the table exists.
What am I doing wrong?
Is it the ActiveSheet.ListObjects("foo").ListColumns(column).range.Select?
This is not a range? I can't access this in a function?
Thanks.
excel vba
I'm trying to do an excel that counts the number of times that a certain letter appears in a table column.
What I'm trying to do is to create a dynamic table, in which i can always add new lines. Because of that, i need a function that has as a parameter one of the columns, which counts the number of times that another parameter appears.
Example, count the number of "shift" in the "column" (:
Function sumColumnShifts(column As Integer, shift As range) As Integer
sumColumnShifts = sumDayShifts(ActiveSheet.ListObjects("foo").ListColumns(column).range.Select, shift)
End Function
Public Function sumDayShifts(ByVal Target As range, shift As range) As Variant
Dim res As Integer
res = 0
For Each cell In Target
If shift.cells(1, 1).Value = cell.Value Then
res = res + 1
End If
Next
sumDayShifts = res
End Function
The problem here is the function can't find the table, but the table exists.
What am I doing wrong?
Is it the ActiveSheet.ListObjects("foo").ListColumns(column).range.Select?
This is not a range? I can't access this in a function?
Thanks.
excel vba
excel vba
asked Nov 10 at 17:43
Enorio
33
33
Does the table have headers you can use instead?
– QHarr
Nov 10 at 17:58
The headers are the default ones when I create a table (Column1, Column2, etc)
– Enorio
Nov 10 at 18:03
So you can pass that as a parameter.
– QHarr
Nov 10 at 18:04
add a comment |
Does the table have headers you can use instead?
– QHarr
Nov 10 at 17:58
The headers are the default ones when I create a table (Column1, Column2, etc)
– Enorio
Nov 10 at 18:03
So you can pass that as a parameter.
– QHarr
Nov 10 at 18:04
Does the table have headers you can use instead?
– QHarr
Nov 10 at 17:58
Does the table have headers you can use instead?
– QHarr
Nov 10 at 17:58
The headers are the default ones when I create a table (Column1, Column2, etc)
– Enorio
Nov 10 at 18:03
The headers are the default ones when I create a table (Column1, Column2, etc)
– Enorio
Nov 10 at 18:03
So you can pass that as a parameter.
– QHarr
Nov 10 at 18:04
So you can pass that as a parameter.
– QHarr
Nov 10 at 18:04
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Can't you just use
=COUNTIF(foo[[#All],[Column1]],"A")
Otherwise,
I would pass the ListObject name and header of column to search along with the search value and use Countif in the function to return the count. You could also alter the function to pass the worksheet as an argument to the function to make it more flexible.
Option Explicit
Public Sub Test()
Const SEARCH_HEADER As String = "Column1"
Const SEARCH_VALUE As String = "A"
Const TABLE_NAME As String = "foo"
Debug.Print GetCount("foo", SEARCH_HEADER, SEARCH_VALUE)
End Sub
Public Function GetCount(ByVal tableName As String, ByVal searchHeader As String, ByVal searchValue As String) As Variant
Dim ws As Worksheet, table As ListObject
Set ws = ThisWorkbook.Worksheets("Sheet1")
On Error Resume Next
Set table = ws.ListObjects("foo")
On Error GoTo 0
If table Is Nothing Or IsError(Application.Match(table.HeaderRowRange, searchHeader, 0)) Then
GetCount = CVErr(xlErrNA)
Exit Function
End If
GetCount = Application.WorksheetFunction.CountIf(table.ListColumns(searchHeader).DataBodyRange, searchValue)
End Function
Data:
And how do i call this function? In a cell i write =GetCount(foo;"Column1","Value")
– Enorio
Nov 10 at 18:40
TBH you can just put =COUNTIF(foo[[#All],[Column1]],"A") and change the Column1 and A as required.
– QHarr
Nov 10 at 19:10
And yes I have edited so you can call as =GetCount(foo,"Column1","Value")
– QHarr
Nov 10 at 19:16
1
The getcount was not working, couldn't found the table. Apparently is just =CONTAR.SE(Tabela1[Coluna6];"A") and it works. It's in PT the excel. Thanks for the help, as you can see, i'm worse than a beginner :)
– Enorio
Nov 10 at 19:23
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Can't you just use
=COUNTIF(foo[[#All],[Column1]],"A")
Otherwise,
I would pass the ListObject name and header of column to search along with the search value and use Countif in the function to return the count. You could also alter the function to pass the worksheet as an argument to the function to make it more flexible.
Option Explicit
Public Sub Test()
Const SEARCH_HEADER As String = "Column1"
Const SEARCH_VALUE As String = "A"
Const TABLE_NAME As String = "foo"
Debug.Print GetCount("foo", SEARCH_HEADER, SEARCH_VALUE)
End Sub
Public Function GetCount(ByVal tableName As String, ByVal searchHeader As String, ByVal searchValue As String) As Variant
Dim ws As Worksheet, table As ListObject
Set ws = ThisWorkbook.Worksheets("Sheet1")
On Error Resume Next
Set table = ws.ListObjects("foo")
On Error GoTo 0
If table Is Nothing Or IsError(Application.Match(table.HeaderRowRange, searchHeader, 0)) Then
GetCount = CVErr(xlErrNA)
Exit Function
End If
GetCount = Application.WorksheetFunction.CountIf(table.ListColumns(searchHeader).DataBodyRange, searchValue)
End Function
Data:
And how do i call this function? In a cell i write =GetCount(foo;"Column1","Value")
– Enorio
Nov 10 at 18:40
TBH you can just put =COUNTIF(foo[[#All],[Column1]],"A") and change the Column1 and A as required.
– QHarr
Nov 10 at 19:10
And yes I have edited so you can call as =GetCount(foo,"Column1","Value")
– QHarr
Nov 10 at 19:16
1
The getcount was not working, couldn't found the table. Apparently is just =CONTAR.SE(Tabela1[Coluna6];"A") and it works. It's in PT the excel. Thanks for the help, as you can see, i'm worse than a beginner :)
– Enorio
Nov 10 at 19:23
add a comment |
up vote
0
down vote
accepted
Can't you just use
=COUNTIF(foo[[#All],[Column1]],"A")
Otherwise,
I would pass the ListObject name and header of column to search along with the search value and use Countif in the function to return the count. You could also alter the function to pass the worksheet as an argument to the function to make it more flexible.
Option Explicit
Public Sub Test()
Const SEARCH_HEADER As String = "Column1"
Const SEARCH_VALUE As String = "A"
Const TABLE_NAME As String = "foo"
Debug.Print GetCount("foo", SEARCH_HEADER, SEARCH_VALUE)
End Sub
Public Function GetCount(ByVal tableName As String, ByVal searchHeader As String, ByVal searchValue As String) As Variant
Dim ws As Worksheet, table As ListObject
Set ws = ThisWorkbook.Worksheets("Sheet1")
On Error Resume Next
Set table = ws.ListObjects("foo")
On Error GoTo 0
If table Is Nothing Or IsError(Application.Match(table.HeaderRowRange, searchHeader, 0)) Then
GetCount = CVErr(xlErrNA)
Exit Function
End If
GetCount = Application.WorksheetFunction.CountIf(table.ListColumns(searchHeader).DataBodyRange, searchValue)
End Function
Data:
And how do i call this function? In a cell i write =GetCount(foo;"Column1","Value")
– Enorio
Nov 10 at 18:40
TBH you can just put =COUNTIF(foo[[#All],[Column1]],"A") and change the Column1 and A as required.
– QHarr
Nov 10 at 19:10
And yes I have edited so you can call as =GetCount(foo,"Column1","Value")
– QHarr
Nov 10 at 19:16
1
The getcount was not working, couldn't found the table. Apparently is just =CONTAR.SE(Tabela1[Coluna6];"A") and it works. It's in PT the excel. Thanks for the help, as you can see, i'm worse than a beginner :)
– Enorio
Nov 10 at 19:23
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Can't you just use
=COUNTIF(foo[[#All],[Column1]],"A")
Otherwise,
I would pass the ListObject name and header of column to search along with the search value and use Countif in the function to return the count. You could also alter the function to pass the worksheet as an argument to the function to make it more flexible.
Option Explicit
Public Sub Test()
Const SEARCH_HEADER As String = "Column1"
Const SEARCH_VALUE As String = "A"
Const TABLE_NAME As String = "foo"
Debug.Print GetCount("foo", SEARCH_HEADER, SEARCH_VALUE)
End Sub
Public Function GetCount(ByVal tableName As String, ByVal searchHeader As String, ByVal searchValue As String) As Variant
Dim ws As Worksheet, table As ListObject
Set ws = ThisWorkbook.Worksheets("Sheet1")
On Error Resume Next
Set table = ws.ListObjects("foo")
On Error GoTo 0
If table Is Nothing Or IsError(Application.Match(table.HeaderRowRange, searchHeader, 0)) Then
GetCount = CVErr(xlErrNA)
Exit Function
End If
GetCount = Application.WorksheetFunction.CountIf(table.ListColumns(searchHeader).DataBodyRange, searchValue)
End Function
Data:
Can't you just use
=COUNTIF(foo[[#All],[Column1]],"A")
Otherwise,
I would pass the ListObject name and header of column to search along with the search value and use Countif in the function to return the count. You could also alter the function to pass the worksheet as an argument to the function to make it more flexible.
Option Explicit
Public Sub Test()
Const SEARCH_HEADER As String = "Column1"
Const SEARCH_VALUE As String = "A"
Const TABLE_NAME As String = "foo"
Debug.Print GetCount("foo", SEARCH_HEADER, SEARCH_VALUE)
End Sub
Public Function GetCount(ByVal tableName As String, ByVal searchHeader As String, ByVal searchValue As String) As Variant
Dim ws As Worksheet, table As ListObject
Set ws = ThisWorkbook.Worksheets("Sheet1")
On Error Resume Next
Set table = ws.ListObjects("foo")
On Error GoTo 0
If table Is Nothing Or IsError(Application.Match(table.HeaderRowRange, searchHeader, 0)) Then
GetCount = CVErr(xlErrNA)
Exit Function
End If
GetCount = Application.WorksheetFunction.CountIf(table.ListColumns(searchHeader).DataBodyRange, searchValue)
End Function
Data:
edited Nov 10 at 19:15
answered Nov 10 at 18:11
QHarr
25.4k81839
25.4k81839
And how do i call this function? In a cell i write =GetCount(foo;"Column1","Value")
– Enorio
Nov 10 at 18:40
TBH you can just put =COUNTIF(foo[[#All],[Column1]],"A") and change the Column1 and A as required.
– QHarr
Nov 10 at 19:10
And yes I have edited so you can call as =GetCount(foo,"Column1","Value")
– QHarr
Nov 10 at 19:16
1
The getcount was not working, couldn't found the table. Apparently is just =CONTAR.SE(Tabela1[Coluna6];"A") and it works. It's in PT the excel. Thanks for the help, as you can see, i'm worse than a beginner :)
– Enorio
Nov 10 at 19:23
add a comment |
And how do i call this function? In a cell i write =GetCount(foo;"Column1","Value")
– Enorio
Nov 10 at 18:40
TBH you can just put =COUNTIF(foo[[#All],[Column1]],"A") and change the Column1 and A as required.
– QHarr
Nov 10 at 19:10
And yes I have edited so you can call as =GetCount(foo,"Column1","Value")
– QHarr
Nov 10 at 19:16
1
The getcount was not working, couldn't found the table. Apparently is just =CONTAR.SE(Tabela1[Coluna6];"A") and it works. It's in PT the excel. Thanks for the help, as you can see, i'm worse than a beginner :)
– Enorio
Nov 10 at 19:23
And how do i call this function? In a cell i write =GetCount(foo;"Column1","Value")
– Enorio
Nov 10 at 18:40
And how do i call this function? In a cell i write =GetCount(foo;"Column1","Value")
– Enorio
Nov 10 at 18:40
TBH you can just put =COUNTIF(foo[[#All],[Column1]],"A") and change the Column1 and A as required.
– QHarr
Nov 10 at 19:10
TBH you can just put =COUNTIF(foo[[#All],[Column1]],"A") and change the Column1 and A as required.
– QHarr
Nov 10 at 19:10
And yes I have edited so you can call as =GetCount(foo,"Column1","Value")
– QHarr
Nov 10 at 19:16
And yes I have edited so you can call as =GetCount(foo,"Column1","Value")
– QHarr
Nov 10 at 19:16
1
1
The getcount was not working, couldn't found the table. Apparently is just =CONTAR.SE(Tabela1[Coluna6];"A") and it works. It's in PT the excel. Thanks for the help, as you can see, i'm worse than a beginner :)
– Enorio
Nov 10 at 19:23
The getcount was not working, couldn't found the table. Apparently is just =CONTAR.SE(Tabela1[Coluna6];"A") and it works. It's in PT the excel. Thanks for the help, as you can see, i'm worse than a beginner :)
– Enorio
Nov 10 at 19:23
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241722%2fexcel-vba-function-with-dynamic-table-column-as-parameter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Does the table have headers you can use instead?
– QHarr
Nov 10 at 17:58
The headers are the default ones when I create a table (Column1, Column2, etc)
– Enorio
Nov 10 at 18:03
So you can pass that as a parameter.
– QHarr
Nov 10 at 18:04