+ Reply to Thread
Results 1 to 5 of 5

copy column in WB1 and paste it in specified columns in WB2

Hybrid View

  1. #1
    Registered User
    Join Date
    06-07-2012
    Location
    bangalore
    MS-Off Ver
    2007
    Posts
    5

    copy column in WB1 and paste it in specified columns in WB2

    hi,

    i have 2 WB's
    WB1 --> sheets 1(update1)-->columns(first name, middle name, last name, address, address1...)
    WB2 -->8sheets(with specific names) -->columns(varies but might have same column name as in WB1 or might not have)

    issue: need to copy a column from WB1 say first name, paste it in all relavent sheets with column name "first name"
    again search for last name and paste in WB2 on relavent sheets and in relavent column name,

    i have coded a bit here but it does in same workbook but not in different work book. please have a look

    Option Explicit
    'Function to check if worksheets entered in input boxes exist
    Public Function wsExists(ByVal WorksheetName As String) As Boolean
    
    On Error Resume Next
    wsExists = (Sheets(WorksheetName).Name <> "")
    On Error GoTo 0 ' now it will error on further errors
    
    End Function
    Sub copy_using_header()
    
    Dim i As Integer
    Dim a(1 To 1) As Integer
    Dim b(1 To 1) As Integer
    Dim lkup As String
    Dim dummy As Variant
    Dim Sheet_Copy_From As String
    Dim Sheet_Copy_To As String
    Dim sn As Variant 'sheet name from array to test
    Dim an As Variant 'Array
    Dim lkr As range
    Dim ahd As Variant
    Dim chd As Variant
    Dim cn As Long
    Dim ws As Worksheet
    Dim lkr1 As range
    Dim ahd1 As Variant
    Dim chd1 As Variant
    Dim cn1 As Long
    Dim ws1 As Worksheet
    
    
    Application.ScreenUpdating = False 
    
    Sheet_Copy_From = Application.InputBox(Prompt:= _
            "Please enter the sheet name you which to copy from", _
            Title:="Sheet_Copy_From", Type:=2) 'Type:=2 = text
        If Sheet_Copy_From = "False" Then 
        Exit Sub
        End If
         
    Sheet_Copy_To = Application.InputBox(Prompt:= _
            "Please enter the sheet name you which to paste in", _
            Title:="Sheet_Copy_To", Type:=2) 'Type:=2 = text
        If Sheet_Copy_To = "False" Then 
        Exit Sub
        End If
        
        
      an = Array(Sheet_Copy_From, Sheet_Copy_To)
      
      For Each sn In an
        
        Select Case wsExists(sn)
        Case False
          MsgBox "The worksheet named ....""" & sn & """ .... is either missing" & vbNewLine & _
                "or spelt incorrectly" & vbNewLine & vbNewLine & _
                "Please rectify and then run this procedure again" & vbNewLine & vbNewLine & _
                "Select OK to exit", _
                vbInformation, ""
                
          Exit Sub
        End Select
     Next
    
    For i = 1 To 1
    
    Select Case i
        Case 1
        lkup = Application.InputBox(Prompt:= _
            "Please enter column heading name", _
            Title:="InputBox Method", Type:=2) 
    End Select
    If lkup = "False" Then 
        Exit Sub
        End If
    
    On Error Resume Next 
    
        a(i) = Sheets(Sheet_Copy_From).Rows(1).Find(lkup, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False).Column
        b(i) = Sheets(Sheet_Copy_To).Rows(1).Find(lkup, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False).Column
    
    
         Sheets(Sheet_Copy_From).Select
         range(Cells(2, a(i)), Cells(Cells(Rows.Count, a(i)).End(xlUp).Row, a(i))).Copy ' Only copies from row 2
           
            
        Sheets(Sheet_Copy_To).Activate
            With Cells(2, b(i)) ' Pastes from row 2 down
              .PasteSpecial Paste:=xlPasteValues
            End With
            
        Application.CutCopyMode = False 
     Next 
    On Error GoTo 0         
        Set dummy = Worksheets(1).Cells.Find(What:=" ", after:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)    
        Application.ScreenUpdating = True  
    End Sub
    Last edited by arlu1201; 06-20-2012 at 05:48 AM. Reason: Use code tags in future.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: copy column in WB1 and paste it in specified columns in WB2

    Sreenivas,

    Please attach sample files so its easy to test your code.

    To Attach a File:

    1. Click on Go Advanced.
    2. In the frame Attach Files you will see the button Manage Attachments
    3. Click the button.
    4. A new window will open titled Manage Attachments - Excel Forum.
    5. Click the Browse... button to locate your file for uploading.
    6. This will open a new window File Upload.
    7. Once you have located the file to upload click the Open button. This window will close.
    8. You are now back in the Manage Attachments - Excel Forum window.
    9. Click the Upload button and wait until the file has uploaded.
    10. Close the window and then click Submit.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    06-07-2012
    Location
    bangalore
    MS-Off Ver
    2007
    Posts
    5

    Re: copy column in WB1 and paste it in specified columns in WB2

    hi,,

    sorry for not having that posted

    i would not be able to post the file as it has some secured data in it
    i will try to make a duplicate copy of it and post it

    regards,
    sreenivas

    thank you for replying

  4. #4
    Registered User
    Join Date
    06-07-2012
    Location
    bangalore
    MS-Off Ver
    2007
    Posts
    5

    Re: copy column in WB1 and paste it in specified columns in WB2

    hi can you alter the above code so that we need to copy from (search for column name) from workbook 1(Staffupload.xlsm)
    and paste it in workbook2(finalstaffupload.xlsm).

    thanks,

    sreenivas.

  5. #5
    Registered User
    Join Date
    06-07-2012
    Location
    bangalore
    MS-Off Ver
    2007
    Posts
    5

    Re: copy column in WB1 and paste it in specified columns in WB2

    staffupload.xlsfinalStaff.xls


    hi,

    please find the attachments.
    i have created a sample for you.
    there are 2 spread sheets.
    1 Staffupload.xls
    2 FinalStaff.xls
    i want to move the data from Staffupload.xls to
    FinalStaff.xls to specific columns by matching column names.
    can u write macro for me!!
    thanks in advance

+ 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