2

I have a question about the VBA code I wrote below.

First of all, a brief summary of what I try to achieve: I have 2 Excel sheets, sheet1 and sheet2. The sheet1 has the data in B:AW and the sheet2 has a data in the M:W. I wanted to create a VBA code that copies the data in B:I and M:O starting from 7th row from sheet1 to B:K of sheet2 starting from again 7th row by also keeping the formatting same. I also set the last row to be copied as the row above the one includes "SGRE" in column B in sheet1. So for instance, lets assume that B1200 has "SGRE" in the cell so the code takes the data in B7:I1199 and M7:O1199 from sheet 1 and copies to sheet2 B7:K1199.

As a next step, I also wanted to capture all the updates in copied columns of sheet1 in sheet2 automatically. The code, apparently, does the job and every time I press run, it captures the update. I will probably assign a button to let the users update sheet2 according to the updates sheet1.

But I have 2 problems:

  1. When I add a new row in sheet1 and run the code, the new row in sheet2 is added for only columns B:K. However, as I said above, I have more data in M:W which is not depending on any data in sheet1 (fixed and manually changed). So when I add a new row in sheet1, I want this row to be added in sheet2 not only for B:K but also M:W (which means a new row for B:W).
  2. When I add a new row in sheet1, run the code, delete the row in sheet1 and run the code, it does not move the last row up in sheet2, instead keeps the last row in sheet2 and duplicates this row above. Lets assume our last row is 1199 and B1199 is "AAA", so when I add a new row in sheet1, run the code, B1199 moves to B1200 as expected but when I delete the row in sheet1 and run the code, B1200 stays as "AAA" and B1199 becomes "AAA" too.
Sub CopyPartialDataToSheet2()
    'Declare variables
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim rng1 As Range
    Dim rng2 As Range

    'Set worksheets
    Set ws1 = ThisWorkbook.Sheets("Sheet1")
    Set ws2 = ThisWorkbook.Sheets("Sheet2")


    'Find last row in Sheet1 and Sheet2
    lastRow1 = ws1.Cells(ws1.Rows.Count, "B").End(xlUp).Row
    lastRow2 = ws2.Cells(ws2.Rows.Count, "B").End(xlUp).Row

    'Find last row to be copied in Sheet1
    Do While ws1.Cells(lastRow1, "B").Value <> "SGRE"
        lastRow1 = lastRow1 - 1
    Loop
    lastRow1 = lastRow1 - 1

    'Print debug information
    Debug.Print "lastRow1: " & lastRow1 & ", lastRow2: " & lastRow2
    
    'Set ranges to be copied
    Set rng1 = ws1.Range("B7:I" & lastRow1)
    Set rng2 = ws1.Range("M7:O" & lastRow1)

    'Copy ranges from Sheet1 to Sheet2
    rng1.Copy ws2.Range("B7")
    rng2.Copy ws2.Range("B7").Offset(0, rng1.Columns.Count)

    'Adjust column widths in Sheet2 to match those in Sheet1
    For i = 1 To rng1.Columns.Count
        ws2.Columns(i).ColumnWidth = ws1.Columns(i).ColumnWidth
    Next i
    For i = 1 To rng2.Columns.Count
        ws2.Columns(i + rng1.Columns.Count).ColumnWidth = ws1.Columns(i + rng1.Columns.Count).ColumnWidth
    Next i
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    'Declare variables
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim rng1 As Range
    Dim rng2 As Range
    Dim rng3 As Range
    Dim rngHeights As Range
    Dim lastRow1 As Long
    Dim lastRow2 As Long
    Dim i As Long

    'Exit the sub if the change was not made in Sheet1
    If Target.Parent.Name <> "Sheet1" Then Exit Sub

    'Set worksheets
    Set ws1 = ThisWorkbook.Sheets("Sheet1")
    Set ws2 = ThisWorkbook.Sheets("Sheet2")

    'Find last row in Sheet1 and Sheet2 
    lastRow1 = ws1.Cells(ws1.Rows.Count, "B").End(xlUp).Row
    lastRow2 = ws2.Cells(ws2.Rows.Count, "B").End(xlUp).Row

    'Find last row to be copied in Sheet1
    Do While ws1.Cells(lastRow1, "B").Value <> "SGRE"
        lastRow1 = lastRow1 - 1
    Loop
    lastRow1 = lastRow1 - 1

    'Print debug information
    Debug.Print "lastRow1: " & lastRow1 & ", lastRow2: " & lastRow2

    'Set ranges to be copied
    Set rng1 = ws1.Range("B7:K" & lastRow1)
    Set rng2 = ws1.Range("M7:O" & lastRow1)
    Set rng3 = ws1.Range("M7:W" & lastRow1)

    'Copy data and row heights from Sheet1 to Sheet2
    rng1.Copy ws2.Range("B7")
    rng2.Copy ws2.Range("B7").Offset(0, rng1.Columns.Count)
    If lastRow1 > lastRow2 Then
        ws2.Range("B" & lastRow2 + 1 & ":K" & lastRow1).Insert Shift:=xlDown
        rng3.Copy ws2.Range("B" & lastRow2 + 1).Offset(0, rng1.Columns.Count + rng2.Columns.Count)
        Set rngHeights = ws1.Range("B" & lastRow2 + 1 & ":B" & lastRow1)
        For i = 1 To rngHeights.Rows.Count
            ws2.Rows(lastRow2 + i).RowHeight = rngHeights.Rows(i).RowHeight
        Next i
    End If
End Sub

elgre
  • 21
  • 2
  • 2
    Does this answer your question? [How to avoid using Select in Excel VBA](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba) – Dominique Jan 03 '23 at 15:31
  • The description says "the row above the one includes SGRE" but the code is `lastRow1 = lastRow1 - 5` which is 5 rows above ? Description says "takes the data in B7:I1199" but the code is `Set rng1 = ws1.Range("C7:I" & lastRow1)` ? – CDP1802 Jan 03 '23 at 16:07
  • Thanks for the comment. That is probably my bad, as I said I am quite new to VBA so I am certainly open any correction in my code as well as helping me with achieving what I want. Re B7;:I1199, it was just an example to make it a bit more clear. The last row will be dynamic, depending on where the cell included "SGRE" is. In that example, I said I1199 but in other case it might be I1589. Therefore, I tried to make it dynamic, defined lastRow1 and linked it to I, instead of giving a fixed value. – elgre Jan 03 '23 at 16:50
  • Another query - Is SGRE in column B or C ? In one code block you have `ws1.Cells(lastRow1, "C").Value <> "SGRE"` in the other `Do While ws1.Cells(lastRow1, "B").Value <> "SGRE"` – CDP1802 Jan 03 '23 at 16:55
  • Good spot, another typo! It is in column B... – elgre Jan 03 '23 at 17:12
  • Consider [editing your question](https://stackoverflow.com/posts/74995366/edit) to remove these typos and errors – cybernetic.nomad Jan 03 '23 at 17:17
  • Is `ws2` correct for `Set rng3 = ws2.Range("M7:W" & lastRow1)`, should it not be `ws1`? – CDP1802 Jan 03 '23 at 17:30
  • I corrected the typos spotted. But what I am thinking is rng3 might be redundant actually. I tried to define 3rd range to sort out the problems I had as listed above but by having another look, they might be redundant. However, I kept it for now for your info. – elgre Jan 03 '23 at 21:24
  • I'm having difficulty understanding the problem here. Is the added/deleted row above or below the SGRE row ? I think you might need to add some screenshots of the sheet. – CDP1802 Jan 04 '23 at 14:31

0 Answers0