-1

I have a userform that allows the user to enter "from" and "to" dates to search through the data that corresponds with the date range the user selects. In the spreadsheet, the date is in column A, and there is a series of data that corresponds to that date in the following columns for each row through column W. I'm trying to develop a code that can take those two dates and look at only the rows that have a date that falls into the entered date range in Column A, and then count the responses in each of the columns within the rows that have dates within the specified range. I'd then like to put the count values into a specific cell for each response's count in a separate worksheet within the same workbook. There are 6 defined responses for each possible response column, so the countif function seems to me to be the most logical.

This sounds rather complicated, but this is my best way to summarize it. I'm open to using autofilters or anything else, but it must be done using vba, and if it uses an autofilter, then it must be returned to the pre-autofilter screen at the end of the sub.

EDIT:

OK, I guess I wasn't very clear. To your first question, the reason why it goes to W is because there are a couple other items associated with each row that are not necessarily relevant for this analysis. The columns with relevant data responses are columns D through W. There is only one date entered into each row, and that is in column A (you can ignore/skip columns B & C). For each column (in this case, a question on a survey), there are 6 defined possible responses that could be entered.

So, for example, D3 could say "Strongly agree", "Somewhat agree", "Somewhat disagree", "Strongly disagree", "No response", or "Not sure/not applicable". This is the case for all of the questions associated with each column. Therefore, I want to be able to pull up a row that has a date that falls within two designated dates (beginning date and end date of range), and then looks across the row to columns D through W and counts the number of the 6 possible responses (described above) for each column ( or "question"). I then want the value of the counts for each possible response within each column to be copied to a specific cell in another worksheet (in this case, Sheet3).

Yes, I meant to say that it needs to filter through column A responses that fall within the specified date range, and then run the countifs for each of the possible responses for each of the 16 columns in the rows that met the date criteria for column A. Does that make sense?

I am open to using any kind of advanced filter or autofilter, but if there are other ideas out there to sort through the dates in Column A based on two userform designated dates, then look to the corresponding rows to the 16 question columns and count the number of each of the 6 possible responses for each question, and put that count into a designated cell on another worksheet (Sheet3).

I've tried to do it as a countif and copy and paste for each possible response for each column after attempting an autofilter, but it wasn't actually filtering it. It seems like even if I got it to work, this wouldn't be the most efficient way of doing it. Here's the code for two of the responses for one column as an example of my coding attempt (the full code accounts for 6 possible responses for 15 columns, so it seemed overly long to include here):

 Private Sub cbOkDateEnter_Click()

Dim ws As Worksheet
    Set ws1 = ThisWorkbook01.Sheets("Sheet1")

With Range("A1:W" & lr)
    .AutoFilter Field:=1, Criteria1:=">=" & tbEnterDate01, Operator:=xlAnd,          Criteria2:="<=" & tbEnterDate02

Dim sum01a, sum01b as Variant

sum01a = Application.WorksheetFunction.CountIf(Sheets("Sheet1").Range("D2:D5000"),     "Strongly disagree")
    Worksheets("Sheet3").Range("J12").Value = sum01a
sum01b = Application.WorksheetFunction.CountIf(Sheets("Sheet1").Range("D2:D5000"), "Somewhat disagree")
    Worksheets("Sheet3").Range("J13").Value = sum01b
End Sub

Any help would be greatly appreciated! Thanks!

1
  • I apologize. I added a part of my code that I worked on as an example of one of my attempts at it.
    – Ryan
    Sep 3, 2013 at 18:55

2 Answers 2

0

