Solved

Copy Multiple Columns from one sheet to another with different headers

Posted on 2014-04-10
9
152 Views
Last Modified: 2014-04-14
Hi,

I have some VBA that copies one sheet from one workbook to another. Now that I have the data in the same workbook. I have searched, but have not been able to find a solution specific to what I need. I would like to copy specific columns from Store Schedule tab to the RC Schedule tab however the columns are not the same and the column names on Store Schedule tab can be in row 2 or 3 or both.  Example of some of the columns that I need to copy (maintaining the column header in the RC Schedule tab).  

From Store Schedule to RC Schedule Column
                  
1. Column A to A
2. Column C to B
3. Column D to D
4. Column E to F
5. Column HG to J

Attaching a sample of the file as well
International-Store-Schedule-Tem.xls
0
Comment
Question by:jmac001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 39993225
are you sure you want C to B?  There is already a StoreNumber column on RC.
0
 

Author Comment

by:jmac001
ID: 39993936
Yes, the store number in the RC tab is different than the store number in the Store Schedule tab.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 39994679
See if this works for you.
Option Explicit

Public Sub Q_28409476()
    Dim wksFrom As Worksheet
    Dim wksTo As Worksheet
    Dim rngFrom As Range
'    Dim rngTo As Range
    Set wksFrom = Worksheets("Store Schedule")
    Set wksTo = Worksheets("RC Schedule")
    Set rngFrom = wksFrom.Range(wksFrom.Range("A5"), wksFrom.Range("A5").End(xlDown))
    rngFrom.Copy wksTo.Range("A2")
    Set rngFrom = wksFrom.Range(wksFrom.Range("C5"), wksFrom.Range("C5").End(xlDown))
    rngFrom.Copy wksTo.Range("B2")
    Set rngFrom = wksFrom.Range(wksFrom.Range("D5"), wksFrom.Range("D5").End(xlDown))
    rngFrom.Copy wksTo.Range("D2")
    Set rngFrom = wksFrom.Range(wksFrom.Range("E5"), wksFrom.Range("E5").End(xlDown))
    rngFrom.Copy wksTo.Range("F2")
    Set rngFrom = wksFrom.Range(wksFrom.Range("HG5"), wksFrom.Range("HG5").End(xlDown))
    rngFrom.Copy wksTo.Range("J2")
End Sub

Open in new window

0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:jmac001
ID: 39995265
Question, I have fields that will not have data, how do I account for that in the VBA.  I placed the code in my working file  and where ever there was a break in the data is where the data stopped for that column.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 39995301
That's generally done by using the lastcell special reference or invoking a Find method to get the last cell in a column (or row).  Do you know how to do that?
0
 

Author Comment

by:jmac001
ID: 39998783
No, I don't
0
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 39999218
Test this version.
Option Explicit


Public Sub Q_28409476()
    Dim wksFrom As Worksheet
    Dim wksTo As Worksheet
    Dim rngFrom As Range
    Dim lngLastRow As Long
    
    Set wksFrom = Worksheets("Store Schedule")
    lngLastRow = wksFrom.Cells.SpecialCells(xlCellTypeLastCell).Row
    Set wksTo = Worksheets("RC Schedule")
    Set rngFrom = wksFrom.Range(wksFrom.Cells(5, 1), wksFrom.wksFrom.Cells(lngLastRow, 1))
    rngFrom.Copy wksTo.Range("A2")
    Set rngFrom = wksFrom.Range(wksFrom.Cells(5, 3), wksFrom.wksFrom.Cells(lngLastRow, 3))
    rngFrom.Copy wksTo.Range("B2")
    Set rngFrom = wksFrom.Range(wksFrom.Cells(5, 4), wksFrom.wksFrom.Cells(lngLastRow, 4))
    rngFrom.Copy wksTo.Range("D2")
    Set rngFrom = wksFrom.Range(wksFrom.Cells(5, 5), wksFrom.wksFrom.Cells(lngLastRow, 5))
    rngFrom.Copy wksTo.Range("F2")
    Set rngFrom = wksFrom.Range(wksFrom.Cells(5, 215), wksFrom.wksFrom.Cells(215, lngLastRow))
    rngFrom.Copy wksTo.Range("J2")
End Sub

Open in new window

0
 

Author Comment

by:jmac001
ID: 39999842
Get errror 1004  (application - defined or or object-defined error) on:

Line 21 -  Set rngFrom = wksFrom.Range(wksFrom.Cells(5, 215), wksFrom.Cells(215 lngLastRow))
0
 
LVL 45

Expert Comment

by:aikimark
ID: 39999930
That should be:
Set rngFrom = wksFrom.Range(wksFrom.Cells(5, 215), wksFrom.Cells(lngLastRow, 215))

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

759 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