I have to prepare a macro for tickets. I have workbook (Test100). User open this workbook. If in column F in this workbook, user gonna change value (doen's matter what is the change, it can be just typping "x" inside the cell), macro should:
a) open new workbook (Test02) in specified location (always the same),
b) copy values from 2 cells in specified columns (A & B) from Test100 into FIRST BLANK row in specified columns (also A & B) in opened workbook - Test02
Test100 - from this I want to copy data
Test02 - here I want to paste the data
c) The point is to copy values from columns A & B but also from the ROW number in where somebody change the value in column F. Sometimes the mentioned values will be in row 3, another time in row 300 - but still in the same columns.
I wrote the code which works for points a) and b), but I have no idea what should I do with point c Could you help me?
Below you can find the code:
a) open new workbook (Test02) in specified location (always the same),
b) copy values from 2 cells in specified columns (A & B) from Test100 into FIRST BLANK row in specified columns (also A & B) in opened workbook - Test02
Test100 - from this I want to copy data
Test02 - here I want to paste the data
c) The point is to copy values from columns A & B but also from the ROW number in where somebody change the value in column F. Sometimes the mentioned values will be in row 3, another time in row 300 - but still in the same columns.
I wrote the code which works for points a) and b), but I have no idea what should I do with point c Could you help me?
Below you can find the code:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Open new workbook (copy destination) after summoning it by change data in column F
If Not Intersect(Target, Range("F:F")) Is Nothing Then
Workbooks.Open "HERE IS THE DESTINATION OF DESTINATED WORKBOOK"
Dim wsCopy As Worksheet, wsDest As Worksheet
Dim ACopyLastRow As Long, ADestLastRow As Long, BCopyLastRow As Long, BDestLastRow As Long
'Set variables for copy and destination sheets
Set wsCopy = Workbooks("Test100").Worksheets("Arkusz1")
Set wsDest = Workbooks("Test02").Worksheets("Arkusz1")
'1. Find last used row in the copy range based on data in column A
ACopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, 1).End(xlUp).Row
'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
ADestLastRow = wsDest.Cells(wsDest.Rows.Count, 4).End(xlUp).Offset(1, 0).Row
'3. Copy & Paste Data
wsCopy.Range("A" & ACopyLastRow).Copy _
wsDest.Range("A" & ADestLastRow)
'4. Find last used row in the copy range based on data in column B
BCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, 1).End(xlUp).Row
'5. Find first blank row in the destination range based on data in column B
'Offset property moves down 1 row
BDestLastRow = wsDest.Cells(wsDest.Rows.Count, 4).End(xlUp).Offset(1, 0).Row
'6. Copy & Paste Data
wsCopy.Range("B" & BCopyLastRow).Copy _
wsDest.Range("B" & BDestLastRow)
End If
End Sub