Results 1 to 6 of 6

Thread: Excel Macro Help

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2004
    Posts
    35

    Excel Macro Help

    Hello,

    I need some help with an excel macro. Have a .xls file with 10 columns, and over 7K rows. I need a macro to search a specific column (column 3) for the existance of the string "Solution :" If the cell contains the search string, I would like to cut the contents of the cell beginning at the "S" in "Solution" to the end of the cell and paste it in the same row, two columns over to the right. (Column 5)

    I am very new to excel and VBA and don't even know where to start. Any example code would be helpful. Thanks in advance.

    -John

  2. #2
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Excel Macro Help

    John:

    Break the task into several pieces and get familiar with each piece. Here is an example of searching for "Solution" in column 3. The "ActualUsedRange" function can be found in the following post: Need Help on selecting using vba on excel
    Code:
    Option Explicit
    Sub Search_String()
      Dim aStr As String
      Dim aRange As Range
      Dim mySheet as Worksheet
      Dim aCell As Range
      Dim aRow As Long
      Dim aCol As Long
      Dim i As Long
      
      'Initialize the search string
      aStr = "Solution"
      
      'What sheet are we working on?
      '(Edited!)
      Set mySheet = ActiveSheet  ' -or-  Sheets("mySheetName")
    
      aRow = 2  'Skips over headers in row 1
      aCol = 3  'Initialize the search column
      
      'Find the Real UsedRange for the data
      Set aRange = ActualUsedRange(mySheet)
      If aRange = Nothing Then
          MsgBox "This Sheet is EMPTY!" : Exit Sub
      End If
      For i = aRow To aRange.Rows.Count
          If Cells(aRow, aCol).Value = aStr Then
              ' Put code to process "Solution" Found HERE
              'TEST TEST TEST TEST
              MsgBox "FOUND: " & Cells(aRow, aCol).Address & Chr(10) & Cells(aRow, aCol).Value
              'END TEST
          Else
              'TEST TEST TEST TEST
              MsgBox "Not Found: " & Cells(aRow, aCol).Address & Chr(10) & Cells(aRow, aCol).Value
              'END TEST
              ' Put code to process NOT FOUND Here
          End If
          aRow = aRow + 1
      Next i
      
    End Sub
    Good Learning and Good Programming!
    Last edited by Webtest; Oct 7th, 2005 at 09:01 AM. Reason: CODE CORRECTION!
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Excel Macro Help

    This shoud work

    VB Code:
    1. Sub FindSolution()
    2. Dim MySheet As Worksheet
    3. Dim RowCount As Integer
    4. Dim SearchValue As String
    5. Dim MyRow As Integer
    6. Dim MyCell As Range
    7. Dim StartNum As Integer
    8.  
    9.     Set MySheet = Sheets("Sheet1") 'Set this variable to the worksheet that has your data
    10.     RowCount = MySheet.UsedRange.Rows.Count 'Find how many rows are used on the sheet
    11.     SearchValue = "Solution :" 'This is the value you are searching for
    12.    
    13.     For MyRow = MySheet.UsedRange.Row To MySheet.UsedRange.Row + RowCount - 1 'loop through each row in the used range
    14.         Set MyCell = MySheet.Cells(MyRow, 3) 'set this variable to the cell in the third colum
    15.         StartNum = InStr(1, MyCell, SearchValue, vbTextCompare) 'find the starting position of the search value in the searched cell
    16.         If StartNum > 0 Then ' if the starting position is not zero then the value is in the cell
    17.             MyCell.Offset(0, 2) = Mid(MyCell, StartNum) 'copy the text from the searched cell to the cell 2 columns to the right, starting with the search string
    18.         End If
    19.     Next MyRow
    20.    
    21.     Set MyCell = Nothing 'clear your object variables
    22.     Set MySheet = Nothing 'clear your object variables
    23. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2004
    Posts
    35

    Re: Excel Macro Help

    Thanks Art and Declan,

    Declan, your solution worked great. One quick question regarding Declan's solution, instead of copying the selected text, can it be cut, so it no longer appears in column 3.

  5. #5
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Excel Macro Help

    You can always test small things by recording a macro, doing the little tasks, stopping the macro, and then edit the macro to look at the code that was produced. Use this code as a template to insert into your actual code.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  6. #6
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Excel Macro Help

    I've added a line to replace the value in column 3.

    VB Code:
    1. Sub FindSolution()
    2. Dim MySheet As Worksheet
    3. Dim RowCount As Integer
    4. Dim SearchValue As String
    5. Dim MyRow As Integer
    6. Dim MyCell As Range
    7. Dim StartNum As Integer
    8.  
    9.     Set MySheet = Sheets("Sheet1") 'Set this variable to the worksheet that has your data
    10.     RowCount = MySheet.UsedRange.Rows.Count 'Find how many rows are used on the sheet
    11.     SearchValue = "Solution :" 'This is the value you are searching for
    12.    
    13.     For MyRow = MySheet.UsedRange.Row To MySheet.UsedRange.Row + RowCount - 1 'loop through each row in the used range
    14.         Set MyCell = MySheet.Cells(MyRow, 3) 'set this variable to the cell in the third colum
    15.         StartNum = InStr(1, MyCell, SearchValue, vbTextCompare) 'find the starting position of the search value in the searched cell
    16.         If StartNum > 0 Then ' if the starting position is not zero then the value is in the cell
    17.             MyCell.Offset(0, 2) = Mid(MyCell, StartNum) 'copy the text from the searched cell to the cell 2 columns to the right, starting with the search string
    18.             MyCell = Application.WorksheetFunction.Replace(MyCell, StartNum, Len(MyCell.Offset(0, 2)), "")
    19.         End If
    20.     Next MyRow
    21.    
    22.     Set MyCell = Nothing 'clear your object variables
    23.     Set MySheet = Nothing 'clear your object variables
    24. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

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



Click Here to Expand Forum to Full Width