+ Reply to Thread
Results 1 to 4 of 4

If WorksheetFunction.Match does not return a value, then...

Hybrid View

  1. #1
    Registered User
    Join Date
    01-22-2019
    Location
    Nova Scotia, Canada
    MS-Off Ver
    2007
    Posts
    22

    If WorksheetFunction.Match does not return a value, then...

    Good evening,
    I'd like to modify the code below so that if the string "pieces" does not return a match, then I want the value for b to equal the row number of the last entry in column T.

    Dim a As Long, b As Long
    Dim rng As Range
    Dim ws As Worksheet
    Dim c As String
    
    Set rng = Range("B1", Cells(Rows.Count, "B").End(xlUp))
    
    a = WorksheetFunction.Match("Hours Paid:", rng, 0) + 1
    b = WorksheetFunction.Match("Pieces:", rng, 0) - 1
    c = WorksheetFunction.Sum(Range("T" & a & ":T" & b))
    
    Range("T" & b + 1) = c
    
    Sheets("Appendix D Calc").Activate
    Range("H7") = c
    M

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: If WorksheetFunction.Match does not return a value, then...

    Sub test()
    Dim a, b
    Dim rng As Range
    Dim ws As Worksheet
    Dim c As Long
    
    Set rng = Range("B1", Cells(Rows.Count, "B").End(xlUp))
    
    a = Application.Match("Hours Paid:", rng, 0)
    If Not IsError(a) Then a = a + 1
    
    b = Application.Match("Pieces:", rng, 0)
    If Not IsError(b) Then
        b = b - 1
    Else
        b = Range("T" & Rows.Count).End(xlUp).row
    End If
    
    c = Application.Sum(Range("T" & a & ":T" & b))
    
    Range("T" & b + 1) = c
    
    Sheets("Appendix D Calc").Activate
    Range("H7") = c
    End Sub
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Registered User
    Join Date
    01-22-2019
    Location
    Nova Scotia, Canada
    MS-Off Ver
    2007
    Posts
    22

    Re: If WorksheetFunction.Match does not return a value, then...

    Quote Originally Posted by bakerman2 View Post
    Sub test()
    Dim a, b
    Dim rng As Range
    Dim ws As Worksheet
    Dim c As Long
    
    Set rng = Range("B1", Cells(Rows.Count, "B").End(xlUp))
    
    a = Application.Match("Hours Paid:", rng, 0)
    If Not IsError(a) Then a = a + 1
    
    b = Application.Match("Pieces:", rng, 0)
    If Not IsError(b) Then
        b = b - 1
    Else
        b = Range("T" & Rows.Count).End(xlUp).row
    End If
    
    c = Application.Sum(Range("T" & a & ":T" & b))
    
    Range("T" & b + 1) = c
    
    Sheets("Appendix D Calc").Activate
    Range("H7") = c
    End Sub
    This works! Thanks Bakerman2!

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: If WorksheetFunction.Match does not return a value, then...

    Glad to help and thanks for rep+.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Return actual % value during Application.WorksheetFunction.Max
    By Sintek in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-23-2018, 03:22 AM
  2. [SOLVED] WorksheetFunction match - Userform
    By Villalobos in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-14-2016, 01:31 PM
  3. Application.WorksheetFunction.Match in VBA
    By karen53 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-24-2014, 06:32 PM
  4. [SOLVED] VBA worksheetfunction.match doesn't match DATE. Userform
    By stevnb in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-05-2012, 09:08 AM
  5. [SOLVED] Userform Match - unable to get the match property of the worksheetfunction class - Help!
    By Tayque_J_Holmes in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-10-2012, 11:01 AM
  6. Worksheetfunction.Match Issue
    By DAA in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-01-2009, 09:51 AM
  7. Application.WorksheetFunction.Match
    By TK in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-06-2005, 05:05 PM

Tags for this Thread

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