+ Reply to Thread
Results 1 to 16 of 16

extract a row within a cell based on keyword

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    extract a row within a cell based on keyword

    Hi all,

    i am wondering if there is any way to extract a row of data within a cell?currently, in the "atm withdrawl" sheet, i have created a macro to copy cells in other sheets ( jan to dec) with the word "atm" into the sheet. However, i would like the program to extract the row with the word "atm" instead of the whole cell. is there any way to do so?Thanks.

    Regards,
    Dan
    Attached Files Attached Files
    Last edited by dan2010; 12-15-2010 at 02:32 AM.

  2. #2
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: extract a row within a cell based on keyword

    Try this modified version of your code
    Sub atm()
    
        Range("a6:l300").ClearContents
        m = 6
        For n = 1 To 12    ' months
            For a = 6 To 36    ' range of each sheet
                '  For j = 2 To 5 ' time slot
                If Sheets(n).Cells(a, 2).Value Like "*atm*" Then
                    Sheets(14).Cells(m, n).Value = "Date: " & Sheets(n).Cells(a, 1) & Chr(10) & Mid(Sheets(n).Cells(a, 2), InStr(Sheets(n).Cells(a, 2), "atm"))
                    m = m + 1
                End If
            Next
        Next
    
    End Sub

    Hope this helps
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,493

    Re: extract a row within a cell based on keyword

    This will extract the atm part of the cell assuming it will be the last entry of the day
    Change activecell to the cell you are using in your loop
    Str is the part you will be sending to the other sheet

        Dim CntSt As Integer, MyPos, Str As String
    
        CntSt = Len(ActiveCell)
        MyPos = InStr(1, ActiveCell, "atm")
        If MyPos > 0 Then
            Str = Mid(ActiveCell, MyPos, CntSt)
            MsgBox Str
        End If

  4. #4
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: extract a row within a cell based on keyword

    As an extra to your question you might want to use this instead of the rather lengthy code you are using at present
    Sub Next_year()
        Dim DaysInMonth As Integer, wsNo As Integer
        Dim YearToSet As String, ChkFileName
    
        ThisWorkbook.Save
        ActiveWorkbook.Application.ScreenUpdating = False
    
        YearToSet = InputBox("Enter the year to use.", "New File", Year(Now()))
    
        For wsNo = 1 To Sheets.Count
            On Error Resume Next
            ChkFileName = CDate("1/" & Sheets(wsNo).Name & "/" & YearToSet)
            If Err.Number <> 0 Then
                Err.Clear
            Else
                With Sheets(wsNo)
                    If .Name = "December" Then
                        DaysInMonth = CDate("1/January/" & YearToSet + 1) - CDate("1/" & .Name & "/" & YearToSet)
                    Else
                        DaysInMonth = CDate("1/" & Month(CDate("1/" & .Name & "/" & YearToSet)) + 1 & "/" & YearToSet) _
                                      - CDate("1/" & .Name & "/" & YearToSet)
                    End If
                    .Range("a6:f36").ClearContents                      'converts cells from "a6 to f36" to empty cells
                    .Range("a6:f36").Interior.ColorIndex = xlNone       ' remove background colour
    
                    .Range("A6") = CDate("1/" & .Name & "/" & YearToSet)
                    .Range("A6:A" & DaysInMonth + 5).DataSeries Rowcol:=xlColumns, Type:=xlChronological, _
                                                                Date:=xlDay, Step:=1, Trend:=False
                    .Range("A6:A" & DaysInMonth + 5).NumberFormat = "dd (ddd)"
                End With
            End If
            On Error GoTo 0
        Next
    
        ActiveWorkbook.Application.ScreenUpdating = True
    
        ThisWorkbook.SaveAs (ThisWorkbook.Path & "\" & "My spendings " & Year(Date) + 1)    'current year + 1
    
    End Sub

    Both suggestions are in this attachment.

    I haven't tried Daves' code, just adapted yours.
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    Re: extract a row within a cell based on keyword

    sorry for forgetting to state that "atm" is not always at the last row. what if the term "atm" is in the middle of the cell? the coding now will copy everything after the "atm".

    marcol : i have tried to create the file for next year, however, only the 1st day of each month has been assigned, leaving the other rows blank.

    what is the function of this code?
    DaysInMonth = CDate("1/January/" & YearToSet + 1) - CDate("1/" & .Name & "/" & YearToSet)

  6. #6
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,493

    Re: extract a row within a cell based on keyword

    Why would you put all entries into one cell?
    You can't do anything with them, each entry needs to be in a separate cell.

    Merged cells are not a good thing to have either.

    Try this example of creating a new workbook for the new year.
    It uses a helper sheet to reduce the amount of code required and is less confusing.

    It will save as a new workbook and then delete the helper sheet.

    Something to work on anyway...
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    Re: extract a row within a cell based on keyword

    marcol : thank you. i have tried to create a new file. It shows the day 1 to day 31 for december, but shows only day 1 for Jan to Nov. I am currently using excel 2007. i have tried experimenting with the codes and found out that the problem lies with the code below.

    i have tried using msgbox to check the DaysInMonth. it shows -300+ to -30. i am currently trying to change the coding to reflect the true date. .

    DaysInMonth = CDate("1/" & Month(CDate("1/" & .Name & "/" & YearToSet)) + 1 & "/" & YearToSet) _
                                      - CDate("1/" & .Name & "/" & YearToSet)
    i will test your code and update u again

    davesexcel :
    i did not merge any cells. i just add a new line for each item.

    i have another program that uses the same format. However, there are 5 columns to fill in. In addition, there are about 7 items for each cell, so it is not possible to put each entry in separate cells ( that would be 35 columns worth of data :o )

    i am trying to kill 2 birds with one stone by trying to figure out how to extract a single row of data in a cell

  8. #8
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    Re: extract a row within a cell based on keyword

    DaysInMonth = CDate(Month(CDate("1/" & .Name & "/" & YearToSet)) + 1 & "/" &  "1/" & YearToSet) _
                                      - CDate("1/" & .Name & "/" & YearToSet)
    By putting the month in front(code on top) , the days and date works well for me. i am not sure why the format of the date is in "mm/dd/yy" even when the code below indicates that the format should be in "dd/mm/yr"

    DaysInMonth = CDate("1/" & Month(CDate("1/" & .Name & "/" & YearToSet)) + 1 & "/" & YearToSet) _
                                      - CDate("1/" & .Name & "/" & YearToSet)

  9. #9
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: extract a row within a cell based on keyword

    1/. This will handle "atm" in any row
    Sub atm()
        Dim strATM As String
        Dim a As Long, m As Long
        
        Range("a6:l300").ClearContents
        m = 6
        For n = 1 To 12    ' months
            For a = 6 To 36    ' range of each sheet
                If Sheets(n).Cells(a, 2).Value Like "*atm*" Then
                    strATM = Mid(Sheets(n).Cells(a, 2), InStr(Sheets(n).Cells(a, 2), "atm"))
                    If InStr(1, strATM, Chr(10)) > 0 Then
                        strATM = Left(strATM, InStr(1, strATM, Chr(10)) - 1)
                    End If
                    Sheets(14).Cells(m, n).Value = "Date: " & Sheets(n).Cells(a, 1) & Chr(10) & strATM
                    m = m + 1
                End If
            Next
        Next
    
    End Sub

    2/. Doesn't the workbook I posted work for you?
    It seems okay to me.
    I just tested the uploaded file in case I had posted the workbook without saving the final version, and it seems to work.

    3/. This code
    DaysInMonth = CDate("1/January/" & YearToSet + 1) - CDate("1/" & .Name & "/" & YearToSet)
    Is used to calculate the number of days in the month of December, as this never varies I could have just used
    DaysInMonth = 31
    I left it in to let you see why it is different from all the other months in this instance.
    DaysInMonth = CDate("1/" & Month(CDate("1/" & .Name & "/" & YearToSet)) + 1 & "/" & YearToSet) - CDate("1/" & .Name & "/" & YearToSet)
    CDate("1/" & .Name & "/" & YearToSet)
    ".Name" returns the sheet name (It is contained within the With Statement)
    "YearToSet" is returned from the Input box.
    When this is concatenated we get
    e.g.
    "1/January/2010" as a string
    Cdate("1/January/2010")
    returns a date



    CDate("1/" & Month(CDate("1/" & .Name & "/" & YearToSet)) + 1 & "/" & YearToSet)
    Month(CDate("1/" & .Name & "/" & YearToSet)) + 1
    This returns the next month based on the name of your worksheet.

    We then subtract the two to get the number of days in any month.

    Hope this helps
    Last edited by Marcol; 12-13-2010 at 09:06 AM.

  10. #10
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: extract a row within a cell based on keyword

    How about posting your current workbook?

    I can see no reason why you can only get the first day returned for all but December.

    How about a less greedy approach, let's go for one bird at a time.

  11. #11
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: extract a row within a cell based on keyword

    Surely you mean
    DaysInMonth = CDate(Month(CDate("1/" & .Name & "/" & YearToSet)) + 1 & "/" &  "1/" & YearToSet) _
                                      - CDate(.Name & "/" & "1/" & YearToSet)
    both parts of the formula should have the same format structure.

  12. #12
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    Re: extract a row within a cell based on keyword

    marcol:
    Both
    DaysInMonth = CDate(Month(CDate("1/" & .Name & "/" & YearToSet)) + 1 & "/" &  "1/" & YearToSet) _
                                      - CDate("1/" & .Name & "/" & YearToSet)
    AND
    DaysInMonth = CDate(Month(CDate("1/" & .Name & "/" & YearToSet)) + 1 & "/" &  "1/" & YearToSet) _
                                      - CDate(.Name & "/" & "1/" & YearToSet)
    work perfectly. i am not sure why but
    CDate(.Name & "/" & "1/" & YearToSet)
    and
    CDate("1/" & .Name & "/" &  YearToSet)
    gives the same format "mm/dd/yy"

    by the way, is it possible to add in an input box so that user can choose to search for different keywords?

    i have tried the code below but it is not working.
    keyword= inputbox("Enter keyword.","input box")
    For n = 1 To 12    ' months
            For a = 6 To 36    ' range of each sheet
                If Sheets(n).Cells(a, 2).Value Like *keyword* Then
                    strATM = Mid(Sheets(n).Cells(a, 2), InStr(Sheets(n).Cells(a, 2), keyword))
                    If InStr(1, strATM, Chr(10)) > 0 Then
                        strATM = Left(strATM, InStr(1, strATM, Chr(10)) - 1)
                    End If
                    Sheets(14).Cells(m, n).Value = "Date: " & Sheets(n).Cells(a, 1) & Chr(10) & strATM
                    m = m + 1
                End If
            Next
        Next
    davesexcel:
    i do not understand your program. is it supposed to have only "start" sheet at the start, manually key in the days for the start of each month. then click the button to generate the 12 sheets?

  13. #13
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: extract a row within a cell based on keyword

    1/. The dates issue must be due to your computers' Regional Settings, if it now works then all is well, the expanation I gave earlier still stands.

    2/. Try this
    Sub atm()
        Dim strItem As String
        Dim Keyword As String
        Dim a As Long, m As Long
        
        Keyword = InputBox("Enter String to Find (Lower Case)", "Extract String", "atm")
        
        Range("a6:l300").ClearContents
        m = 6
        For n = 1 To 12    ' months
            For a = 6 To 36    ' range of each sheet
                If Sheets(n).Cells(a, 2).Value Like "*" & Keyword & "*" Then
                    strItem = Mid(Sheets(n).Cells(a, 2), InStr(Sheets(n).Cells(a, 2), Keyword))
                    If InStr(1, strItem, Chr(10)) > 0 Then
                        strItem = Left(strItem, InStr(1, strItem, Chr(10)) - 1)
                    End If
                    Sheets(14).Cells(m, n).Value = "Date: " & Sheets(n).Cells(a, 1) & Chr(10) & strItem
                    m = m + 1
                End If
            Next
        Next
    
    End Sub

    I have changed the variable names to be more descriptive of the new problem, I suggest you also change the sub name, and assisn the renamed macro to the relevant button.

    Note this method is case sensitive, but that would not appear to be a problem as all your data is in lower case.

    Hope this helps.

  14. #14
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    Re: extract a row within a cell based on keyword

    Oh i see. so that is how to use the wild card. thanks for all the help. 1 more question, hopefully the last, what if the keyword has both upper and lower case? this is for the other program i am using.
    Last edited by dan2010; 12-14-2010 at 10:29 AM.

  15. #15
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: extract a row within a cell based on keyword

    Try this
    Sub atm()
        Dim strItem As String
        Dim Keyword As String
        Dim a As Long, m As Long
        
        Keyword = InputBox("Enter String to Find", "Extract String", "ATM")
        Keyword = LCase(Keyword)
        
        Range("a6:l300").ClearContents
        m = 6
        For n = 1 To 12    ' months
            For a = 6 To 36    ' range of each sheet
                If LCase(Sheets(n).Cells(a, 2).Value) Like "*" & Keyword & "*" Then
                    strItem = LCase(Sheets(n).Cells(a, 2).Value)
                    strItem = Mid(strItem, InStr(strItem, Keyword))
                    If InStr(1, strItem, Chr(10)) > 0 Then
                        strItem = Left(strItem, InStr(1, strItem, Chr(10)) - 1)
                    End If
                    Sheets(14).Cells(m, n).Value = "Date: " & Sheets(n).Cells(a, 1) & Chr(10) & strItem
                    m = m + 1
                End If
            Next
        Next
    
    End Sub

    This will return the result in lower case (for the search string)

    If you want the result in upper case then change this line
    Sheets(14).Cells(m, n).Value = "Date: " & Sheets(n).Cells(a, 1) & Chr(10) & strItem
    to
    Sheets(14).Cells(m, n).Value = "Date: " & Sheets(n).Cells(a, 1) & Chr(10) & Ucase(strItem)

    If you want proper case you will need to use WorksheetFunction.Proper()
    Sheets(14).Cells(m, n).Value = "Date: " & Sheets(n).Cells(a, 1) & Chr(10) & WorksheetFunction.Proper(strItem)

    Hope this helps

  16. #16
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    Thumbs up Re: extract a row within a cell based on keyword

    marcol:
    you have been an excellent help. thank you very much for your time and effort. i am glad to know that there are many helpful people in the forum like you who are always available to help us with our problem.

+ Reply to Thread

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