+ Reply to Thread
Results 1 to 6 of 6

Loop Through all Worksheets, Change cell Value based on another conditional cell

Hybrid View

Spswytch Loop Through all Worksheets,... 03-12-2015, 10:52 PM
MarvinP Re: Loop Through all... 03-12-2015, 10:55 PM
Spswytch Re: Loop Through all... 03-13-2015, 02:48 AM
Spswytch Re: Loop Through all... 03-13-2015, 02:45 AM
MarvinP Re: Loop Through all... 03-13-2015, 10:31 AM
Spswytch Re: Loop Through all... 03-13-2015, 12:01 PM
  1. #1
    Registered User
    Join Date
    03-12-2015
    Location
    Pitssburgh
    MS-Off Ver
    2013
    Posts
    4

    Loop Through all Worksheets, Change cell Value based on another conditional cell

    I know its something dumb im missing.

    All I need to do is scan a large range in column "A" I.E. A1:A524, If the Cell in Column A matches a Number in the Case Statement is Fills in the Adjacent Cell in Column "B" with the Text from the case statement. There are 40+ Worksheets in the Workbook. I can get the Following to work for the 1st Worksheet just fine, I put a small error trap in the bottom and it does indeed flip through the worksheets one at a time as evidenced by the Pop up windows showing each worksheet as it flips through them, but only ever changes the 1st worksheet. What am I Missing?



    Sub ProcessAllWorksheets()
    Dim wk As Worksheet
    Dim Target As Range
    For Each wk In ActiveWorkbook.Worksheets

    For Each Target In Range("A1:A5")
    Select Case Target.Value
    Case "21"
    Me.Range("B" & Target.Row).Value = "FTP Service Port"
    Case "22"
    Me.Range("B" & Target.Row).Value = "SSH Management Port"
    Case "123"
    Me.Range("B" & Target.Row).Value = "Network Time Protocol"
    Case "2207"
    Me.Range("B" & Target.Row) = "Python"
    End Select
    Next Target
    MsgBox wk.Name
    Next wk
    End Sub

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,378

    Re: Loop Through all Worksheets, Change cell Value based on another conditional cell

    Hi Spswytch and welcome to the forum,

    Code works on the active sheet. You simply need to make the next sheet active with a Wk.Select statement just below your first For line.

    If that doesn't do it for you, keep asking.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    03-12-2015
    Location
    Pitssburgh
    MS-Off Ver
    2013
    Posts
    4

    Re: Loop Through all Worksheets, Change cell Value based on another conditional cell

    Nevermind I got it.

    It was the Me.Range statements, should be wk.range. changed it works fine

  4. #4
    Registered User
    Join Date
    03-12-2015
    Location
    Pitssburgh
    MS-Off Ver
    2013
    Posts
    4

    Re: Loop Through all Worksheets, Change cell Value based on another conditional cell

    Thanks for your reply, Still no go. Although with your line added it does activate each sheet, but doesnt run through the second for loop nor change cell values in sheet 2 or 3

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,378

    Re: Loop Through all Worksheets, Change cell Value based on another conditional cell

    Hi Spswytch,

    You had your numbers in quotes. That was one problem. Another might have been you were only checking the first 5 rows. See if this works for you.

    Sub ProcessAllWorksheets()
    Dim wk As Worksheet
    Dim Target As Range
        For Each wk In ActiveWorkbook.Worksheets
        wk.Select
        For Each Target In Range("A1:A524")
            Select Case Target.Value
                Case 21
                    Range("B" & Target.Row).Value = "FTP Service Port"
                Case 22
                    Range("B" & Target.Row).Value = "SSH Management Port"
                Case 123
                    Range("B" & Target.Row).Value = "Network Time Protocol"
                Case 2207
                    Range("B" & Target.Row) = "Python"
            End Select
        Next Target
            'MsgBox wk.Name
        Next wk
    End Sub

  6. #6
    Registered User
    Join Date
    03-12-2015
    Location
    Pitssburgh
    MS-Off Ver
    2013
    Posts
    4

    Re: Loop Through all Worksheets, Change cell Value based on another conditional cell

    I had only given the 1st 5 Case statements as an example, im not going to list out the 1500 ports in this post that seemed pointless. As for the Numbers in Quotes, The Column is Type General not Type Number safer to use the quotes. It works I just had to change where it was looking for the Range in the worksheets. The Following worked for what i needed it to.

    Sub ProcessAllWorksheets()
    Dim wk As Worksheet
    Dim Target As Range
    For Each wk In ActiveWorkbook.Worksheets

    For Each Target In Range("A1:A5")

    Select Case Target.Value
    Case "21"
    Me.Range("B" & Target.Row).Value = "FTP Service Port"
    Case "22"
    wk.Range("B" & Target.Row).Value = "SSH Management Port"
    Case "123"
    wk.Range("B" & Target.Row).Value = "Network Time Protocol"
    Case "2207"
    wk.Range("B" & Target.Row) = "Python"
    End Select
    Next Target
    MsgBox wk.Name
    Next wk
    End Sub

    Thank you all for your advice. It put me on the right path greatly appreciated.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 8
    Last Post: 05-07-2014, 04:57 AM
  2. [SOLVED] Open & loop through worksheets based on filename cell value
    By ryanpetersen in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-05-2014, 03:16 PM
  3. Replies: 5
    Last Post: 12-07-2013, 02:21 PM
  4. Replies: 3
    Last Post: 11-08-2013, 03:10 PM
  5. Replies: 1
    Last Post: 10-09-2013, 05:05 PM

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