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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:18:09+00:00 2026-05-31T03:18:09+00:00

Dear competent people. I’m having a problem with the following code, specifically that the

  • 0

Dear competent people.

I’m having a problem with the following code, specifically that the sub completes correctly but does not copy the correct data to the correct location. I get a repeating pattern of lines of zeros which does not correlate with the iterators in place.

I think the problem is with copying the values from a sub-set of a range, Episode&r. Previously I looked at using the union property but this was shown to be wrong by a commenter below.

Currently I nine ranges named “Episode”1-9 each row of which contains data for one respondent. Columns 5 through 15 of these ranges contain the data to be copied, therefore the range to be copied for each respondent is: row i, columns five through fifteen. This is the step I an stuck with.

If I could copy it, the data would end up on sheet2 where a range has been named for each respondent, called Respondent&n. The rows of Response&n represent time slots during which Episode&r can occur. Outside of slots where Episode&r occurs there can be zeroes, but this isn’t actually necessary.

The logical structure appears to work fine. I have watched the Local values for the counters closely in debugging and they work as they are supposed to.

I am currently looking at using the Range.Item method to select row ‘n’, columns 5-15 from Episode&r, but cannot get it right.

Any assistance at all would be very much appreciated.

A link to an example sheet is here: http://dl.dropbox.com/u/41041934/StackOverflow/TornHairExampleSheet.xlsm

Sub PopulateMedia()
Application.ScreenUpdating = False
Sheets(1).Activate

'Count the total number of response rows in original sheet
Dim Responses As Long, n As Integer, i As Integer, j As Integer, r As Integer
Responses = Sheets("Sheet1").Range("A:A").End(xlDown).row

'For each response...
For n = 1 To Responses
Dim curr_resp As Range
Set curr_resp = Sheets(2).Range("Response" & n) 'Define a range containing all response data
    For r = 1 To 9 'For each episode...
        Dim curr_ep As Range 'Define a range containing episode data for all responses
        Set curr_ep = Sheets(1).Range("episode" & r)

'Variables contain start, end and inter-episode times
        Dim Stime As Integer, Etime As Integer, Itime As Integer 
    Stime = curr_ep.Cells(n, 1).Value
    Etime = curr_ep.Cells(n, 16).Value
    Itime = curr_ep.Cells(n, 18).Value

'Define a range within this episode which contains the columns to be copied
 Dim media As Range 
    Sheets(1).Activate
    Set media = Set media = Sheets(1).Range("Episode" & r).Item(n, "5:15") 'range to be copied is union of active episode and active response.***This line is certainly incorrect, example purpose.

    Sheets(2).Activate

'for each time-slot...***This is the section I'm having trouble with
        For i = 1 To (Etime + Itime) 
            If i > Etime Then
'fill the response range with zeros for time slots outside Stime and Etime
            Sheets(2).Range("Response" & n).Rows = 0 
            ElseIf i >= Stime Then
'Copy data from above union for slots between Stime and Etime
            Sheets(2).Range("Response" & n).Rows(i) = media 
            Else
'Stick with the zeroes until a new 'r' means a new episode***
            Sheets(2).Range("Response" & n).Rows(i) = 0 
            End If
        Next i
    Next r
Next n
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-05-31T03:18:10+00:00Added an answer on May 31, 2026 at 3:18 am

    To be honest, your spreadsheet is a real mess, which is also probably why you find it difficult to work with it!

    Anyway, what you are trying to achieve seems to be: in your range named episode1, you would like to capture the row number i which corresponds to your i-th respondent and copy the information to your second sheet. And do that for each episode and respondent. If that is the case, the code below seems to be doing what you want. It is not very clean and could be improved further.

    Sub PopulateMedia()
        Application.ScreenUpdating = False
    
        'Count the total number of response rows in original sheet
        Dim Responses As Long, n As Integer, i As Integer, j As Integer, r As Integer
        Responses = Sheets("Sheet1").Range("A:A").End(xlDown).Row
    
        'For each response...
        For n = 1 To Responses
            Dim curr_resp As Range
            Set curr_resp = Sheets(2).Range("Response" & n) 'Define a range containing all response data
            For r = 1 To 9 'For each episode...
                Dim curr_ep As Range 'Define a range containing episode data for all responses
                Set curr_ep = Sheets(1).Range("episode" & r)
                Dim Stime As Integer, Etime As Integer, Itime As Integer 'Variables contain start, end and inter-episode times
                Stime = curr_ep.Cells(n, 1)
                Etime = curr_ep.Cells(n, 16)
                Itime = curr_ep.Cells(n, 18)
                Dim media As Range 'Define a range within this episode which contains the columns to be copied
                Set media = Sheets(1).Range("Episode" & r)
                For i = 1 To (Etime + Itime) 'for each time-slot...***This is the section I'm having trouble with
                    If i > Etime Then
                      curr_resp.Rows(i) = 0 'fill the response range with zeros for time slots outside Stime and Etime
                    ElseIf i >= Stime Then
                      Dim a As Variant
                      a = media.Range(media.Cells(n, 5), media.Cells(n, 15))
                      curr_resp.Rows(i).Resize(1, 11) = a 'Copy data from above union for slots between Stime and Etime
                    Else
                      curr_resp.Rows(i) = 0 'Stick with the zeroes until a new 'r' means a new episode***
                    End If
                Next i
            Next r
        Next n
    
        Application.ScreenUpdating = True
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Dear all, I have a select query that currently produces the following results: DoctorName
Dear Folk's i'm using the following code in order to send the bytes of
Dear g++ hackers, I have the following question. When some data of an object
Dear Masters! Is it possible to ensure, that only characters with codes between 0
Dear stackoveflow, I have this problem. I'm working with an old version of mssql
dear all..i have this code: <script> var str=KD-R435MUN2D; var matches=str.match(/(EE|[EJU]).*(D)/i); if (matches) { var
Dear visitors stackoverflow! I can not solve a simple problem: Body given the background
Dear all, I am writing a python program that is retrieving edifact log messages
Dear Friends good afternoon. My problem may be this is very basic one i.e.
Dear honorable people of stackoverflow, why is my textbox, which is my validated control,

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.