+ Reply to Thread
Results 1 to 3 of 3

help with if then select case statements

  1. #1
    Registered User
    Join Date
    06-24-2011
    Location
    Bradenton, FL
    MS-Off Ver
    Excel 2007
    Posts
    1

    help with if then select case statements

    I am new to VBA and cannot get this to work. I have an Office 2007 workbook which will start out with a worksheets in it, named "ChGridR1" then add a worksheet named "ChGridR2". I am trying to get the macro to detect the name and then do a find and replace with information from a third sheet "CHQ". I have tried If, Then, Else and Select Case but I cannot get either to work. I would be grateful if someone would tell me why. Thank you in advance for any help you can give.




    Sub rhgridreplace()
    '
    ' rhgridreplace Macro

    Select Case Grid

    Case Is = ("CHGridR1")

    Sheets("CHGridR1").Select
    Columns("J:J").Select
    Selection.Replace What:="#REF!", Replacement:="CHQ!B12", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Columns("L:L").Select
    Selection.Replace What:="#REF!", Replacement:="CHQ!D12", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Columns("M:M").Select
    Selection.Replace What:="#REF!", Replacement:="CHQ!F12", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Columns("C:C").Select
    Selection.Replace What:="#REF!", Replacement:="CHQ!B12", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Columns("E:E").Select
    Selection.Replace What:="#REF!", Replacement:="CHQ!D12", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Columns("F:F").Select
    Selection.Replace What:="#REF!", Replacement:="CHQ!F12", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False

    Case Is = ("CHGridR2")

    Sheets("CHGridR2").Select
    Columns("J:J").Select
    Selection.Replace What:="#REF!", Replacement:="CHQ!B12", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Columns("L:L").Select
    Selection.Replace What:="#REF!", Replacement:="CHQ!D12", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Columns("M:M").Select
    Selection.Replace What:="#REF!", Replacement:="CHQ!F12", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Columns("C:C").Select
    Selection.Replace What:="#REF!", Replacement:="CHQ!B12", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Columns("E:E").Select
    Selection.Replace What:="#REF!", Replacement:="CHQ!D12", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Columns("F:F").Select
    Selection.Replace What:="#REF!", Replacement:="CHQ!F12", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False

    End Select


    End Sub

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    38,190

    Re: help with if then select case statements

    Hi timermel, welcome to the forum.

    Please take a few moments to read the forum rules and use CODE tags around your code.

    It would also help to post a sample workbook so the code can be checked and tested in the context of typical data and workbook structure.

    Regards
    Trevor Shuttleworth - Excel Aid

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,202

    Re: help with if then select case statements

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution. Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.
    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    PLEASE PM WHEN YOU HAVE DONE THIS AND I WILL DELETE THIS POST
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

+ 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