?
Solved

Easy:  Copy names from one sheet to another. (EXCEL)

Posted on 2004-08-13
15
Medium Priority
?
214 Views
Last Modified: 2010-05-02
This is what I was looking for when I meant copy column data over.  I only wanted to copy the names into a another sheet but I want them to be in 4 columns instead of one long column?  4 cols of 15 person each. There is spacing problems too.

[code]
John Smith            data
                      data
Paul Rucks                      data
Steve Parker      data
                      data
            data
[/code]

How do I get a list of only the names?


Sub CopyTraders()

    Dim Trader As Range
    Dim Traders As Range
    Dim ColCount As Integer
   
    Sheets("Report").Select
    Range("A1").Select
   
    Set Traders = Range(ActiveCell, ActiveCell.End(xlDown))
   
    Sheets("Layout").Select
    Range("A1").Select
   
    ColCount = 1
    For Each Trader In Traders
        If ColCount > 3 Then  ' set this value to the number of columns you require
            ColCount = 1
            ActiveCell.Offset(1, 0).Select
        End If
        ActiveCell.Offset(0, (ColCount - 1) * 2).Value = Trader
        ColCount = ColCount + 1
    Next Trader
   
   
End Sub
0
Comment
Question by:efarhat
  • 6
  • 5
  • 3
  • +1
15 Comments
 
LVL 8

Expert Comment

by:bramsquad
ID: 11793264
unless you have two excel objects open, your going to have to store the data in a temp variable...or possibly look into programmatically saving the data to the clipboard

~b

0
 

Author Comment

by:efarhat
ID: 11793563
i don't understand? it's one excel app one with 2 sheets.  copy data from one sheet and move it to the other. that code right there works except the logic is incorrect.
0
 
LVL 15

Expert Comment

by:Colosseo
ID: 11793711
Hi can you explain again what you are after... maybe with before and after examples

thanks

scott
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 77

Expert Comment

by:GrahamSkan
ID: 11794100
Sub CopyTraders()
    Dim Trader As String
    Dim Traders As Range
    Dim r As Integer
    Dim r2 As Integer
    Dim ColCount As Integer
    Dim rng As Range
   
    Set rng = ActiveWorkbook.Sheets("Report").UsedRange
    ColCount = 1
    For r = 1 To rng.Rows.Count
        Debug.Print rng.Cells(r, 1).Value
       
        If rng.Cells(r, 1).Value <> "" Then
            r2 = r2 + 1
            Trader = rng.Cells(r, 1).Value
        End If
        ActiveWorkbook.Sheets("Layout").Cells(r2, 1) = Trader
        ActiveWorkbook.Sheets("Layout").Cells(r2, ColCount + 1) = rng.Cells(r, 2)
        ColCount = (ColCount + 1) Mod 4
    Next r
End Sub

0
 

Author Comment

by:efarhat
ID: 11794269
Scott,

thanks for reading my post.  let me explain. if you scroll up to the first post, you'll see names in one column and data in other columns.  i want to copy ONLY the names which are in the first column.  Although, the names don't follow one by one (row by row).  From the example at the top, "John Smith" is on row 1 and "Paul Rucks" is on row 3.

Once you copy all the names, I want them to be displayed in Sheet2, one after another.
ie.
John Smith
Paul Rucks
Steve Parker

but there are a lot of names, 50 total and I want to display them in 4 columns, names per column.  (i know the last column will only have 5.)
0
 
LVL 15

Expert Comment

by:Colosseo
ID: 11794456
Hi there

I dont think I totally understand what you mean by  I want to display them in 4 columns, names per column.  (i know the last column will only have 5.)

Anyway the code below will go needs you to specify the start and end rows to search it will then go through each row in sheet1 checking the value in column A.
If the value is not empty then it is moved to sheet2

Give it ago and if i have missed something (which i think i have) then let me know :)

Regards

Scott

 Sub get_Names()

  Dim int_Start_Cell
  Dim int_Stop_Cell

  int_Start_Cell = 1
  int_Stop_Cell = 8

  ' Select cell A1 on sheet2
  Sheets("Sheet2").Select
  Range("A1").Select

  ' Loop from the starting cell to the end cell
  For int_Current_Cell = int_Start_Cell To int_Stop_Cell
     ' If the value in the cell is not empty
     If Sheets("Sheet1").Range("A" & int_Current_Cell).Value <> "" Then
       ' Output the name to Sheet2
       ActiveCell.Value = Sheets("Sheet1").Range("A" & int_Current_Cell).Value
       ' Move to the next cell
       ActiveCell.Offset(1, 0).Select
     End If
  Next

End Sub
0
 
LVL 77

Accepted Solution

by:
GrahamSkan earned 200 total points
ID: 11794479
Sorry, I misunderstood. What I did would display the names in col 1 and the data in cols 2 to 5.

Try this instead.

Sub CopyTraders()
    Dim Trader As String
    Dim r As Integer
    Dim r2 As Integer
    Dim ColCount As Integer
    Dim rng As Range
   
    Set rng = ActiveWorkbook.Sheets("Report").UsedRange
    r2 = 1
    For r = 1 To rng.Rows.Count
        If rng.Cells(r, 1).Value <> "" Then
            Trader = rng.Cells(r, 1).Value
            ActiveWorkbook.Sheets("Layout").Cells(r2, ColCount + 1) = Trader
            ColCount = (ColCount + 1) Mod 4
            If ColCount = 0 Then
                r2 = r2 + 1
            End If
        End If
    Next r
End Sub

0
 

Author Comment

by:efarhat
ID: 11796314
Colosseo,

You don't have int_Current_Cell defined.
when i sent int_start_cell = 4 and int_current_cell =4 it still doesn't go into the loop. not sure why.
For int_Current_Cell = int_Start_Cell To int_Stop_Cell ??
0
 
LVL 15

Expert Comment

by:Colosseo
ID: 11796776
hi

try

int_Start_Cell = 4
int_Stop_Cell = 5

int_Current_Cell id defined in the for statement

For int_Current_Cell = int_Start_Cell To int_Stop_Cell

Scott
0
 

Author Comment

by:efarhat
ID: 11796990
scott,

how can you define the id like that?  i received an error saying,  variable not defined
0
 

Author Comment

by:efarhat
ID: 11797188
Grahman Scam, your code worked. i had to make some modifications but it's working correctly.
0
 
LVL 15

Expert Comment

by:Colosseo
ID: 11797191
Hi efarhat

sorry about that thats my fault, you're one of those crazy people who uses Option Explicit :)

try this code instead... with Option Explicit you have to Dimensionalise(spelling) every variable which is good for error checking and is good practise

Sub get_Names()
 
  Dim int_Current_Cell
  Dim int_Start_Cell
  Dim int_Stop_Cell

  int_Start_Cell = 1
  int_Stop_Cell = 8

  ' Select cell A1 on sheet2
  Sheets("Sheet2").Select
  Range("A1").Select

  ' Loop from the starting cell to the end cell
  For int_Current_Cell = int_Start_Cell To int_Stop_Cell
     ' If the value in the cell is not empty
     If Sheets("Sheet1").Range("A" & int_Current_Cell).Value <> "" Then
       ' Output the name to Sheet2
       ActiveCell.Value = Sheets("Sheet1").Range("A" & int_Current_Cell).Value
       ' Move to the next cell
       ActiveCell.Offset(1, 0).Select
     End If
  Next

End Sub

Regards

Scott
0
 
LVL 15

Expert Comment

by:Colosseo
ID: 11797212
I just tried Grahams code....

I now know what you meant by I want to display them in 4 columns, names per column.  (i know the last column will only have 5.)

I also know my code doesnt do that :)

Scott
0
 
LVL 77

Expert Comment

by:GrahamSkan
ID: 11798991
efarhat, thanks for trying the code. I trust that your misspelling of my surname was accidental.
Graham
0
 

Author Comment

by:efarhat
ID: 11833915
indeed it was.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month8 days, 21 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question