Try is Worksheet_Change Sub. Does it do what you need?
The reason for the question is this; if for example $A$3 has a value of 123 (assuming $A$4 and $A$5 do not have values)
1st time 123 should be written to Dnextrow on the "GLD"
if you re-enter 123 in cell $A$3 on the "g1" this Sub will
not write to sheet "GLD". Same for $A$4 and $A$5
what should happen if you clear $A$3 at this point? the way the code currently works is 123 would remain in Column D on "GLD" because
a cleared (or zero) cell value is not equal to 123.
It think bit more clarification of your logic is needed.
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim wsDestination As Worksheet
Dim NextRow As Long
Dim idx As Integer
Dim addrs, outputs
Dim col
addrs = Array("$A$3", "$A$4", "$A$5")
outputs = Array("D", "G", "J")
Set wsDestination = Sheets("GLD")
With WorksheetFunction
'look for an address match in the acceptable range (defined by addrs array)
idx = IIf(.IsNA(.Match(Target.Address, addrs, 0)), 0, .Match(Target.Address, addrs, 0))
If idx = 0 Then Exit Sub 'itarget.address in not in acceptable range exit
'get the destination column for the source address
col = .Index(outputs, idx)
End With
With wsDestination
'calculate the next row
NextRow = .Cells(Rows.Count, col).End(xlUp).Row + 1
'if source value is the same as the value entered in the column
If .Range(col & NextRow - 1).Value = Target.Value Then Exit Sub
.Range(col & NextRow).Value = Target.Value
End With
End Sub
Thanks a lot for your help. You wrote an excellent macro. It is short and fast, with sometimes annoying, but useful error message box.
First I like to answer your questions:
(a) Cell values fo A3, A4 and A5 on Sheet g1 are automatically updated by the excel query, so they change every day in different times.
(b) Becuase each one of them are updated in different times, I wanted the way you did it in your macro: It copies the value to the destination cells, whenever change occurs; (c) Also, I didnot want the macro copy the same value more than ones to the last empty cell on the destination sheet (values are hardly ever same in consequative days), when the excel query refreshes with no change in value. Your present macro solved this problem, too. However, the major problem that I couldnot explain clearly before still remains unsolved.
HERE IS A DETAILED EXPLANATION OF THE MACRO PROBLEM THAT NEEDS SOLUTION:
I place the macro in sheet1 (not in thisworkbook or another place) (Is it ok?)
All my data are numbers with varying decimals.
Sheet1 has my data. It has two columns: Name (A column) and Decimal Number (B column). (I previously wrote wrong as A3, A4 and A5).
Sheet1 data values are updated by excel workboook query automatically.
But, your macro gives the following message when I try to refresh the excel workbook query:
"unable to get match property of the WorksheetFunction Class".
However, it completes updating the data in the sheet1, when I press "end" selection in the message box.
Namely, the excel query-update places the old values with new ones on the Sheet1.
REGARDING THE PRESENT MACRO: The present macro does not copy the updated data values in the sheet1
to the destination sheet unless I write them by hand (I have many sheets of different data). So, the macro needs revision so that it copies the new values on the source cells to the destination cells (without entering by hand).
Thank you for your help again.