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

  • SEARCH
  • Home
  • 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 9260845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:59:52+00:00 2026-06-18T12:59:52+00:00

Im working on a little script (below). The script iterates through rows on a

  • 0

Im working on a little script (below). The script iterates through rows on a data set and places either a 1 or 0 in a cell based on some contingencies of 2 select case statements. It works perfect, but I want to know if there’s a way to group the range of cells together that are getting a 0 placed within them.

Sub compVal()

Dim WB As Workbook  'an object of type workbook
Dim WS1 As Worksheet ' objects of type worksheet

Set WB = ActiveWorkbook ' reference WB to the current Workbook
Set WS1 = Worksheets("Sheet1") 'Reference to Sheet 1 of the current workbook

'loop through sheet1's rows
Dim i As Integer

For i = 2 To WS1.UsedRange.Rows.Count

   Select Case WS1.Cells(i, 1).Value 'first cell from row i

      Case "Target"

         Select Case WS1.Cells(i, 2).Value

            Case 1

               WS1.Cells(i, 3).Value = 1
               WS1.Cells(i, 4).Value = 0
               WS1.Cells(i, 5).Value = 0
               WS1.Cells(i, 6).Value = 0

            Case 2

               WS1.Cells(i, 3).Value = 0
               WS1.Cells(i, 4).Value = 0
               WS1.Cells(i, 5).Value = 1
               WS1.Cells(i, 6).Value = 0

         End Select

      Case "NonTarget"

         Select Case WS1.Cells(i, 2).Value

            Case 1

               WS1.Cells(i, 3).Value = 0
               WS1.Cells(i, 4).Value = 1
               WS1.Cells(i, 5).Value = 0
               WS1.Cells(i, 6).Value = 0

            Case 2

               WS1.Cells(i, 3).Value = 0
               WS1.Cells(i, 4).Value = 0
               WS1.Cells(i, 5).Value = 0
               WS1.Cells(i, 6).Value = 1
         End Select

      End Select
Next i

End Sub

  • 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-18T12:59:53+00:00Added an answer on June 18, 2026 at 12:59 pm

    This is a great example of code re-use:

    Sub compVal()
    
    Dim WB As Workbook  'an object of type workbook
    Dim WS1 As Worksheet ' objects of type worksheet
    
    Set WB = ActiveWorkbook ' reference WB to the current Workbook
    Set WS1 = Worksheets("Sheet1") 'Reference to Sheet 1 of the current workbook
    
    'loop through sheet1's rows
    Dim i As Long ' USE LONG FOR CELL REFERENCES, THERE ARE A LOT OF ROWS POSSIBLE : )
    
    For i = 2 To WS1.UsedRange.Rows.Count
    
       Select Case WS1.Cells(i, 1).Value 'first cell from row i
    
          Case "Target"
    
             Select Case WS1.Cells(i, 2).Value
    
                Case 1
    
                   AddColumns WS1, i, 1, 0, 0, 0
    
                Case 2
    
                   AddColumns WS1, i, 0, 0, 1, 0
    
             End Select
    
          Case "NonTarget"
    
             Select Case WS1.Cells(i, 2).Value
    
                Case 1
    
                   AddColumns WS1, i, 0, 1, 0, 0
    
                Case 2
    
                   AddColumns WS1, i, 0, 0, 0, 1
    
             End Select
    
          End Select
    Next i
    End Sub
    
    Sub AddColumns(WS As Worksheet, i As Long, c As Variant, d As Variant, e As Variant, f As Variant)
        WS.Cells(i, 3).Resize(1, 4).Value = Array(c, d, e, f)
    
    End Sub
    

    There are other efficiencies that could be introduced, such as replacing the four arguments c,d,e and f with a single integer where

    0  = 0,0,0,0
    1  = 1,0,0,0
    2  = 0,1,0,0
    ...
    15 = 1,1,1,1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on a little backup script from some firebird databases and
I was working on a little script that would do some logging for me
I'm having little trouble creating a script working with URLs. I'm using urllib.urlopen() to
I'm having a little trouble with a script I'm working on for SharePoint 2010.
I'm working on a little Android app to stream some camera footage (as a
I have little problem with printing data like this, I have written script like
I am working on a little script that warns the user that his session
Writing a little command-line script at the moment, and I've got it working pretty
Why is this little script not working? <!DOCTYPE html> <html> <head> <meta charset=UTF-8> <title>Insert
A jQuery related question - I have a little gallery style script working -

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.