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 9110771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:20:54+00:00 2026-06-17T03:20:54+00:00

I have a question regarding copying cells from one sheet to another using VBA.

  • 0

I have a question regarding copying cells from one sheet to another using VBA.

Question Background:

Sheet1 of an excel spreadsheet contains headers and a number of test observations from multiple test subjects. Each row is an observation of one trial per subject. Each test subject has a static number of observations, i.e. 15 rows. I need to select an individual cell(s) from a specified row(s) and copy them to sheet2 of the same workbook into 1 row per subject. Sheet 2 will contain headers as well. Being new to VBA, and to programming/scripting in general, I’m having some issues wrapping my head around on how to do this. Below is a similar example of the data that I am trying to manipulate. The data set below contains 5 columns. The subject numbers are 1111 and 2222. Each heading of Variable() contains 1 letter, e.g. VariableOne = “a”, VariableTwo = “b”, etc.

Subject – VariableOne – VariableTwo – VariableThree – VariableFour

1111 – a b c d

1111 – e f g h

1111 – i j k l

1111 – m n o p

2222 – a b c d

2222 – e f g h

2222 – i j k l

2222 – m n o p

For example, row 2 (due to headers) of sheet 2 will include the subject number 1111 and values “m” from VariableOne, “f” from VariableTwo, “c” from VariableThree, and “l” from VariableFour. Row 3 of sheet 2 will contain subject #2222 and the values listed in the same position as subject 1111 (the values will be different but their position within the data set will be the same).

I think the code should include 2 loops; one to iterate through subjects and the other to iterate through the actual data. I’m not really sure as to how to go about doing this so if anybody has done this before, I would appreciate the help.

Additionally, I’ve just gone through the book “Microsoft Excel VBA programming for the absolute beginner”, which was a nice introduction to VBA for excel but it didn’t help me with formulating any solutions for my personal VBA/excel needs. Does anyone know of a better source, such as a book, to become more acquainted with the VBA scripting language for excel?

  • 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-17T03:20:55+00:00Added an answer on June 17, 2026 at 3:20 am

    Your question suggests to me that you are looking to copy the values on sheet one in positions m, f, c, and l into a single row on sheet two for each subject. The following does just that (and assumes that you have already manually copied over the headers into the same columns as sheet one). Let me know if my understanding of your question is mistaken and I will attempt to adjust the code sample accordingly.

    Sub CopyMySubjectsVariables()
    
        'I find making worksheet variables helps make code easier to understand
        Dim sheetOne, sheetTwo As Worksheet
        Set sheetOne = Worksheets("Sheet1")
        Set sheetTwo = Worksheets("Sheet2")
    
        'For the same reason, I set the column numbers to variables when possible
        Dim subjectCol, variableOneCol, variableTwoCol, variableThreeCol, variableFourCol As Integer
        subjectCol = 1
        variableOneCol = 2
        variableTwoCol = 3
        variableThreeCol = 4
        variableFourCol = 5
    
        'In your example table there are only four observations per subject 
        Dim numObservationsPerSubject As Integer
        numObservationsPerSubject = 4
    
        'Since Sheet Two also contains headers, the first subject will start on Row 2
        Dim subjectRowOnSheetTwo As Integer
        subjectRowOnSheetTwo = 2
    
        'Loop through all used rows of sheet one "stepping" from one subject to the next
        Dim subjectStartingRowOnSheetOne As Integer 'This variable is usually called "i" but I wanted to clarify it a bit
        For subjectStartingRowOnSheetOne = 2 To sheetOne.UsedRange.Rows.Count Step numObservationsPerSubject
    
            'Copy Subject Number/Name/Whatever From Sheet One to Sheet Two
            sheetTwo.Cells(subjectRowOnSheetTwo, subjectCol).Value = sheetOne.Cells(subjectStartingRowOnSheetOne, subjectCol).Value
    
            'Copy Variable One From the Fourth Row (startingRow+3) of the Subjects Observations ("m"'s position in your example)
            sheetTwo.Cells(subjectRowOnSheetTwo, variableOneCol).Value = sheetOne.Cells(subjectStartingRowOnSheetOne + 3, variableOneCol).Value
    
            'Copy Variable Two From Second Row (startingRow+1) of Subjects Observations ("f"'s position in your example)
            sheetTwo.Cells(subjectRowOnSheetTwo, variableTwoCol).Value = sheetOne.Cells(subjectStartingRowOnSheetOne + 1, variableTwoCol).Value
    
            'Copy Variable Three From First Row (startingRow) of Subjects Observations ("c"'s position in your example)
            sheetTwo.Cells(subjectRowOnSheetTwo, variableThreeCol).Value = sheetOne.Cells(subjectStartingRowOnSheetOne, variableThreeCol).Value
    
            'Copy Variable Three From Third Row (startingRow+2) of Subjects Observations ("l"'s position in your example)
            sheetTwo.Cells(subjectRowOnSheetTwo, variableFourCol).Value = sheetOne.Cells(subjectStartingRowOnSheetOne + 2, variableFourCol).Value
    
            'Increment the Starting Row on Sheet Two so the next subject starts on a new Row
            subjectRowOnSheetTwo = subjectRowOnSheetTwo + 1
    
        Next subjectStartingRowOnSheetOne
    
    End Sub
    

    After running this code on your example table, Sheet Two has the following:

    1111 m f c l

    2222 m f c l

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding importing( Via the import/export wizard) an excel spreadsheet into
I have question regarding about finding subview using '.tags' in one UIView. for (UIView
I have a question regarding the best practise of handling formated text when using
I have question regarding the service and activity. I have one service which calls
I have a question regarding the development of liferay portlets using the liferay plugin
I have a question regarding editing/saving data in database using Django. I have template
I have question regarding Java Comparator. Or maybe you all have another Idea to
I have a question regarding copying pointers in the stl library. Say I define:
I have question regarding shared memory segmentation in c using POSIX system calls. Is
I have a question regarding passing multiple arguments to a function, when using lapply

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.