I am not sure why you use ColumnW with 15 columns (don't really have any idea of what is where!) but am curious whether this is any help (for running in a new sheet):

Sub Macro1()
    Range("D3").Select
    ActiveCell.FormulaR1C1 = _
        "=COUNTIFS(Sheet1!R2C1:R5000C1,"">=""&R1C1,Sheet1!R2C1:R5000C1,""<=""&R2C1,Sheet1!R2C:R5000C,RC1)"
    ActiveCell.Copy
    Application.CutCopyMode = False
    Selection.Copy
    Range("E3:W3").Select
    ActiveSheet.Paste
    Range("D3:W3").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("D4:D8").Select
    ActiveSheet.Paste
End Sub

This assumes: in that sheet (not Sheet1) A1 contains your start date, A2 your end date and A3:A8 your six possible responses. Based on Record Macro, I trust you can adjust as required (if nearly there!) or come back with further details for help.

6
  • OK, I guess I wasn't very clear. To your first question, the reason why it goes to W is because there are a couple other items associated with each row that are not necessarily relevant for this analysis. The columns with relevant data responses are columns D through W. There is only one date entered into each row, and that is in column A (you can ignore/skip columns B & C). For each column (in this case, a question on a survey), there are 6 defined possible responses that could be entered.
    – Ryan
    Sep 3, 2013 at 20:27
  • So, for example, D3 could say "Strongly agree", "Somewhat agree", "Somewhat disagree", "Strongly disagree", "No response", or "Not sure/not applicable". This is the case for all of the questions associated with each column.
    – Ryan
    Sep 3, 2013 at 20:27
  • Therefore, I want to be able to pull up a row that has a date that falls within two designated dates (beginning date and end date of range), and then looks across the row to columns D through W and counts the number of the 6 possible responses (described above) for each column ( or "question"). I then want the value of the counts for each possible response within each column to be copied to a specific cell in another worksheet (in this case, Sheet3).
    – Ryan
    Sep 3, 2013 at 20:27
  • Well, I need it to first sort through the dates, and then run the CountIfs for all 6 possible responses for all 16 questions. Maybe I'm misreading it, but it seems like your macros suggestion doesn't first sort by date and then runs the countifs... Could you please clarify?
    – Ryan
    Sep 3, 2013 at 20:38
  • OK. I'm not really sure how to do that. Yes, I meant to say that it needs to filter through column A responses that fall within the specified date range, and then run the countifs for each of the possible responses for each of the 16 columns in the rows that met the date criteria for column A. Does that make sense? Also, how do you post beyond the comments? Thank you so much for your help!
    – Ryan
    Sep 3, 2013 at 20:49
0

Something like this might work - where your OK button on your userform is CommandButton1 and your to and from textboxes are ToDateBox and FromDateBox. The bwlow would go in your userform code module.

Private Sub CommandButton1_Click()

Dim CountOpt1, CountOpt2, CountOpt3, CountOpt4, CountOpt5, CountOpt6 As Long

Dim DateArray() As Variant
Dim DateRange As Range

Dim DateStart, DateEnd As String

Dim DateCount, ColCount As Double

' Sort your data in date order so the range you're searching for is contiguous

    Sheets("Sheet1").UsedRange.Columns.Sort key1:=Range("A2"), Header1:=xlYes, order1:=xlAscending

' Set the formatting on the output from your userform
' This works for me, but you'll have to figure out how your dates are formatted so the macro can find them...
    DateStart = Format(FromDateBox.Value, ddmmyyyy)
    DateEnd = Format(ToDateBox.Value, ddmmyyyy)

' Set the width of your range to the ColCount variable
    ColCount = Sheets("Sheet1").UsedRange.Columns.Count

' Set the height of your range to the DateCount variable depending on instances of dates between your input range
    DateCount = Application.WorksheetFunction.CountIfs(Range("Sheet1!A:A"), _
    ">=" & DateStart, Range("Sheet1!A:A"), "<=" & DateEnd)


' Find earliest occurence of start date and set it to the DateRange variable
    Do While DateRange Is Nothing
        With Sheets("Sheet1").Range("A:A")
            Set DateRange = .Find(What:=DateStart, _
            After:=.Cells(.Cells.Count), _
            LookIn:=xlValues, _
            LookAt:=xlWhole, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=False)
                If DateRange Is Nothing Then
                    DateStart = DateAdd("d", 1, DateStart)
                    DateStart = Format(DateStart, ddmmyyyy)
                End If
        End With
    Loop

' Expand the DateRange variable to exclude the dates (and columns B & C with unrelated data)
' and then encompass all the rows within your target date range
    Set DateRange = DateRange.Offset(0, 3)
    Set DateRange = DateRange.Resize(DateCount, ColCount - 3)

' Add the DateRange to a 2D array
    DateArray = DateRange

' Loop through the array counting instances of your answers
For Each c In DateArray
    If c = "Strongly Agree" Then CountOpt1 = CountOpt1 + 1
    If c = "Somewhat Agree" Then CountOpt2 = CountOpt2 + 1
    If c = "Somewhat Disagree" Then CountOpt3 = CountOpt3 + 1
    If c = "Strongly Disagree" Then CountOpt4 = CountOpt4 + 1
    If c = "No Response" Then CountOpt5 = CountOpt5 + 1
    If c = "Not Sure/Not Applicable" Then CountOpt6 = CountOpt6 + 1
Next c

' Activate the sheet you want to dump the counts
Sheets("Sheet3").Activate

' Put the counts wherever you want - for example
Range("A1") = "Option 1"
Range("B1") = CountOpt1
Range("A2") = "Option 2"
Range("B2") = CountOpt2
Range("A3") = "Option 3"
Range("B3") = CountOpt3
Range("A4") = "Option 4"
Range("B4") = CountOpt4
Range("A5") = "Option 5"
Range("B5") = CountOpt5
Range("A6") = "Option 6"
Range("B6") = CountOpt6

' Unload the userform if you want
Unload Me

End Sub

Give it a try.. Working with dates can be a bit funny though, you need to make sure the dates being brought in from your userform and assigned to the variables are in the same format as the dates in the range you're searching or it can give some really wily results...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.