Copy Data From Multiple Columns in One Sheet to Another & Add Pre-Defined Value

censo

Board Regular
Joined
Dec 25, 2015
Messages
161
Office Version
  1. 2013
Platform
  1. Windows
Hello everyone,

On Sheet1 there's multiple columns with data of which I only need the data of 2 columns; G & H.

The Challenge:

1. The lines of data (rows) will vary (so cannot hard code it to the last line of data that's present at any given moment

2. The data to be copied over from Sheet 1 (columns G & H) cannot include the header information, so copying of cells begins at row 2

3. Column H, Sheet 1 needs to go to Column A, Sheet 2 (under the header - so beginning on row 2)
3a. Column G, Sheet 1 needs to go to Column B, Sheet 2 (under the header - so beginning on row 2)

4. Lastly, the exact string "USD" must be copied down, but only to the last row of data.

Can such a solution be crafted with functions or do we need to employ VBA code?

Thanks!


Excel 2012
ABCDEFGHIJKLMNOPQR
1local_idvendor_urlvendor_variantvendor_stockvendor_pricevendor_shippingtotalreferencecompare_urlcompare_variantcompare_stockcompare_pricecompare_shippingprofit_formulaselling_formulareprice_storereprice_skureprice_pause
2555340http://www.walmart.com/ip/Nine-Stars-18.5-Gallon-Motion-Sensor-Recycle-Unit-and-Trash-Can-Stainless-Steel/15819203186086WAL-15819203#1453535282996000AMAZON_USB0031M9H300
3582037http://www.walmart.com/ip/Hamilton-Beach-33249-4-qt.-Stay-or-Go-Slow-Cooker/21898512124.974.9729.94WAL-21898512#1453535198179200AMAZON_USB005GZ2NDQ0
4582039http://www.homedepot.com/p/Stanley-3-in-1-Rolling-Workshop-STST18613/203367137136.975.9942.96HDP-STST18613#1453535199908900AMAZON_USB009CP4FAK0
5582041http://www.homedepot.com/p/BLACK-DECKER-20-Volt-Max-Lithium-Drill-and-Project-Kit-LDX120PK/204067339169.99069.99HDP-204067339-1#1453535939112000AMAZON_USB00C625KVE0
6582042http://www.walmart.com/ip/46529370112.994.9717.96WAL-46529370#1453535225132700AMAZON_USB00HJ6O4A00
7582043http://www.walmart.com/ip/Dorcy-41-4751-Dorcy-41-4751-180-Lumen-LED-Cyber-Light-Flashlight-Green/27434943114.994.9719.96WAL-27434943#1453535214556900AMAZON_USB008IHDTWS0
8582044http://www.walmart.com/ip/293846830011.8911.89WAL-29384683#145353521673700AMAZON_USB00DXRHQK40
9582046http://www.walmart.com/ip/15409071129.994.9734.96WAL-15409071#1453535202357400AMAZON_USB00336TY0K0
Sheet1



Excel 2012
ABC
1SKUCOSTCURRENCY
2WAL-1581920386USD
3WAL-2189851229.94USD
4HDP-STST1861342.96USD
5HDP-204067339-169.99USD
6WAL-4652937017.96USD
7WAL-2743494319.96USD
8WAL-2938468311.89USD
9WAL-1540907134.96USD
10^^ (From Sheet 1) ^^^^ Auto-populated ^^
11
Sheet2
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi Censo,

Code:
Sub copyPasteData()

Dim copySheet As Worksheet, pasteSheet As Worksheet
Dim lastRow As Long, startRow As Long


Set copySheet = Worksheets("Sheet1")
Set pasteSheet = Worksheets("Sheet2")


lastRow = WorksheetFunction.CountA(copySheet.Range("G:G"))


copySheet.Range("G2:H" & lastRow).Copy


startRow = WorksheetFunction.CountA(pasteSheet.Range("A:A"))


If startRow = 1 Then
    startRow = 2
Else
    startRow = startRow + 1
End If


pasteSheet.Range("A" & startRow).PasteSpecial xlPasteValues


lastRow = WorksheetFunction.CountA(pasteSheet.Range("A:A"))


pasteSheet.Range("C" & startRow & ":" & "C" & lastRow).Value = "USD"


End Sub

The sheet names in quotes "Sheet1" etc.. need to be changed if you rename the sheets. I hope this is useful, please let me know!
 
Upvote 0
Here's another option, it assumes you have the headers in place on both sheets.
Code:
Sub censo()
    With Worksheets("Sheet1").Range("A1").CurrentRegion
        .Offset(1).Columns("H").Copy Worksheets("Sheet2").Range("A2")
        .Offset(1).Columns("G").Copy Worksheets("Sheet2").Range("B2")
    End With

    With Worksheets("Sheet2").Range("B2").CurrentRegion.Columns("B")
        .Offset(1, 1).Resize(.Rows.Count - 1, 1).Value = "USD"
    End With
End Sub
 
Last edited:
Upvote 0
This is great info guys - thanks. Favor to ask though - I have no experience how to use or implement said code. Please provide a primer?
 
Upvote 0
1) Press Alt + F11

