+ Reply to Thread
Results 1 to 3 of 3

Macro help - Copy cells among several worksheets

Hybrid View

  1. #1
    Registered User
    Join Date
    11-25-2011
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    53

    Macro help - Copy cells among several worksheets

    Hi,

    I hope somebody can help with this problem, had some great help here in the past.

    I am trying to make a macro which will progress through a column of data and distribute the contents between several other sheets. The purpose is to allocate appointments between a certain number of people who are available for that particular day.

    My main sheet has a list of names and telephone numbers for people who we need to contact. The number of people that the appointments are to be allocated between varies from day to day so I have a cell where I enter this info (name = numPeople), as well as a cell which totals the number of appointment (name = totalEntries). So if there are 100 appointments and 10 people working they should get 10 appointments each. The appointments are ordered by an estimated time, so they should be allocated one at a time between the sheets working down the list. Here is what I have made so far, but it doesn't seem to do anything other than quickly select each cell in turn.

    Sub Allocate()
    
        Dim mainCounter As Integer
        Dim sheetCounter As Integer
        Dim lineCounter As Integer
            
    'tracking position in the main data
    mainCounter = "1"
    'sheetCounter will be a number equal to one of the worksheets
    sheetCounter = "0"
    'lineCounter will track the row where date is actively being pasted (starting at 6 - headers in the first 5 rows)
    lineCounter = "6"
    
    While mainCounter < totalEntries
    
        'select correct worksheet using numPeople
        If sheetCounter = numPeople Then
            sheetCounter = "1"
            lineCounter = lineCounter + 1
        Else
            sheetCounter = sheetCounter + 1
        End If
        
        Sheets("Data").Cells("A" & mainCounter).Copy Destination:=Sheets("num" & sheetCounter).Range("A" & lineCounter)
        
        mainCounter = mainCounter + 1
        
    
    Wend
    
    End Sub
    The code is supposed to use 3 counters:

    mainCounter to track position in main data (sheet name "Data")
    sheetCounter to decide which sheet the data should be pasted to (sheets named "num1", "num2" etc)
    lineCounter to decide which row it should pasting onto in each sheet

    Any help/pointers as to where I'm going wrong would be appreciated, not having much look with Google.

    Regards
    Diberlee

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro help - Copy cells among several worksheets

    you don't need the totalEntries cell, the macro can see the last row with data by simply looking up column A. How's this:
    Option Explicit
    
    Sub AllocateCalls()
    Dim mCounter As Long, sCounter As Long
    Dim Callers As Long, LR As Long, Rw As Long
    
    With ActiveSheet
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
        Callers = .Range("numPeople").Value
    
        If Callers = 0 Then
            MsgBox "Please set the number of people to greater than zero"
            Exit Sub
        End If
        
        For Rw = 6 To LR
            If sCounter = Callers Then sCounter = 1 Else sCounter = sCounter + 1
            .Range("A" & Rw).EntireRow.Copy Sheets("num" & sCounter).Range("A" & Rows.Count).End(xlUp).Offset(1)
        Next Rw
    End With
    
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    11-25-2011
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    53

    Re: Macro help - Copy cells among several worksheets

    Thanks JBeaucaire, I'll give that a go when I have some time free and let you know

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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