VBA: Copy + paste from workbook to another and rearrange columns into rows

lunatu

Board Regular
Joined
Feb 5, 2021
Messages
77
Office Version
  1. 2010
Platform
  1. Windows
  2. Web
Hi,
Im having an situation where I dont where to start from, hopefully getting some help from here :)
I have workbook 1 where I have one row per customer and products listed in columns and values in a row. I need to copy all the products with values based on the status (=won) into another workbook and rearrange the columns into rows, so there might be multiple rows per customer. Below pictures to help understand what Im looking for:

Workbook 1

1639737247878.png


Workbook 2

1639737356412.png
 

Attachments

  • 1639737275274.png
    1639737275274.png
    8 KB · Views: 9

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi Lunatu,

Use below code:

VBA Code:
Option Explicit

Sub copyData()
Dim wb As New Workbook, rowToCopy As Integer
Dim lRow As Integer, nRow As Integer, rowno As Integer, colno As Integer

'Create workbook with name ProductData in which data will be copied. Keep the file open.

Set wb = Workbooks("ProductData.xlsx")
lRow = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

nRow = wb.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row + 1

rowToCopy = nRow

For rowno = 2 To lRow
    If ThisWorkbook.Sheets("Sheet1").Range("H" & rowno) = "won" Then
        For colno = 2 To 7
            If ThisWorkbook.Sheets("Sheet1").Cells(rowno, colno) <> "" Then
                ThisWorkbook.Sheets("Sheet1").Range("A" & rowno).Copy wb.Sheets("Sheet1").Range("A" & rowToCopy) 'To copy customer name
                ThisWorkbook.Sheets("Sheet1").Cells(1, colno).Copy wb.Sheets("Sheet1").Range("B" & rowToCopy) 'To copy product name
                ThisWorkbook.Sheets("Sheet1").Cells(rowno, colno).Copy wb.Sheets("Sheet1").Range("C" & rowToCopy) 'To copy product value
                rowToCopy = rowToCopy + 1
            End If
        Next
    End If
Next
End Sub
 
Upvote 0
Solution
Hi!

Still one question: I added formulas in workbook 1 columns B to C, how it will copy only values not formulas?
 
Upvote 0
Hi Lunatu,

Use below code:

VBA Code:
Option Explicit

Sub copyData()
Dim wb As New Workbook, rowToCopy As Integer
Dim lRow As Integer, nRow As Integer, rowno As Integer, colno As Integer

'Create workbook with name ProductData in which data will be copied. Keep the file open.

Set wb = Workbooks("ProductData.xlsx")
lRow = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

nRow = wb.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row + 1

rowToCopy = nRow

For rowno = 2 To lRow
    If ThisWorkbook.Sheets("Sheet1").Range("H" & rowno) = "won" Then
        For colno = 2 To 7
            If ThisWorkbook.Sheets("Sheet1").Cells(rowno, colno) <> "" Then
                ThisWorkbook.Sheets("Sheet1").Range("A" & rowno).Copy wb.Sheets("Sheet1").Range("A" & rowToCopy) 'To copy customer name
                ThisWorkbook.Sheets("Sheet1").Cells(1, colno).Copy wb.Sheets("Sheet1").Range("B" & rowToCopy) 'To copy product name
                ThisWorkbook.Sheets("Sheet1").Cells(rowno, colno).Copy wb.Sheets("Sheet1").Range("C" & rowToCopy) 'To copy product value
                rowToCopy = rowToCopy + 1
            End If
        Next
    End If
Next
End Sub
Hi,

still few question if you could help me. I modified the code a little bit:
- column H can be either "won" or "part-wont", the code should copy if either of them are in the cell
- in column I i have added date and I want to copy all rows from yesterday --> current date - 1
- there must be text in column A
- I added formulas in columns B-G and I want to paste the values not the formulas.

The code I modified is not working:

Sub copyData()
Dim wb As New Workbook, rowToCopy As Integer
Dim lRow As Integer, nRow As Integer, rowno As Integer, colno As Integer

Set wb = Workbooks("ProductData.xlsx")
lRow = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

nRow = wb.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row + 1

rowToCopy = nRow

For rowno = 2 To lRow
If ThisWorkbook.Sheets("Sheet1").Range("H" & rowno) = "won" Or ThisWorkbook.Sheets("Sheet1").Range("Q" & rowno) = "part-won" And ThisWorkbook.Sheets("Sheet1").Range("I" & rowno) = Date - 1 And ThisWorkbook.Sheets("Sheet1").Range("A" & rowno) > 0 Then
For colno = 2 To 7
If ThisWorkbook.Sheets("Sheet1").Cells(rowno, colno) <> "" Then
ThisWorkbook.Sheets("Sheet1").Range("A" & rowno).Copy wb.Sheets("Sheet1").Range("A" & rowToCopy)
ThisWorkbook.Sheets("Sheet1").Cells(1, colno).Copy wb.Sheets("Sheet1").Range("B" & rowToCopy)
ThisWorkbook.Sheets("Sheet1").Cells(rowno, colno).Copy wb.Sheets("Sheet1").Range("C" & rowToCopy)
rowToCopy = rowToCopy + 1
End If
Next
End If
Next
End Sub
 
Upvote 0
Hi!

Still one question: I added formulas in workbook 1 columns B to C, how it will copy only values not formulas?


Hi,

To paste only values use below:

Excel Formula:
ThisWorkbook.Sheets("Sheet1").Cells(rowno, colno).Copy
wb.Sheets("Sheet1").Range("C" & rowToCopy).PasteSpecial xlPasteValues
 
Upvote 0
Hi Lunatu,

Check below modified code:

VBA Code:
Dim wb As New Workbook, rowToCopy As Integer
Dim lRow As Integer, nRow As Integer, rowno As Integer, colno As Integer

'Create workbook with name ProductData in which data will be copied. Keep the file open.

Set wb = Workbooks("ProductData.xlsx")
lRow = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

nRow = wb.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row + 1

rowToCopy = nRow

Application.ScreenUpdating = False
With ThisWorkbook
    For rowno = 2 To lRow
        If (.Sheets("Sheet1").Range("H" & rowno) = "won" Or .Sheets("Sheet1").Range("H" & rowno) = "part-won") _
        And .Sheets("Sheet1").Range("A" & rowno) <> vbNullString And .Sheets("Sheet1").Range("I" & rowno) >= Date - 1 Then
            For colno = 2 To 7
                If .Sheets("Sheet1").Cells(rowno, colno) <> "" Then
                    .Sheets("Sheet1").Range("A" & rowno).Copy wb.Sheets("Sheet1").Range("A" & rowToCopy) 'To copy customer name
                    .Sheets("Sheet1").Cells(1, colno).Copy wb.Sheets("Sheet1").Range("B" & rowToCopy) 'To copy product name
                    .Sheets("Sheet1").Cells(rowno, colno).Copy
                    wb.Sheets("Sheet1").Range("C" & rowToCopy).PasteSpecial xlPasteValues 'To copy product value as values only
                    rowToCopy = rowToCopy + 1
                End If
            Next
        End If
    Next
End With
Application.ScreenUpdating = True
Application.CutCopyMode = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,691
Members
449,117
Latest member
Aaagu

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