CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual Basic Programming > Visual Basic 6.0 Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Visual Basic 6.0 Programming Ask questions about VB 6.0 (or earlier versions) or help others by answering their question.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 3rd, 2009, 10:28 PM
    vbadvt vbadvt is offline
    Junior Member
     
    Join Date: Sep 2009
    Posts: 9
    vbadvt is an unknown quantity at this point (<10)
    Copying data from one excel sheet and pasting into another sheet (Transpose)

    Hello everyone,

    I need to copy data present in a single column from one excel sheet and to paste the data along a single row in another excel sheet.

    For example, I have data 'data(1), data(2), data(3).....data(10)' present in column B, in cells (B1, B2,B3,B4,B5,B6,B7,B8,B9,B10) of sheet 1. I need to copy and paste these 10 data into sheet 2, along row 1, say in cells 'A1, B1, C1, D1, E1, F1, G1, H1, I1, J1'. This is similar to transpose operation. I am looking for a 'for loop' to perform this operation. I need to program this in VB6.

    Please help me. Thanks
    Reply With Quote
      #2    
    Old November 3rd, 2009, 11:49 PM
    dglienna's Avatar
    dglienna dglienna is offline
    ex MVP - Visual Basic
    Power Poster
     
    Join Date: Jan 2006
    Location: Chicago, IL
    Posts: 9,895
    dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)
    Re: Copying data from one excel sheet and pasting into another sheet (Transpose)

    That IS a Transpose, if you ask me. Record a TRANSPOSE macro in Excel, and then use that. Why re-invent the wheel?
    __________________
    David CodeGuru Article: Bound Controls are Evil-VB6
    101 Samples: VB & C# VS2008 Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!
    Reply With Quote
      #3    
    Old November 4th, 2009, 12:59 PM
    vbadvt vbadvt is offline
    Junior Member
     
    Join Date: Sep 2009
    Posts: 9
    vbadvt is an unknown quantity at this point (<10)
    Re: Copying data from one excel sheet and pasting into another sheet (Transpose)

    Thanks for the reply.

    However, there is a small concern, which I tried to mention in my earlier post. I cannot copy all the 10 data, at once, from sheet 1 to sheet 2. This is because it is possible that some of the data may not be present. I need to write a small piece of code that needs to work with different files, which may have this condition.

    data(1) should be pasted in A1, data(2) in B1, data(3) in C1, data(4) in D1, data(5) in E1, data(6) in F1, data(7) in G1, data(8) in H1, data(9) in I1 and data(10) in J1. For example, a file may have the following contents in the cells B1 to B9 as , data(1), data(2), data(4), data(5), data(6), data(7), data(8), data(9), data(10). This needs to be transposed and pasted in sheet 2 in the appropriate cells namely: A1, B1, D1, E1, F1, G1, H1, I1, J1. In this case, if I copy all the data, at once, then the data may not be pasted in the corresponding cells on sheet 2, as they may be pasted in A1, B1, C1, D1, E1, F1, G1, H1, I1, instead of what I had posted above (which is what I need). Basically, I would need to perform a search on each string namely data(1), data(2) etc. and if found, need to copy that onto the corresponding cells in Sheet 2.

    This is a small example that I have given for understanding purposes. I have hundreds of such data to be transposed from one sheet to another. I believe a 'loop' may be useful but I am unable to move forward.

    Looking for help. Thanks
    Reply With Quote
      #4    
    Old November 4th, 2009, 04:42 PM
    dglienna's Avatar
    dglienna dglienna is offline
    ex MVP - Visual Basic
    Power Poster
     
    Join Date: Jan 2006
    Location: Chicago, IL
    Posts: 9,895
    dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)
    Re: Copying data from one excel sheet and pasting into another sheet (Transpose)

    Wow, who designed EXCEL to be a DB? Not good...

    I'd import everything and start again.
    __________________
    David CodeGuru Article: Bound Controls are Evil-VB6
    101 Samples: VB & C# VS2008 Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!
    Reply With Quote
      #5    
    Old November 4th, 2009, 08:20 PM
    vbadvt vbadvt is offline
    Junior Member
     
    Join Date: Sep 2009
    Posts: 9
    vbadvt is an unknown quantity at this point (<10)
    Re: Copying data from one excel sheet and pasting into another sheet (Transpose)

    Import everything and start again?

    Could you please explain what you meant? I did not understand.

    Thanks
    Reply With Quote
      #6    
    Old November 4th, 2009, 09:12 PM
    dglienna's Avatar
    dglienna dglienna is offline
    ex MVP - Visual Basic
    Power Poster
     
    Join Date: Jan 2006
    Location: Chicago, IL
    Posts: 9,895
    dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)
    Re: Copying data from one excel sheet and pasting into another sheet (Transpose)

    Access can read Excel files, and import them. Your field names need work, and what you need isn't even clear.

    If you can't get transpose to work, then how would you code it?
    __________________
    David CodeGuru Article: Bound Controls are Evil-VB6
    101 Samples: VB & C# VS2008 Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!
    Reply With Quote
      #7    
    Old November 4th, 2009, 11:50 PM
    vbadvt vbadvt is offline
    Junior Member
     
    Join Date: Sep 2009
    Posts: 9
    vbadvt is an unknown quantity at this point (<10)
    Re: Copying data from one excel sheet and pasting into another sheet (Transpose)

    What I need to do is to check if the string 'data(1)' exists in sheet 1 and if it does, then I would need to copy it and transpose it to the other sheet (sheet 2). Similarly, I would need to do this for the rest of the data. So, I am thinking that we would need to use a loop to run the search for different strings 'data(1), data(2)..' where the count variable will be the index value of data. Looking for a code snippet which would perform this action.

    In a nutshell, here is what I need:

    In Sheet 1, search for string 'data(1)'. If found, then copy it and paste it in the cell 'A1' in Sheet 2.

    Go back to sheet 1. Search for string 'data(2)'. If found, then copy it and paste it in the cell 'B1' in Sheet 2.

    Go back to sheet 1. Search for string 'data(3)'. If found, then copy it and paste it in the cell 'C1' in Sheet 2.

    I need to repeat this action for 100s of data. So, a sample code snippet will be really helpful.

    Looking for help. Thanks
    Reply With Quote
      #8    
    Old November 5th, 2009, 02:15 AM
    dglienna's Avatar
    dglienna dglienna is offline
    ex MVP - Visual Basic
    Power Poster
     
    Join Date: Jan 2006
    Location: Chicago, IL
    Posts: 9,895
    dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)dglienna has a brilliant future (2000+)
    Re: Copying data from one excel sheet and pasting into another sheet (Transpose)

    Record a macro doing it once, and post that. Use an IF to change color or something when it's blank, maybe.

    You want to look in cell x, and see if it's blank and if not, paste it.

    I guess if it's blank, you don't want a blank in sheet 2?

    If that's the case, use a formula to delete the blank ones after you transpose
    __________________
    David CodeGuru Article: Bound Controls are Evil-VB6
    101 Samples: VB & C# VS2008 Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!
    Reply With Quote
      #9    
    Old November 5th, 2009, 05:29 AM
    vbadvt vbadvt is offline
    Junior Member
     
    Join Date: Sep 2009
    Posts: 9
    vbadvt is an unknown quantity at this point (<10)
    Re: Copying data from one excel sheet and pasting into another sheet (Transpose)

    Thanks dglienna for your replies. I have recorded a macro of what I exactly need. If I am not able to find a string in sheet 1, then I would need to have an empty cell on sheet 2, corresponding to that string. Here is the macro:

    Cells.Find(What:="Student: 1", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate

    Range("D11").Select
    Selection.Copy

    Sheets("Sheet2").Select
    Range("M6").Select

    ActiveSheet.Paste
    Sheets("Sheet1").Select

    Cells.Find(What:="Student: 2", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate

    Range("D12").Select
    Application.CutCopyMode = False
    Selection.Copy

    Sheets("Sheet2").Select
    Range("N6").Select
    ActiveSheet.Paste

    Sheets("Sheet1").Select
    Cells.Find(What:="Student: 3", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate

    Range("D13").Select
    Application.CutCopyMode = False
    Selection.Copy

    Sheets("Sheet2").Select
    Range("O6").Select
    ActiveSheet.Paste


    Basically I am searching for strings: Student(1), Student(2), Student(3)... etc... These strings are found in Column 'A'. However, I want to copy their marks which is stored in column 'D'. So, I copy columns 'D11', 'D12', 'D13' and so on and paste them in sheet 2 on Row 6. In Sheet 2, Column M, corresponds to student 1, column N corresponds to student 2 and column O corresponds to student 3 and so on. If I do not find info for a particular student, say Student (2), then I should leave the Cell N6, in Sheet 2 blank. This cell corresponds to Student 2.

    Hope I have been clear this time and looking forward for help.

    Thank you.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual Basic Programming > Visual Basic 6.0 Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 02:23 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009