+ Reply to Thread
Results 1 to 9 of 9

Mid and Mod to extract a number from string

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-27-2013
    Location
    Columbus Ohio
    MS-Off Ver
    Excel 2010
    Posts
    137

    Mid and Mod to extract a number from string

    Hello, I am trying to grab a number from a string to determine if it is even or odd. I am using the following function
      Dim i As Integer
        For i = 2 To 944
            If Mid("H" & i, 8, 8) = "8" And Mid("H" & i, 8, 9) Mod 2 <> 0 Then
                Range("A" & i & ":J" & i).Copy Destination:=Sheets(test)
        End If
    However when I run it I get the error message type mismatch. I am assuming this is due to the fact I am running a math function on a string? Any suggestions or change I can use to get this to work right? I saw one example that used the .NumberFormat but that threw errors as well. What is the best way to parse a string character into type int?

  2. #2
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Mid and Mod to extract a number from string

    Hi,

    It would help if you attached your workbook example

    In any case change the "8" to the number 8 only
      Dim i As Integer
        For i = 2 To 944
            If Mid("H" & i, 8, 8) = 8 And Mid("H" & i, 8, 9) Mod 2 <> 0 Then
                Range("A" & i & ":J" & i).Copy Destination:=Sheets(test)
        End If

  3. #3
    Forum Contributor
    Join Date
    10-27-2013
    Location
    Columbus Ohio
    MS-Off Ver
    Excel 2010
    Posts
    137

    Re: Mid and Mod to extract a number from string

    I have included the work book. What I am pulling from is a string so I thought that it had to be included in quotes.
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Mid and Mod to extract a number from string

    Also, be sure to refer to a range value within the mid function. Without testing the code and seeing the workbook example, at the moment, I don't fully understand it

  5. #5
    Forum Contributor
    Join Date
    10-27-2013
    Location
    Columbus Ohio
    MS-Off Ver
    Excel 2010
    Posts
    137

    Re: Mid and Mod to extract a number from string

    I just tried to specify the range in Mid and still getting the same error.

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Mid and Mod to extract a number from string

    You are looping through the strings "H2", "H3", "H4", ...
    Also, Mid(anyString, 8, 8) will never ="8" (unless anyString = "8') since the Mid is returning 8 characters.
    Also, the destination argument of Copy should be a range, not a sheet.

    If you want to loop through the cells H1, H2, etc

    Dim i As Integer
    
    For i = 2 To 944
        With Range("A" & i)
            If (Mid(CStr(.Cells(1, 8).Value)), 8, 1) = "8") And ((Val(Mid(CStr(.Cells(1, 8).Value)) & i, 8, 9)) Mod 2) <> 0) Then
                .Resize(1, 11).Copy Destination:=Sheets(test).Range(.Address)
            End If
        End With
    Next i
    Last edited by mikerickson; 11-22-2013 at 01:34 PM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  7. #7
    Forum Contributor
    Join Date
    10-27-2013
    Location
    Columbus Ohio
    MS-Off Ver
    Excel 2010
    Posts
    137

    Re: Mid and Mod to extract a number from string

    I misunderstood Mid. I have changed it to return only 1 character.

  8. #8
    Forum Contributor
    Join Date
    10-27-2013
    Location
    Columbus Ohio
    MS-Off Ver
    Excel 2010
    Posts
    137

    Re: Mid and Mod to extract a number from string

    Quote Originally Posted by mikerickson View Post
    You are looping through the strings "H2", "H3", "H4", ...
    Also, Mid(anyString, 8, 8) will never ="8" (unless anyString = "8') since the Mid is returning 8 characters.
    Also, the destination argument of Copy should be a range, not a sheet.

    If you want to loop through the cells H1, H2, etc

    Dim i As Integer
    
    For i = 2 To 944
        With Range("A" & i)
            If (Mid(CStr(.Cells(1, 8).Value)), 8, 1) = "8") And ((Val(Mid(CStr(.Cells(1, 8).Value)) & i, 8, 9)) Mod 2) <> 0) Then
                .Resize(1, 11).Copy Destination:=Sheets(test).Range(.Address)
            End If
        End With
    Next i
    That is throwing a Syntax error.

  9. #9
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Mid and Mod to extract a number from string

    Try this parethesis arrangement
    If (Mid(CStr(.Cells(1, 8).Value), 8, 1) = "8") And (Val(Mid(CStr(.Cells(1, 8).Value), 8, 9)) Mod 2) <> 0) Then

+ 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] How to Extract A Number from String if Text within the String Equals XYZ
    By tyrsfury in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 11-15-2012, 03:30 PM
  2. [SOLVED] Extract a mainly numeric string (ISSN number) from an arbitrary string
    By Buzzed Aldrin in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 07-04-2012, 01:49 PM
  3. Extract Number from String Help
    By kobe4ever8 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-31-2011, 02:25 PM
  4. Extract number out of a string
    By mkvassh in forum Excel General
    Replies: 4
    Last Post: 04-07-2010, 05:25 PM
  5. Extract Only Number From A String
    By lehainam in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-26-2006, 06:45 AM

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