1

When a cell (A1) value is changed, it will be copied to another sheet (sheet3) in particular column (c)each time creating new cell. the code works fine if cell value in A1 is manually entered, but here i have formula /paste link in A1. and with this formula, its not automatically updating. Another challenge is I wanted to copied to sheet1 in another excel(Amount) kept in same folder.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = Range("A1").Address Then
    ' Get the last row on our destination sheet (using Sheet2, col A here)...
    Dim intLastRow As Long
    intLastRow = Sheet3.Cells(Sheet3.Rows.Count, "C").End(xlUp).Row
    ' Add our value to the next row...
    Sheet3.Cells(intLastRow + 1, "C") = Target.Value
End If

End Sub
2
  • It's looks like easier but not getting clicked,,,quite interesting too, if U select cell A1 press F2 and Enter then it works ,,, I've tried SEND KEY also but not working ,,, put this question on StackOverflow !! Commented Apr 3, 2021 at 9:57
  • ,, check my post below I've solved the issue !! Commented Apr 4, 2021 at 5:38

2 Answers 2

0

::Caveat::

The Worksheet Change event works when content in a cell changes, and while the Worksheet Selection Change event works whenever a new cell is selected.

In your case whenever you either enter data in Cell A1 or recalculate it, the Macro copies every new value to another sheet in new row.

But when Cell A1 is linked with formula it doesn't works, the reason is in that case neither Worksheet Change or Selection Change works.

Therefore I would like to suggest Worksheet Calculate event, because it triggers Macro whenever the Sheet calculates any formula, as happens in your case, since Cell A1 is linked with formula cell.

Private Sub CalCopy()

 Workbooks.Open "C:\Yourfolder\satyam.xlsx"
 a = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row + 1
 Sheets("Sheet1").Range("A" & a).Value = Workbooks("1.xlsm").Worksheets("Sheet1").Range("A1").Value

  ActiveWorkbook.Save
  Workbooks("satyam.xlsx").Close

 End Sub

Private Sub Worksheet_Calculate()

    If Range("A1").Value = Range("H1") Then
      
       Call CalCopy
     End If
    
    End Sub

How it works:

  • Cell A1 is linked with the Cell H1, has a formula.
  • As soon you change data in source cells linked with the formula in Cell H1, Excel gets new values in both Cell H1 & A1.
  • And then the Worksheet Calculate event triggers the Macro CalCopy, copies value from Cell A1 into Sheet1 in another Workbook as new row/record .

N.B.

  • Use both the VBA macros as Standard Module.
  • Save the Workbook as Macro Enabled 1.xlsm.
  • You need to change File path as needed.
  • You may adjust cell references in the code as needed
21
  • Thanks Rajesh, it worked fine . Only one thing, I stuck as mentioned in my initial question. Commented Apr 4, 2021 at 15:31
  • If sheet1 and sheet2 are in one excel , it worked fine. But here sheet1 is in one excel and sheet2 is another excel e.g. "satyam" ,and its .xlsx format.so what to put at the place of "sheet2"? How to give the path? Commented Apr 4, 2021 at 15:35
  • Hi @user3287723 ,,, glad to help you,,, my solution addresses your core issue,,, why your code was not working I've already explained in CAVEAT section,,,! Now about your another issue that how to use shown VBA code with Workbook is CLOSED, What U have to do is add this code row in Private Sub CalCopy() at 1st row Workbooks.Open "C:/myfolder/satyam.xlsx" (adjust Fie path as your need), and these 2 row at 4 & 5th , ActiveWorkbook.Save, Workbooks("satyam.xlsx").Close, now one more thing since my suggested method is working for you so you may mark it as ANSWER and well Up Vote also ☺ Commented Apr 5, 2021 at 4:23
  • Hi Rajesh I tried as per advise following code Private Sub CalCopy() Workbooks.Open "C:/Users/satya/OneDrive/Desktop/satyam.xlsx" a = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row + 1 Sheets("Sheet1").Range("A" & a).Value = Sheets("Sheet4").Range("A1").Value ActiveWorkbook.Save Workbooks("satyam.xlsx").Close End Sub Private Sub Worksheet_Calculate() If Range("A1").Value = Range("F1") Then Call CalCopy End If End Sub Commented Apr 5, 2021 at 15:44
  • even with above code, when open the coded excel, it also opens another excel- satyam.xlsx and showing the above error msg Commented Apr 5, 2021 at 16:15
0

Try using the Change event rather than SelectionChange.

1
  • I used the change event but it didnt worked Commented Apr 4, 2021 at 17:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .