+ Reply to Thread
Results 1 to 9 of 9

Mid and Mod to extract a number from string

Hybrid View

  1. #1
    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.

  2. #2
    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.

  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

    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.

+ 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