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.










share|improve this question






















  • 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















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.










share|improve this question






















  • 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













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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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












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:








share|improve this answer























  • 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













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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:








share|improve this answer























  • 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

















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:








share|improve this answer























  • 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















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:








share|improve this answer














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:









share|improve this answer














share|improve this answer



share|improve this answer








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




















  • 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




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python