+ Reply to Thread
Results 1 to 9 of 9

Search cell

Hybrid View

  1. #1
    Registered User
    Join Date
    05-20-2010
    Location
    Green Bay
    MS-Off Ver
    Excel 2007
    Posts
    20

    Search cell

    I have a macro that I am trying to get something done. I want it to search a range of cells from E2:P13. If it contains a "1" then copy cell B27 to that cell and repeat for all cells in that range. If it contains a "2" then copy cell C27 to that cell.

    I had a VB class in college and wrote this, but I would have to copy and paste it for each cell and if I add a row it would not be easy to change.

    If Range("E2").Value = "1" Then
            Range("B27").Select
            Selection.Copy
            Range("E2").Select
            ActiveSheet.Paste
            Application.CutCopyMode = False
        ElseIf Range("E2").Value = "2" Then
            Range("C27").Select
            Selection.Copy
            Range("E2").Select
            ActiveSheet.Paste
            Application.CutCopyMode = False
        End If
    Can anyone help with something more "dynamic"?

    Thanks!
    Last edited by esullivan; 05-20-2010 at 11:23 AM.

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Macro Help

    You can loop through cells using a Range object like this:

    Dim rngLoopRange As Range
    
    For Each rngLoopRange In Range("E2:P13")
    
        Select Case rngLoopRange.Value
        
            Case 1
                    
                rngLoopRange = Range("B27")
                    
            Case 2
                
                rngLoopRange = Range("C27")
                    
        End Select
            
    Next rngLoopRange

    Hope it helps,

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Registered User
    Join Date
    05-20-2010
    Location
    Green Bay
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Search cell macro help

    So how would I tell it do paste in the cell it's found a match for? I am sorry, I know BASIC VB, but this is kind of over my head.

    Thanks

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Search cell

    [E2:P13].replace 1, [B27],,xlvalues,xlwhole
    [E2:P13].replace 2, [C27],,xlvalues,xlwhole

  5. #5
    Registered User
    Join Date
    05-20-2010
    Location
    Green Bay
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Search cell

    Like this?

    Dim rngLoopRange As Range
    For Each rngLoopRange In Range("E2:P13")
        Select Case rngLoopRange.Value
            Case 1
                rngLoopRange = Range("B27")
    	    [E2:P13].replace 1, [B27],,xlvalues,xlwhole
            Case 2      
                rngLoopRange = Range("C27")
    	    [E2:P13].replace 2, [C27],,xlvalues,xlwhole     
        End Select
    Next rngLoopRange

  6. #6
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Search cell macro help

    Quote Originally Posted by esullivan View Post
    So how would I tell it do paste in the cell it's found a match for? I am sorry, I know BASIC VB, but this is kind of over my head.

    Thanks
    Did you try the code I posted? It should do everything you described in your original post.

    Dom

  7. #7
    Registered User
    Join Date
    05-20-2010
    Location
    Green Bay
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Search cell

    Oh wow, yeah it does thanks. Except it on;y copies the value not the formatting. I have fill colors and text colors that need to be changed.

    Thanks again for all your help by the way!
    Last edited by esullivan; 05-20-2010 at 05:40 PM.

  8. #8
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Search cell

    Try this:

    Dim rngLoopRange As Range
    
    Application.ScreenUpdating = False
    
    For Each rngLoopRange In Range("E2:P13")
    
        Select Case rngLoopRange.Value
        
            Case 1
                    
                Range("B27").Copy rngLoopRange
                    
            Case 2
                
                Range("C27").Copy rngLoopRange
                    
        End Select
        
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    
    Next rngLoopRange

    Dom

  9. #9
    Registered User
    Join Date
    05-20-2010
    Location
    Green Bay
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Search cell

    That worked great! Thanks man!!!!

+ 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