1
votes

I need to copy tan color cells from one workbook and paste into another workbook. and need to take only specific cells values in that excel. I achieved that, but only able to paste into another sheet in same workbook. Can you please help me in pasting the data to another workbook on a specific sheet and that too the values should be pasted in second row,(i.e starting from second row) as first row has title in it.

Source Table Titles:

Project | Phase | Status | st Dt | End Dt | Pre | resource | Remark | Comments

Dest Table Title:

Project | Phase | st Dt | End Dt | resource |

Existing Code:

Option Explicit
    Sub CopyRowsGroup()
    Dim wks As Worksheet
    Dim wNew As Worksheet
    'Dim y As Workbook

    Dim lRow As Long
    Dim lNewRow As Long
    Dim x As Long
    Dim ptr As Long

     Set wks = ActiveSheet
        lRow = wks.Cells.SpecialCells(xlCellTypeLastCell).Row
        Set wNew = Worksheets.Add

    'Set y = Workbooks.Open("C:\Users\1519728\Desktop\Capacity Planning Tracker-ver1.0.xlsx")
    'Workbooks.Open("C:\Users\1519728\Desktop\Capacity Planning Tracker-ver1.0.xlsx").Activate
    'Set wNew = y.Sheets("Data dump")
        lNewRow = 1
        For x = 1 To lRow
            If wks.Cells(x, 1).Interior.Color = RGB(221, 217, 195) Then
              wks.Cells(x, 1).EntireRow.Copy
              wNew.Cells(lNewRow, 1).PasteSpecial Paste:=xlPasteValues
              lNewRow = lNewRow + 1
            End If
        Next

        wNew.Rows([1]).EntireRow.Delete
        wNew.Columns([3]).EntireColumn.Delete
        wNew.Columns([3]).EntireColumn.Delete
        wNew.Columns([5]).EntireColumn.Delete
        wNew.Columns([6]).EntireColumn.Delete
        wNew.Columns([6]).EntireColumn.Delete
        wNew.Columns([6]).EntireColumn.Delete

        For ptr = 2 To lNewRow - 2
            If Cells(ptr, "A") = vbNullString Then
              Cells(ptr, "A") = Cells(ptr, "A").Offset(-1, 0)
            End If
        Next

End Sub
1
Will your second workbook be an existing workbook that you will open or a new one created via the macro?tittaenälg
I believe I answered my own question while working through the problemtittaenälg

1 Answers

0
votes

You are really close to having it do what you want. The fatal flaw is where you opened the destination file a second time while trying to activate it, which erased the y variable you had assigned the line above it. There is no need to make your destination file active, but if for whatever reason you really want it to become active I included a line that would let it work.

Apart from that I made a few small changes and left comments as to why they were made.

Sub CopyRowsGroup()
Dim wks As Worksheet
Dim wNew As Worksheet
Dim y As Workbook
Dim lRow As Long
Dim lNewRow As Long
Dim x As Long
Dim ptr As Long

Set wks = ActiveSheet
lRow = wks.Cells.SpecialCells(xlCellTypeLastCell).Row
'Set wNew = Worksheets.Add 'commented out since we're using the destination file as the paste location

Set y = Workbooks.Open("C:\Users\1519728\Desktop\Capacity Planning Tracker-ver1.0.xlsx")
'The line below is what was causing your problems. You opened the workbook again and erased your y variable
'Workbooks.Open("C:\Users\1519728\Desktop\Capacity Planning Tracker-ver1.0.xlsx").Activate   'you don't need to activate a workbook after opening it
'If you really want to make the workbook active, simply use the line below
'y.Activate
Set wNew = y.Sheets("Data dump")

'Copy the rest of your data over
    lNewRow = 2 'Changed to 2 to accomodate the header in row 1
    For x = 1 To lRow
        If wks.Cells(x, 1).Interior.Color = RGB(221, 217, 195) Then
          wks.Cells(x, 1).EntireRow.Copy
          wNew.Cells(lNewRow, 1).PasteSpecial Paste:=xlPasteValues
          lNewRow = lNewRow + 1
        End If
    Next

    'wNew.Rows([1]).EntireRow.Delete   'This was deleting the header column which I am assuming was already in the sheet based on your request for the data to begin being copied to row 2
    wNew.Columns([3]).EntireColumn.Delete
    'wNew.Columns([3]).EntireColumn.Delete   'this was deleting the end dt column, which you listed as one of the columns you wanted to keep
    wNew.Columns([5]).EntireColumn.Delete
    wNew.Columns([6]).EntireColumn.Delete
    wNew.Columns([6]).EntireColumn.Delete
    'wNew.Columns([6]).EntireColumn.Delete   'not deleting anything so we don't need it

    For ptr = 2 To lNewRow - 2
        If Cells(ptr, "A") = vbNullString Then
          Cells(ptr, "A") = Cells(ptr, "A").Offset(-1, 0)
        End If
    Next

End Sub