Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 26th, 2009, 11:56 AM
Authorized User
 
Join Date: Aug 2006
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help with a loop

Hello:
I would like some help because I'm not that familiar with VBA. What I need to do is create a loop which checks R24, S24, T24,...,AF24 for a given value. If that value is found in that one cell (range), then do a bunch of other stuff.

This is my code, but I don't know how to make it do the loop so it's more effective. Any help would be very greatly appreciated.
Code:
If Range("t24") = "watch" Then
    Range("R25:X25").Select
    ActiveWindow.SmallScroll ToRight:=2
    Range("R25:AJ25").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("AA24:AF24").Select
    Selection.Copy
    Range("S25").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("R25:AJ25").Select
    Application.CutCopyMode = False
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("AH24:AM24").Select
    Selection.Copy
    Range("S25").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    End If
Best Regards,
Arholly
 
Old June 26th, 2009, 09:58 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Can you try something like this:

Code:
Sub Check_MultipleRanges()
Dim oRange As Range
Dim oCell As Range
Set oRange = Range("R24, S24, T24, AF24")
For Each oCell In oRange
    If LCase(oCell.Value) = "watch" Then
    
        ' Bunch of Stuff
        MsgBox "Found"
    
    End If
    
Next oCell
End Sub
Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop twice, then inside loop select nodes?? JohnBampton XSLT 2 March 9th, 2009 05:21 AM
Do Loop? emanuele.musa VB How-To 0 July 27th, 2007 05:50 AM
Do Loop junemo Beginning PHP 8 July 28th, 2004 02:58 AM
For Loop aadz5 C# 2 December 26th, 2003 07:38 AM
nested while loop doesn't loop hosefo81 PHP Databases 5 November 12th, 2003 08:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.