On 27 Sep, 19:44, Ron Rosenfeld <r...@nospam.net> wrote:
> On Mon, 27 Sep 2010 09:36:35 -0700 (PDT), Johnnyboy5
>
> <intermediatec...@gmail.com> wrote:
> >I would really love it – if after the message comes up if the answer
> >could also populate a cell in another sheet.
>
> >Say cell P18 *in worksheet “Main Totals” * in the same workbook as the
> >other data.
>
> Although you could certainly add a line like:
>
> worksheets("Main Totals").range("P18").value = mc
>
> I would rewrite it as a function, and enter that UDF into the cell
> where you want the result.
>
> In doing that, you could set it up so that you could also allow for
> some variables. *As written, I've allowed for the function to specify
> the string to search for, and also the worksheets to examine. *If you
> do not specify any worksheets, the function will look at all
> worksheets that have the three letter month abbreviation as the name;
> but if you specify any sheets, it will look only at those sheets.
>
> Just an example of some of the things that can be done.
>
> ===================================
> Option Explicit
> Function CountStuff(SearchString As String, _
> * * * * ParamArray MonthNames() As Variant) As Long
> * Dim sMonths() As String
> * Dim i As Long
> * Dim ws As Worksheet, ValidWS As Boolean
> * Dim mycol As Range
> * Dim mc As Long
>
> * 'If no months are specified then check all months
> * * If UBound(MonthNames) = -1 Then
> * * * * ReDim sMonths(1 To 12)
> * * * * For i = 1 To 12
> * * * * * * sMonths(i) = Format(DateSerial(2000, i, 1), "mmm")
> * * * * Next i
> * * * Else
> * * * * ReDim sMonths(1 To UBound(MonthNames) - _
> * * * * LBound(MonthNames) + 1)
> * * * * For i = LBound(MonthNames) To UBound(MonthNames)
> * * * * * * sMonths(i + IIf(LBound(MonthNames) = 0, _
> * * * * * * * * 1, 0)) = MonthNames(i)
> * * * * Next i
> * * End If
>
> For Each ws In Worksheets
> * * ValidWS = False
> * * On Error Resume Next
> * * * * ValidWS = WorksheetFunction.Match(ws.Name, sMonths, 0)
> * * On Error GoTo 0
> * * If ValidWS Then
> * * * * Set mycol = ws.Columns("N")
> * * * * mc = mc + WorksheetFunction.CountIf(mycol, SearchString)
> * * End If
> Next ws
>
> MsgBox (mc) 'you might not want this line
> CountStuff = mc
>
> End Function
> ===================================
Ahh, read stuff.... got it to work, but I did change the months
names in your last post some are four letter not three...any ideas
I see where you are going with this User Designed Function.... nice
Johnny
|