Results 1 to 5 of 5

VBA code to sum a range based on criteria in two separate columns

Threaded View

  1. #1
    Registered User
    Join Date
    09-11-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2002, 2003,2007
    Posts
    39

    VBA code to sum a range based on criteria in two separate columns

    My apologies for the vague Title in my original post.

    I've have the following code:

    Function IsOdd(x As Integer) As Boolean
        IsOdd = (x Mod 2) <> 0
    End Function
    Sub ImportTravelTime()
    
        Dim xlBook As Workbook
        Dim xlSheet As Worksheet
        Dim Continue As Boolean
        Dim OutSh As Worksheet
        Dim i As Integer
        Dim j As Integer
        Dim From As Integer
        Dim To1 As Integer
    
        Set OutSh = ThisWorkbook.Sheets("Travel Time")
        Range("C10:G14,C21:G25,F9,F20").Select
        Application.CutCopyMode = False
        Selection.ClearContents
        Range("B6:B15,B17:B26,C6:J8,C17:J19").Select
        Selection.ClearContents
        With Selection
            .HorizontalAlignment = xlGeneral
            .VerticalAlignment = xlCenter
            .WrapText = True
            .Orientation = 90
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        
        Continue = False
        For x = 1 To Excel.Workbooks.Count
          If LCase(Excel.Workbooks(x).Name) = "tt.xls" Then
            Continue = True
            Set xlBook = Excel.Workbooks(x)
          End If
        Next x
        
        If Continue Then
          xlBook.Activate
          Sheets("Travel Time Results").Activate
          Range("A2:C3,A58:C59,E2:F2,J2:L4").Select
          Selection.UnMerge
    
          OutSh.Range("B6").Value = Range("A2").Value
          OutSh.Range("B17").Value = Range("A58").Value
          OutSh.Range("C6").Value = Range("J2").Value
          OutSh.Range("C17").Value = Range("J2").Value
          OutSh.Range("F9").Value = Range("E2").Value
          OutSh.Range("F20").Value = Range("E2").Value
          
          OutSh.Activate
          Range("B6:B15,B17:B26").Select
          Application.CutCopyMode = False
          With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .WrapText = True
            .Orientation = 90
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
          End With
          Range("C6:J8,C17:J19").Select
          With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .WrapText = True
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
          End With
          xlBook.Activate
          Sheets("Travel Time Results").Activate
          Range("A2:C3,A58:C59,E2:F2,J2:L4").Select
          Selection.Merge
          
          Sheets("Travel Time Input").Activate
          For i = 1 To 10
              Set findit = Range("A:A").Find(what:=i)
              If Not findit Is Nothing Then
              firstadd = findit.Address
                If IsOdd(i) Then
                    outrow = OutSh.Cells(Rows.Count, 1).End(xlUp).Offset(Application.WorksheetFunction.RoundUp(i / 2, 0) + 8, 0).Row
                    OutSh.Cells(outrow, 3).Value = findit.Offset(0, 2).Value
                Else
                    outrow = OutSh.Cells(Rows.Count, 1).End(xlUp).Offset(Application.WorksheetFunction.RoundUp(i / 2, 0) + 8, 0).Row
                    OutSh.Cells(outrow, 4).Value = findit.Offset(0, 3).Value
                End If
              End If
          Next i
          
          Sheets("Travel Time Input").Activate
          For j = 1 To 10
              Set findit = Range("G:G").Find(what:=j)
              If Not findit Is Nothing Then
              firstadd = findit.Address
                If IsOdd(j) Then
                    outrow = OutSh.Cells(Rows.Count, 1).End(xlUp).Offset(Application.WorksheetFunction.RoundUp(j / 2, 0) + 19, 0).Row
                    OutSh.Cells(outrow, 3).Value = findit.Offset(0, 2).Value
                Else
                    outrow = OutSh.Cells(Rows.Count, 1).End(xlUp).Offset(Application.WorksheetFunction.RoundUp(j / 2, 0) + 19, 0).Row
                    OutSh.Cells(outrow, 4).Value = findit.Offset(0, 3).Value
                End If
              End If
          Next j
          
    Stop 'This is where I'm having troubles figuring out the sum range
                
          Set From = OutSh.Range("C10")
          Set To1 = OutSh.Range("D10")
          Sheets("Travel Time Results").Activate
          
    
    
        Else
            MsgBox "Travel Time.xls is not Open"
        End If
      
    End Sub
    Here is what I'm trying to accomplish after the Stop in the above code (BTW, I run the code from VPP.xls, worksheet Travel Time).

    Based on the Street name in VPP.xls, worksheet Travel Time, I want to find the distance between the streets from TT.xls worksheet, Travel Time Results, and report that in column E (the table is filled out how it should look after the code is run).

    For example, I want the distance between Street A and Street F WB. It should return a value of 5.2 (5238.9/1000).

    I've been using the following excel formula in column E in VPP.xls, but I want the code to actually just report the value in the cell. I can't figure out how to use the application.worksheet function, which I may not be able to do because of the : contained within the formula.

    =ROUND(SUM(INDEX('[TT.xls]Travel Time Results'!$D$6:$D$55,MATCH($C10,'[TT.xls]Travel Time Results'!$B$6:$B$55,0)):INDEX('[TT.xls]Travel Time Results'!$D$6:$D$55,MATCH($D10,'[TT.xls]Travel Time Results'!$C$6:$C$55,0)))/1000,1)
    Similar to finding the distance, I want to find the A and B times between those same streets and put that in the appropriate A and B columns. I imagine that would be similar code to the above, so if I can just figure that part out, I can get the rest.

    Thanks

    J

    Again, I apologize for my vagueness originally.
    Attached Files Attached Files
    Last edited by new.vbacoder; 11-17-2010 at 01:38 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1