2) In the new window that pops up choose the Insert menu and from the drop down choose Module.

3) Copy and paste the code into the white window that came up when you chose module.

4) Press Alt + q to get back to Excel.

5) If you have Excel 2007 or later save the file as a type .xlsm macro enabled workbook.

Make sure you enable macros when you are prompted.

Go into the list of macros, for me I have 2010 and I go to the developer tab and choose macros then I pick it from the list and choose run.

The name of the macro will be the part immediately after the word Sub in the code.

For my code the macro is named censo.
 
Last edited:
Upvote 0
1) Press Alt + F11

2) In the new window that pops up choose the Insert menu and from the drop down choose Module.

3) Copy and paste the code into the white window that came up when you chose module.

4) Press Alt + q to get back to Excel.

5) If you have Excel 2007 or later save the file as a type .xlsm macro enabled workbook.

Make sure you enable macros when you are prompted.

Go into the list of macros, for me I have 2010 and I go to the developer tab and choose macros then I pick it from the list and choose run.

The name of the macro will be the part immediately after the word Sub in the code.

For my code the macro is named censo.


skywriter, your solution works like a charm. Thank you kindly.

Perhaps you can assist me tweak the results a little. After the macro is ran, is it possible to switch view to (or make active) the sheet the data has been copied to (Sheet2)? If so, what additional code would need to be entered?

For clarification, whether I have 10 rows or 10,000 rows, your code will include everything down to the last row of data right?
 
Upvote 0
Hi Censo,

Code:
Sub copyPasteData()

Dim copySheet As Worksheet, pasteSheet As Worksheet
Dim lastRow As Long, startRow As Long


Set copySheet = Worksheets("Sheet1")
Set pasteSheet = Worksheets("Sheet2")


lastRow = WorksheetFunction.CountA(copySheet.Range("G:G"))


copySheet.Range("G2:H" & lastRow).Copy


startRow = WorksheetFunction.CountA(pasteSheet.Range("A:A"))


If startRow = 1 Then
    startRow = 2
Else
    startRow = startRow + 1
End If


pasteSheet.Range("A" & startRow).PasteSpecial xlPasteValues


lastRow = WorksheetFunction.CountA(pasteSheet.Range("A:A"))


pasteSheet.Range("C" & startRow & ":" & "C" & lastRow).Value = "USD"


End Sub

The sheet names in quotes "Sheet1" etc.. need to be changed if you rename the sheets. I hope this is useful, please let me know!

Thanks for your reply, Webbarr
 
Upvote 0
As long as you have data in A1 and there's no completely blank rows in your data between the first and last row you will be okay.

In the examples I provided, the first example is okay because there's an item in row 4, but the second example would be a problem.

Blank columns would not be an issue as long as there are headers in row 1.

Excel Workbook
ABC
1HeaderHeaderHeader
2testtesttest
3testtesttest
4test
5testtesttest
6testtesttest
Sheet1


Excel Workbook
ABC
1HeaderHeaderHeader
2testtesttest
3testtesttest
4
5testtesttest
6testtesttest
Sheet1
 
Upvote 0
As long as you have data in A1 and there's no completely blank rows in your data between the first and last row you will be okay.

In the examples I provided, the first example is okay because there's an item in row 4, but the second example would be a problem.

Blank columns would not be an issue as long as there are headers in row 1.

Ok, thanks for clarifying. Were you able to figure out how to make Sheet2 active in the original code you provided?
 
Upvote 0

Forum statistics

Threads
1,214,791
Messages
6,121,611
Members
449,038
Latest member
apwr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top