Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8335845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:47:54+00:00 2026-06-09T03:47:54+00:00

How can I create a case statement (or multiple if statements) within a loop

  • 0

How can I create a case statement (or multiple if statements) within a loop in VBA?

In this example, I have a list of 4 names that correspond to 4 different groups. The names are in column ‘C’ of a spreadsheet and I would like to create a new column, column ‘D’, where each individual’s group name is listed.

Here is the code I am working with at the moment:

Sub AddGroupColumn()
   'Counts number of rows in sheet. Loops through rows.
   For i = 1 To Range("C1048576").End(xlUp).Row
       If Range("C2:C" & i).Value = "john.doe" Then
          Set Range("D2:D" & i).Value = "group 1"
       If Range("C2:C" & i).Value = "jane.doe" Then
          Range("D2:D" & i).Value = "group 2"
       If Range("C2:C" & i).Value = "james.doe" Then
          Range("D2:D" & i).Value = "group 3"
       If Range("C2:C" & i).Value = "jenn.doe" Then
          Range("D2:D" & i).Value = "group 4"
    Next i 
 End Sub

Please provide suggestion on how I can fix the code above. I know the syntax is off and I’m not sure if I should use a ‘Case’ statement of ‘If/Then/Else/Elseif’. Here is the error that I am currently receiving:

Compile Error: Next without For

Also, please advise if there is a more efficient way to solve this problem. In the real case, there are 12 names, 12 groups and 100,000 rows of names.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:47:56+00:00Added an answer on June 9, 2026 at 3:47 am

    You might put the names and groups into arrays.

    Sub AddGroupColumn()
    
        Dim rCell As Range
        Dim rRng As Range
        Dim vaNames As Variant
        Dim vaGroups As Variant
        Dim lMatch As Long
    
        vaNames = Array("john.doe", "jane.doe", "james.doe", "jenn.doe")
        vaGroups = Array("group 1", "group 2", "group 3", "group 4")
    
        With Sheet1
            Set rRng = .Range("C1", .Cells(.Rows.Count, 3).End(xlUp))
        End With
    
        For Each rCell In rRng.Cells
            lMatch = Application.WorksheetFunction.Match(rCell.Value, vaNames, False)
            rCell.Offset(0, 1).Value = vaGroups(lMatch - 1)
        Next rCell
    
    End Sub
    

    Note that in your example, you don’t specify which sheet the ranges apply to. These are called unqualified range references and can cause some unexpected behavior. If your code is in a standard module, unqualified ranges refer to the ActiveSheet. If your code is in a sheet class module, unqualified ranges refer to that sheet.

    If some of the names in column C don’t have a group, you have to change the loop to account for that. Like this

    For Each rCell In rRng.Cells
        On Error Resume Next
            lMatch = Application.WorksheetFunction.Match(rCell.Value, vaNames, False)
        On Error GoTo 0
    
        If lMatch - 1 > LBound(vaGroups) Then
            rCell.Offset(0, 1).Value = vaGroups(lMatch - 1)
        Else
            rCell.Offset(0, 1).Value = "No group"
        End If
    Next rCell
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried this in multiple ways: if-then-else, guards, case statements but I never
This week I took a SQL Statement with 11 case statements with sub query
I am trying to create a set using MDX that contains a CASE statement.
I have a query with SELECT CASE statement that's working fine: SELECT (CASE `t`.`is_combined`
I want get the time used for a case so I can create an
How can I create two different lists for one model? In my case I
Simple case of trying to prove I can convert NSString to NSDate . Create
I can create a contact that is not mail enabled, but how do I
One can create an anonymous object that is initialized through constructor parameters, such as
I have a LINQ query that searches for multiple keywords on multiple columns. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.