Same macro on two different worksheets - works on one, aborts on the other
I have a macro that I have tested on a development workbook. This macro runs successfully and provides the expected results. When I copy this macro to the production version of the workbook, the macro aborts for different reasons. When I step through the macro on debug, the code does nor execute as expected. For example, in the production version, the IF statement testing the date executes, followed by the first .Cells statement then the macro aborts.
No clue on why. Any ideas would be appreciated.
Private Sub Worksheet_Calculate()
Dim Cost_Per_day
Dim COST_kg
Dim AVG_SALES_PRICE
Dim COST_NET_PURCHASE
Dim PROFIT_GROSS
Dim PROFIT_NET
Dim PROFIT_NET_X
Dim Flag_set
Dim dtmTime As Date
Dim Rw As Long
'If Critical Cells change, move contents to Log sheet
Dim Xrg As Range
Set Xrg = Range("E5:I11")
If Not Intersect(Xrg, Range("E5:I11 ")) Is Nothing Then
dtmTime = Now()
Cost_day = Worksheets("FEED_ANALYSIS").Range("E7").Value
COST_kg = Worksheets("FEED_ANALYSIS").Range("F7").Value
AVG_SALES_PRICE = Worksheets("FEED_ANALYSIS").Range("I5").Value
COST_NET_PURCHASE = Worksheets("FEED_ANALYSIS").Range("G11").Value
PROFIT_GROSS = Worksheets("FEED_ANALYSIS").Range("I7").Value
PROFIT_NET = Worksheets("FEED_ANALYSIS").Range("I8").Value
PROFIT_NET_X = Worksheets("FEED_ANALYSIS").Range("I9").Value
Rw = Sheets("LOG").Range("A" & Rows.Count).End(xlUp).Row + 1
With Sheets("LOG")
datcomp = .Cells(Rw - 1, 1)
' if the previous entry date is the same as the current date, do not create the entries... one entry per day`
If Year(datcomp) = Year(dtmTime) And Month(datcomp) = Month(dtmTime) And Day(datcomp) = Day(dtmTime) Then GoTo NoUpd
.Cells(Rw, 1) = dtmTime
.Cells(Rw, 2) = Cost_Per_day
.Cells(Rw, 3) = COST_kg
.Cells(Rw, 4) = AVG_SALES_PRICE
.Cells(Rw, 5) = COST_NET_PURCHASE
.Cells(Rw, 6) = PROFIT_GROSS
.Cells(Rw, 7) = PROFIT_NET
.Cells(Rw, 8) = PROFIT_NET_X
.Cells(Rw, 11) = .Cells(Rw - 1, 1)
NoUpd:
End With
End If
End Sub`
excel vba
add a comment |
I have a macro that I have tested on a development workbook. This macro runs successfully and provides the expected results. When I copy this macro to the production version of the workbook, the macro aborts for different reasons. When I step through the macro on debug, the code does nor execute as expected. For example, in the production version, the IF statement testing the date executes, followed by the first .Cells statement then the macro aborts.
No clue on why. Any ideas would be appreciated.
Private Sub Worksheet_Calculate()
Dim Cost_Per_day
Dim COST_kg
Dim AVG_SALES_PRICE
Dim COST_NET_PURCHASE
Dim PROFIT_GROSS
Dim PROFIT_NET
Dim PROFIT_NET_X
Dim Flag_set
Dim dtmTime As Date
Dim Rw As Long
'If Critical Cells change, move contents to Log sheet
Dim Xrg As Range
Set Xrg = Range("E5:I11")
If Not Intersect(Xrg, Range("E5:I11 ")) Is Nothing Then
dtmTime = Now()
Cost_day = Worksheets("FEED_ANALYSIS").Range("E7").Value
COST_kg = Worksheets("FEED_ANALYSIS").Range("F7").Value
AVG_SALES_PRICE = Worksheets("FEED_ANALYSIS").Range("I5").Value
COST_NET_PURCHASE = Worksheets("FEED_ANALYSIS").Range("G11").Value
PROFIT_GROSS = Worksheets("FEED_ANALYSIS").Range("I7").Value
PROFIT_NET = Worksheets("FEED_ANALYSIS").Range("I8").Value
PROFIT_NET_X = Worksheets("FEED_ANALYSIS").Range("I9").Value
Rw = Sheets("LOG").Range("A" & Rows.Count).End(xlUp).Row + 1
With Sheets("LOG")
datcomp = .Cells(Rw - 1, 1)
' if the previous entry date is the same as the current date, do not create the entries... one entry per day`
If Year(datcomp) = Year(dtmTime) And Month(datcomp) = Month(dtmTime) And Day(datcomp) = Day(dtmTime) Then GoTo NoUpd
.Cells(Rw, 1) = dtmTime
.Cells(Rw, 2) = Cost_Per_day
.Cells(Rw, 3) = COST_kg
.Cells(Rw, 4) = AVG_SALES_PRICE
.Cells(Rw, 5) = COST_NET_PURCHASE
.Cells(Rw, 6) = PROFIT_GROSS
.Cells(Rw, 7) = PROFIT_NET
.Cells(Rw, 8) = PROFIT_NET_X
.Cells(Rw, 11) = .Cells(Rw - 1, 1)
NoUpd:
End With
End If
End Sub`
excel vba
Please share your data.
– Michal Rosa
Nov 24 '18 at 1:33
Welcome to SO. I think it will fire always on the calculation event of the worksheet.If Not Intersect(Xrg, Range("E5:I11 ")) Is Nothing Then
will be always true. From the code it may be assumed .that it is intended to fire fromWorksheets("FEED_ANALYSIS")
and the data will be feed into the range E5:I11. If this assumption is true it is better to use Change event ofWorksheets("FEED_ANALYSIS")
– Ahmed AU
Nov 24 '18 at 1:43
add a comment |
I have a macro that I have tested on a development workbook. This macro runs successfully and provides the expected results. When I copy this macro to the production version of the workbook, the macro aborts for different reasons. When I step through the macro on debug, the code does nor execute as expected. For example, in the production version, the IF statement testing the date executes, followed by the first .Cells statement then the macro aborts.
No clue on why. Any ideas would be appreciated.
Private Sub Worksheet_Calculate()
Dim Cost_Per_day
Dim COST_kg
Dim AVG_SALES_PRICE
Dim COST_NET_PURCHASE
Dim PROFIT_GROSS
Dim PROFIT_NET
Dim PROFIT_NET_X
Dim Flag_set
Dim dtmTime As Date
Dim Rw As Long
'If Critical Cells change, move contents to Log sheet
Dim Xrg As Range
Set Xrg = Range("E5:I11")
If Not Intersect(Xrg, Range("E5:I11 ")) Is Nothing Then
dtmTime = Now()
Cost_day = Worksheets("FEED_ANALYSIS").Range("E7").Value
COST_kg = Worksheets("FEED_ANALYSIS").Range("F7").Value
AVG_SALES_PRICE = Worksheets("FEED_ANALYSIS").Range("I5").Value
COST_NET_PURCHASE = Worksheets("FEED_ANALYSIS").Range("G11").Value
PROFIT_GROSS = Worksheets("FEED_ANALYSIS").Range("I7").Value
PROFIT_NET = Worksheets("FEED_ANALYSIS").Range("I8").Value
PROFIT_NET_X = Worksheets("FEED_ANALYSIS").Range("I9").Value
Rw = Sheets("LOG").Range("A" & Rows.Count).End(xlUp).Row + 1
With Sheets("LOG")
datcomp = .Cells(Rw - 1, 1)
' if the previous entry date is the same as the current date, do not create the entries... one entry per day`
If Year(datcomp) = Year(dtmTime) And Month(datcomp) = Month(dtmTime) And Day(datcomp) = Day(dtmTime) Then GoTo NoUpd
.Cells(Rw, 1) = dtmTime
.Cells(Rw, 2) = Cost_Per_day
.Cells(Rw, 3) = COST_kg
.Cells(Rw, 4) = AVG_SALES_PRICE
.Cells(Rw, 5) = COST_NET_PURCHASE
.Cells(Rw, 6) = PROFIT_GROSS
.Cells(Rw, 7) = PROFIT_NET
.Cells(Rw, 8) = PROFIT_NET_X
.Cells(Rw, 11) = .Cells(Rw - 1, 1)
NoUpd:
End With
End If
End Sub`
excel vba
I have a macro that I have tested on a development workbook. This macro runs successfully and provides the expected results. When I copy this macro to the production version of the workbook, the macro aborts for different reasons. When I step through the macro on debug, the code does nor execute as expected. For example, in the production version, the IF statement testing the date executes, followed by the first .Cells statement then the macro aborts.
No clue on why. Any ideas would be appreciated.
Private Sub Worksheet_Calculate()
Dim Cost_Per_day
Dim COST_kg
Dim AVG_SALES_PRICE
Dim COST_NET_PURCHASE
Dim PROFIT_GROSS
Dim PROFIT_NET
Dim PROFIT_NET_X
Dim Flag_set
Dim dtmTime As Date
Dim Rw As Long
'If Critical Cells change, move contents to Log sheet
Dim Xrg As Range
Set Xrg = Range("E5:I11")
If Not Intersect(Xrg, Range("E5:I11 ")) Is Nothing Then
dtmTime = Now()
Cost_day = Worksheets("FEED_ANALYSIS").Range("E7").Value
COST_kg = Worksheets("FEED_ANALYSIS").Range("F7").Value
AVG_SALES_PRICE = Worksheets("FEED_ANALYSIS").Range("I5").Value
COST_NET_PURCHASE = Worksheets("FEED_ANALYSIS").Range("G11").Value
PROFIT_GROSS = Worksheets("FEED_ANALYSIS").Range("I7").Value
PROFIT_NET = Worksheets("FEED_ANALYSIS").Range("I8").Value
PROFIT_NET_X = Worksheets("FEED_ANALYSIS").Range("I9").Value
Rw = Sheets("LOG").Range("A" & Rows.Count).End(xlUp).Row + 1
With Sheets("LOG")
datcomp = .Cells(Rw - 1, 1)
' if the previous entry date is the same as the current date, do not create the entries... one entry per day`
If Year(datcomp) = Year(dtmTime) And Month(datcomp) = Month(dtmTime) And Day(datcomp) = Day(dtmTime) Then GoTo NoUpd
.Cells(Rw, 1) = dtmTime
.Cells(Rw, 2) = Cost_Per_day
.Cells(Rw, 3) = COST_kg
.Cells(Rw, 4) = AVG_SALES_PRICE
.Cells(Rw, 5) = COST_NET_PURCHASE
.Cells(Rw, 6) = PROFIT_GROSS
.Cells(Rw, 7) = PROFIT_NET
.Cells(Rw, 8) = PROFIT_NET_X
.Cells(Rw, 11) = .Cells(Rw - 1, 1)
NoUpd:
End With
End If
End Sub`
excel vba
excel vba
edited Nov 24 '18 at 1:40
K.Dᴀᴠɪs
7,051112239
7,051112239
asked Nov 24 '18 at 1:01
GeorgiaCowsGeorgiaCows
62
62
Please share your data.
– Michal Rosa
Nov 24 '18 at 1:33
Welcome to SO. I think it will fire always on the calculation event of the worksheet.If Not Intersect(Xrg, Range("E5:I11 ")) Is Nothing Then
will be always true. From the code it may be assumed .that it is intended to fire fromWorksheets("FEED_ANALYSIS")
and the data will be feed into the range E5:I11. If this assumption is true it is better to use Change event ofWorksheets("FEED_ANALYSIS")
– Ahmed AU
Nov 24 '18 at 1:43
add a comment |
Please share your data.
– Michal Rosa
Nov 24 '18 at 1:33
Welcome to SO. I think it will fire always on the calculation event of the worksheet.If Not Intersect(Xrg, Range("E5:I11 ")) Is Nothing Then
will be always true. From the code it may be assumed .that it is intended to fire fromWorksheets("FEED_ANALYSIS")
and the data will be feed into the range E5:I11. If this assumption is true it is better to use Change event ofWorksheets("FEED_ANALYSIS")
– Ahmed AU
Nov 24 '18 at 1:43
Please share your data.
– Michal Rosa
Nov 24 '18 at 1:33
Please share your data.
– Michal Rosa
Nov 24 '18 at 1:33
Welcome to SO. I think it will fire always on the calculation event of the worksheet.
If Not Intersect(Xrg, Range("E5:I11 ")) Is Nothing Then
will be always true. From the code it may be assumed .that it is intended to fire from Worksheets("FEED_ANALYSIS")
and the data will be feed into the range E5:I11. If this assumption is true it is better to use Change event of Worksheets("FEED_ANALYSIS")
– Ahmed AU
Nov 24 '18 at 1:43
Welcome to SO. I think it will fire always on the calculation event of the worksheet.
If Not Intersect(Xrg, Range("E5:I11 ")) Is Nothing Then
will be always true. From the code it may be assumed .that it is intended to fire from Worksheets("FEED_ANALYSIS")
and the data will be feed into the range E5:I11. If this assumption is true it is better to use Change event of Worksheets("FEED_ANALYSIS")
– Ahmed AU
Nov 24 '18 at 1:43
add a comment |
1 Answer
1
active
oldest
votes
Assumption: Data is being feed into sheet FEED_ANALYSIS range E5:I11.
Following Problem guessed:
- After first change in any cell (or calculate event) of FEED_ANALYSIS, current date is being added to col A in sheet LOG with variable
dtmTime
(and being treated asdatcomp
in next event). So further update of LOG from cell changes in FEED_ANALYSIS are prevented as if clause is comparingdtmTime
withdatcomp
and branching toNoUpd:
. - I think the code will be fired always on the calculation event of the worksheet.
If Not Intersect(Xrg, Range("E5:I11 ")) Is Nothing Then
will be always true. - Typo of
Cost_Per_day
andCost _day
may be throwing error at.Cells(Rw, 2) = Cost_Per_day
if the above assumptions are correct, may try the code in FEED_ANALYSIS. Tried to keep modifications minimal.
Option Explicit 'added
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cost_Per_day
Dim COST_kg
Dim AVG_SALES_PRICE
Dim COST_NET_PURCHASE
Dim PROFIT_GROSS
Dim PROFIT_NET
Dim PROFIT_NET_X
Dim Flag_set
Dim dtmTime As Date
Dim datcomp As Date 'added
Dim Rw As Long
Dim LastRw As Long ' added
Dim PrvRw As Long 'added
'If Critical Cells change, move contents to Log sheet
Dim Xrg As Range
Set Xrg = Range("E5:I11")
If Not Intersect(Target, Xrg) Is Nothing Then
dtmTime = Now()
Cost_Per_day = Worksheets("FEED_ANALYSIS").Range("E7").Value ' Cost_day changed to Cost_Per_day as per Dim
COST_kg = Worksheets("FEED_ANALYSIS").Range("F7").Value
AVG_SALES_PRICE = Worksheets("FEED_ANALYSIS").Range("I5").Value
COST_NET_PURCHASE = Worksheets("FEED_ANALYSIS").Range("G11").Value
PROFIT_GROSS = Worksheets("FEED_ANALYSIS").Range("I7").Value
PROFIT_NET = Worksheets("FEED_ANALYSIS").Range("I8").Value
PROFIT_NET_X = Worksheets("FEED_ANALYSIS").Range("I9").Value
LastRw = Sheets("LOG").Range("A" & Rows.Count).End(xlUp).Row + 1
PrvRw = LastRw - 1
With Sheets("LOG")
datcomp = .Cells(PrvRw, 1)
' if the previous entry date is the same as the current date then
' choose previous entry row to update other data
' else
' Chose last row for new entry
' But this approach will not work if data is not enetered for some unforeseen
' reason before 23:59 hrs of currect date i.e dtmTime = Now()
' Better to use datetime from a manually entered cell in sheet "FEED_ANALYSIS"
' with some validation
If Year(datcomp) <> Year(dtmTime) Or Month(datcomp) <> Month(dtmTime) Or Day(datcomp) <> Day(dtmTime) Then
Rw = LastRw
.Cells(Rw, 1) = dtmTime
Else
Rw = PrvRw
End If
.Cells(Rw, 1) = dtmTime
.Cells(Rw, 2) = Cost_Per_day
.Cells(Rw, 3) = COST_kg
.Cells(Rw, 4) = AVG_SALES_PRICE
.Cells(Rw, 5) = COST_NET_PURCHASE
.Cells(Rw, 6) = PROFIT_GROSS
.Cells(Rw, 7) = PROFIT_NET
.Cells(Rw, 8) = PROFIT_NET_X
.Cells(Rw, 11) = .Cells(Rw - 1, 1)
End With
End If
End Sub
Answer would read better with proper indenting. Kudos for getting rid of theGOTO
!
– AJD
Nov 24 '18 at 6:48
Thank you very much. I am sure by viewing my code you have determined that I am completely new to this. I still have 2 issues. 1) FEED_ANALYSIS is a sheet that has no manual input fields therefore the code will not execute. 2) ive added the code to the FEED_ANALYSIS sheet (right click on name, view code, copy paste code), and now code does not execute at all in either debug (F8, or F5), or if i actualy manualy change fields in the change array. Any suggestions?
– GeorgiaCows
Nov 24 '18 at 9:45
May try the simplest approach to shift the code to the sheet where manual feeding in taking place. Then modify lineSet Xrg = Range("E5:I11")
with range that is causing changes in the 7 variables in sheet FEED_ANALYSIS. Could not understand 2nd point in your comment., Did you actually manually changed any cells in the range E5:I11 in sheet FEED_ANALYSIS and still failed to trigger the code? It is working in my trial.Please share How the data is being handled and how you want to trigger the code for exploring other alternatives.
– Ahmed AU
Nov 24 '18 at 16:31
Execution of the code can be tested by changes in sheet LOG only. After manual change in FEED_ANALYSIS, go to LOG and check for changed value. Alternatively may useMsgbox
orDebug.Print
in various stages of the procedure and manually change critical cells in FEED_ANALYSIS to test.execution of the code..
– Ahmed AU
Nov 25 '18 at 1:02
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454312%2fsame-macro-on-two-different-worksheets-works-on-one-aborts-on-the-other%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assumption: Data is being feed into sheet FEED_ANALYSIS range E5:I11.
Following Problem guessed:
- After first change in any cell (or calculate event) of FEED_ANALYSIS, current date is being added to col A in sheet LOG with variable
dtmTime
(and being treated asdatcomp
in next event). So further update of LOG from cell changes in FEED_ANALYSIS are prevented as if clause is comparingdtmTime
withdatcomp
and branching toNoUpd:
. - I think the code will be fired always on the calculation event of the worksheet.
If Not Intersect(Xrg, Range("E5:I11 ")) Is Nothing Then
will be always true. - Typo of
Cost_Per_day
andCost _day
may be throwing error at.Cells(Rw, 2) = Cost_Per_day
if the above assumptions are correct, may try the code in FEED_ANALYSIS. Tried to keep modifications minimal.
Option Explicit 'added
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cost_Per_day
Dim COST_kg
Dim AVG_SALES_PRICE
Dim COST_NET_PURCHASE
Dim PROFIT_GROSS
Dim PROFIT_NET
Dim PROFIT_NET_X
Dim Flag_set
Dim dtmTime As Date
Dim datcomp As Date 'added
Dim Rw As Long
Dim LastRw As Long ' added
Dim PrvRw As Long 'added
'If Critical Cells change, move contents to Log sheet
Dim Xrg As Range
Set Xrg = Range("E5:I11")
If Not Intersect(Target, Xrg) Is Nothing Then
dtmTime = Now()
Cost_Per_day = Worksheets("FEED_ANALYSIS").Range("E7").Value ' Cost_day changed to Cost_Per_day as per Dim
COST_kg = Worksheets("FEED_ANALYSIS").Range("F7").Value
AVG_SALES_PRICE = Worksheets("FEED_ANALYSIS").Range("I5").Value
COST_NET_PURCHASE = Worksheets("FEED_ANALYSIS").Range("G11").Value
PROFIT_GROSS = Worksheets("FEED_ANALYSIS").Range("I7").Value
PROFIT_NET = Worksheets("FEED_ANALYSIS").Range("I8").Value
PROFIT_NET_X = Worksheets("FEED_ANALYSIS").Range("I9").Value
LastRw = Sheets("LOG").Range("A" & Rows.Count).End(xlUp).Row + 1
PrvRw = LastRw - 1
With Sheets("LOG")
datcomp = .Cells(PrvRw, 1)
' if the previous entry date is the same as the current date then
' choose previous entry row to update other data
' else
' Chose last row for new entry
' But this approach will not work if data is not enetered for some unforeseen
' reason before 23:59 hrs of currect date i.e dtmTime = Now()
' Better to use datetime from a manually entered cell in sheet "FEED_ANALYSIS"
' with some validation
If Year(datcomp) <> Year(dtmTime) Or Month(datcomp) <> Month(dtmTime) Or Day(datcomp) <> Day(dtmTime) Then
Rw = LastRw
.Cells(Rw, 1) = dtmTime
Else
Rw = PrvRw
End If
.Cells(Rw, 1) = dtmTime
.Cells(Rw, 2) = Cost_Per_day
.Cells(Rw, 3) = COST_kg
.Cells(Rw, 4) = AVG_SALES_PRICE
.Cells(Rw, 5) = COST_NET_PURCHASE
.Cells(Rw, 6) = PROFIT_GROSS
.Cells(Rw, 7) = PROFIT_NET
.Cells(Rw, 8) = PROFIT_NET_X
.Cells(Rw, 11) = .Cells(Rw - 1, 1)
End With
End If
End Sub
Answer would read better with proper indenting. Kudos for getting rid of theGOTO
!
– AJD
Nov 24 '18 at 6:48
Thank you very much. I am sure by viewing my code you have determined that I am completely new to this. I still have 2 issues. 1) FEED_ANALYSIS is a sheet that has no manual input fields therefore the code will not execute. 2) ive added the code to the FEED_ANALYSIS sheet (right click on name, view code, copy paste code), and now code does not execute at all in either debug (F8, or F5), or if i actualy manualy change fields in the change array. Any suggestions?
– GeorgiaCows
Nov 24 '18 at 9:45
May try the simplest approach to shift the code to the sheet where manual feeding in taking place. Then modify lineSet Xrg = Range("E5:I11")
with range that is causing changes in the 7 variables in sheet FEED_ANALYSIS. Could not understand 2nd point in your comment., Did you actually manually changed any cells in the range E5:I11 in sheet FEED_ANALYSIS and still failed to trigger the code? It is working in my trial.Please share How the data is being handled and how you want to trigger the code for exploring other alternatives.
– Ahmed AU
Nov 24 '18 at 16:31
Execution of the code can be tested by changes in sheet LOG only. After manual change in FEED_ANALYSIS, go to LOG and check for changed value. Alternatively may useMsgbox
orDebug.Print
in various stages of the procedure and manually change critical cells in FEED_ANALYSIS to test.execution of the code..
– Ahmed AU
Nov 25 '18 at 1:02
add a comment |
Assumption: Data is being feed into sheet FEED_ANALYSIS range E5:I11.
Following Problem guessed:
- After first change in any cell (or calculate event) of FEED_ANALYSIS, current date is being added to col A in sheet LOG with variable
dtmTime
(and being treated asdatcomp
in next event). So further update of LOG from cell changes in FEED_ANALYSIS are prevented as if clause is comparingdtmTime
withdatcomp
and branching toNoUpd:
. - I think the code will be fired always on the calculation event of the worksheet.
If Not Intersect(Xrg, Range("E5:I11 ")) Is Nothing Then
will be always true. - Typo of
Cost_Per_day
andCost _day
may be throwing error at.Cells(Rw, 2) = Cost_Per_day
if the above assumptions are correct, may try the code in FEED_ANALYSIS. Tried to keep modifications minimal.
Option Explicit 'added
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cost_Per_day
Dim COST_kg
Dim AVG_SALES_PRICE
Dim COST_NET_PURCHASE
Dim PROFIT_GROSS
Dim PROFIT_NET
Dim PROFIT_NET_X
Dim Flag_set
Dim dtmTime As Date
Dim datcomp As Date 'added
Dim Rw As Long
Dim LastRw As Long ' added
Dim PrvRw As Long 'added
'If Critical Cells change, move contents to Log sheet
Dim Xrg As Range
Set Xrg = Range("E5:I11")
If Not Intersect(Target, Xrg) Is Nothing Then
dtmTime = Now()
Cost_Per_day = Worksheets("FEED_ANALYSIS").Range("E7").Value ' Cost_day changed to Cost_Per_day as per Dim
COST_kg = Worksheets("FEED_ANALYSIS").Range("F7").Value
AVG_SALES_PRICE = Worksheets("FEED_ANALYSIS").Range("I5").Value
COST_NET_PURCHASE = Worksheets("FEED_ANALYSIS").Range("G11").Value
PROFIT_GROSS = Worksheets("FEED_ANALYSIS").Range("I7").Value
PROFIT_NET = Worksheets("FEED_ANALYSIS").Range("I8").Value
PROFIT_NET_X = Worksheets("FEED_ANALYSIS").Range("I9").Value
LastRw = Sheets("LOG").Range("A" & Rows.Count).End(xlUp).Row + 1
PrvRw = LastRw - 1
With Sheets("LOG")
datcomp = .Cells(PrvRw, 1)
' if the previous entry date is the same as the current date then
' choose previous entry row to update other data
' else
' Chose last row for new entry
' But this approach will not work if data is not enetered for some unforeseen
' reason before 23:59 hrs of currect date i.e dtmTime = Now()
' Better to use datetime from a manually entered cell in sheet "FEED_ANALYSIS"
' with some validation
If Year(datcomp) <> Year(dtmTime) Or Month(datcomp) <> Month(dtmTime) Or Day(datcomp) <> Day(dtmTime) Then
Rw = LastRw
.Cells(Rw, 1) = dtmTime
Else
Rw = PrvRw
End If
.Cells(Rw, 1) = dtmTime
.Cells(Rw, 2) = Cost_Per_day
.Cells(Rw, 3) = COST_kg
.Cells(Rw, 4) = AVG_SALES_PRICE
.Cells(Rw, 5) = COST_NET_PURCHASE
.Cells(Rw, 6) = PROFIT_GROSS
.Cells(Rw, 7) = PROFIT_NET
.Cells(Rw, 8) = PROFIT_NET_X
.Cells(Rw, 11) = .Cells(Rw - 1, 1)
End With
End If
End Sub
Answer would read better with proper indenting. Kudos for getting rid of theGOTO
!
– AJD
Nov 24 '18 at 6:48
Thank you very much. I am sure by viewing my code you have determined that I am completely new to this. I still have 2 issues. 1) FEED_ANALYSIS is a sheet that has no manual input fields therefore the code will not execute. 2) ive added the code to the FEED_ANALYSIS sheet (right click on name, view code, copy paste code), and now code does not execute at all in either debug (F8, or F5), or if i actualy manualy change fields in the change array. Any suggestions?
– GeorgiaCows
Nov 24 '18 at 9:45
May try the simplest approach to shift the code to the sheet where manual feeding in taking place. Then modify lineSet Xrg = Range("E5:I11")
with range that is causing changes in the 7 variables in sheet FEED_ANALYSIS. Could not understand 2nd point in your comment., Did you actually manually changed any cells in the range E5:I11 in sheet FEED_ANALYSIS and still failed to trigger the code? It is working in my trial.Please share How the data is being handled and how you want to trigger the code for exploring other alternatives.
– Ahmed AU
Nov 24 '18 at 16:31
Execution of the code can be tested by changes in sheet LOG only. After manual change in FEED_ANALYSIS, go to LOG and check for changed value. Alternatively may useMsgbox
orDebug.Print
in various stages of the procedure and manually change critical cells in FEED_ANALYSIS to test.execution of the code..
– Ahmed AU
Nov 25 '18 at 1:02
add a comment |
Assumption: Data is being feed into sheet FEED_ANALYSIS range E5:I11.
Following Problem guessed:
- After first change in any cell (or calculate event) of FEED_ANALYSIS, current date is being added to col A in sheet LOG with variable
dtmTime
(and being treated asdatcomp
in next event). So further update of LOG from cell changes in FEED_ANALYSIS are prevented as if clause is comparingdtmTime
withdatcomp
and branching toNoUpd:
. - I think the code will be fired always on the calculation event of the worksheet.
If Not Intersect(Xrg, Range("E5:I11 ")) Is Nothing Then
will be always true. - Typo of
Cost_Per_day
andCost _day
may be throwing error at.Cells(Rw, 2) = Cost_Per_day
if the above assumptions are correct, may try the code in FEED_ANALYSIS. Tried to keep modifications minimal.
Option Explicit 'added
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cost_Per_day
Dim COST_kg
Dim AVG_SALES_PRICE
Dim COST_NET_PURCHASE
Dim PROFIT_GROSS
Dim PROFIT_NET
Dim PROFIT_NET_X
Dim Flag_set
Dim dtmTime As Date
Dim datcomp As Date 'added
Dim Rw As Long
Dim LastRw As Long ' added
Dim PrvRw As Long 'added
'If Critical Cells change, move contents to Log sheet
Dim Xrg As Range
Set Xrg = Range("E5:I11")
If Not Intersect(Target, Xrg) Is Nothing Then
dtmTime = Now()
Cost_Per_day = Worksheets("FEED_ANALYSIS").Range("E7").Value ' Cost_day changed to Cost_Per_day as per Dim
COST_kg = Worksheets("FEED_ANALYSIS").Range("F7").Value
AVG_SALES_PRICE = Worksheets("FEED_ANALYSIS").Range("I5").Value
COST_NET_PURCHASE = Worksheets("FEED_ANALYSIS").Range("G11").Value
PROFIT_GROSS = Worksheets("FEED_ANALYSIS").Range("I7").Value
PROFIT_NET = Worksheets("FEED_ANALYSIS").Range("I8").Value
PROFIT_NET_X = Worksheets("FEED_ANALYSIS").Range("I9").Value
LastRw = Sheets("LOG").Range("A" & Rows.Count).End(xlUp).Row + 1
PrvRw = LastRw - 1
With Sheets("LOG")
datcomp = .Cells(PrvRw, 1)
' if the previous entry date is the same as the current date then
' choose previous entry row to update other data
' else
' Chose last row for new entry
' But this approach will not work if data is not enetered for some unforeseen
' reason before 23:59 hrs of currect date i.e dtmTime = Now()
' Better to use datetime from a manually entered cell in sheet "FEED_ANALYSIS"
' with some validation
If Year(datcomp) <> Year(dtmTime) Or Month(datcomp) <> Month(dtmTime) Or Day(datcomp) <> Day(dtmTime) Then
Rw = LastRw
.Cells(Rw, 1) = dtmTime
Else
Rw = PrvRw
End If
.Cells(Rw, 1) = dtmTime
.Cells(Rw, 2) = Cost_Per_day
.Cells(Rw, 3) = COST_kg
.Cells(Rw, 4) = AVG_SALES_PRICE
.Cells(Rw, 5) = COST_NET_PURCHASE
.Cells(Rw, 6) = PROFIT_GROSS
.Cells(Rw, 7) = PROFIT_NET
.Cells(Rw, 8) = PROFIT_NET_X
.Cells(Rw, 11) = .Cells(Rw - 1, 1)
End With
End If
End Sub
Assumption: Data is being feed into sheet FEED_ANALYSIS range E5:I11.
Following Problem guessed:
- After first change in any cell (or calculate event) of FEED_ANALYSIS, current date is being added to col A in sheet LOG with variable
dtmTime
(and being treated asdatcomp
in next event). So further update of LOG from cell changes in FEED_ANALYSIS are prevented as if clause is comparingdtmTime
withdatcomp
and branching toNoUpd:
. - I think the code will be fired always on the calculation event of the worksheet.
If Not Intersect(Xrg, Range("E5:I11 ")) Is Nothing Then
will be always true. - Typo of
Cost_Per_day
andCost _day
may be throwing error at.Cells(Rw, 2) = Cost_Per_day
if the above assumptions are correct, may try the code in FEED_ANALYSIS. Tried to keep modifications minimal.
Option Explicit 'added
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cost_Per_day
Dim COST_kg
Dim AVG_SALES_PRICE
Dim COST_NET_PURCHASE
Dim PROFIT_GROSS
Dim PROFIT_NET
Dim PROFIT_NET_X
Dim Flag_set
Dim dtmTime As Date
Dim datcomp As Date 'added
Dim Rw As Long
Dim LastRw As Long ' added
Dim PrvRw As Long 'added
'If Critical Cells change, move contents to Log sheet
Dim Xrg As Range
Set Xrg = Range("E5:I11")
If Not Intersect(Target, Xrg) Is Nothing Then
dtmTime = Now()
Cost_Per_day = Worksheets("FEED_ANALYSIS").Range("E7").Value ' Cost_day changed to Cost_Per_day as per Dim
COST_kg = Worksheets("FEED_ANALYSIS").Range("F7").Value
AVG_SALES_PRICE = Worksheets("FEED_ANALYSIS").Range("I5").Value
COST_NET_PURCHASE = Worksheets("FEED_ANALYSIS").Range("G11").Value
PROFIT_GROSS = Worksheets("FEED_ANALYSIS").Range("I7").Value
PROFIT_NET = Worksheets("FEED_ANALYSIS").Range("I8").Value
PROFIT_NET_X = Worksheets("FEED_ANALYSIS").Range("I9").Value
LastRw = Sheets("LOG").Range("A" & Rows.Count).End(xlUp).Row + 1
PrvRw = LastRw - 1
With Sheets("LOG")
datcomp = .Cells(PrvRw, 1)
' if the previous entry date is the same as the current date then
' choose previous entry row to update other data
' else
' Chose last row for new entry
' But this approach will not work if data is not enetered for some unforeseen
' reason before 23:59 hrs of currect date i.e dtmTime = Now()
' Better to use datetime from a manually entered cell in sheet "FEED_ANALYSIS"
' with some validation
If Year(datcomp) <> Year(dtmTime) Or Month(datcomp) <> Month(dtmTime) Or Day(datcomp) <> Day(dtmTime) Then
Rw = LastRw
.Cells(Rw, 1) = dtmTime
Else
Rw = PrvRw
End If
.Cells(Rw, 1) = dtmTime
.Cells(Rw, 2) = Cost_Per_day
.Cells(Rw, 3) = COST_kg
.Cells(Rw, 4) = AVG_SALES_PRICE
.Cells(Rw, 5) = COST_NET_PURCHASE
.Cells(Rw, 6) = PROFIT_GROSS
.Cells(Rw, 7) = PROFIT_NET
.Cells(Rw, 8) = PROFIT_NET_X
.Cells(Rw, 11) = .Cells(Rw - 1, 1)
End With
End If
End Sub
edited Nov 24 '18 at 5:39
answered Nov 24 '18 at 5:34
Ahmed AUAhmed AU
77028
77028
Answer would read better with proper indenting. Kudos for getting rid of theGOTO
!
– AJD
Nov 24 '18 at 6:48
Thank you very much. I am sure by viewing my code you have determined that I am completely new to this. I still have 2 issues. 1) FEED_ANALYSIS is a sheet that has no manual input fields therefore the code will not execute. 2) ive added the code to the FEED_ANALYSIS sheet (right click on name, view code, copy paste code), and now code does not execute at all in either debug (F8, or F5), or if i actualy manualy change fields in the change array. Any suggestions?
– GeorgiaCows
Nov 24 '18 at 9:45
May try the simplest approach to shift the code to the sheet where manual feeding in taking place. Then modify lineSet Xrg = Range("E5:I11")
with range that is causing changes in the 7 variables in sheet FEED_ANALYSIS. Could not understand 2nd point in your comment., Did you actually manually changed any cells in the range E5:I11 in sheet FEED_ANALYSIS and still failed to trigger the code? It is working in my trial.Please share How the data is being handled and how you want to trigger the code for exploring other alternatives.
– Ahmed AU
Nov 24 '18 at 16:31
Execution of the code can be tested by changes in sheet LOG only. After manual change in FEED_ANALYSIS, go to LOG and check for changed value. Alternatively may useMsgbox
orDebug.Print
in various stages of the procedure and manually change critical cells in FEED_ANALYSIS to test.execution of the code..
– Ahmed AU
Nov 25 '18 at 1:02
add a comment |
Answer would read better with proper indenting. Kudos for getting rid of theGOTO
!
– AJD
Nov 24 '18 at 6:48
Thank you very much. I am sure by viewing my code you have determined that I am completely new to this. I still have 2 issues. 1) FEED_ANALYSIS is a sheet that has no manual input fields therefore the code will not execute. 2) ive added the code to the FEED_ANALYSIS sheet (right click on name, view code, copy paste code), and now code does not execute at all in either debug (F8, or F5), or if i actualy manualy change fields in the change array. Any suggestions?
– GeorgiaCows
Nov 24 '18 at 9:45
May try the simplest approach to shift the code to the sheet where manual feeding in taking place. Then modify lineSet Xrg = Range("E5:I11")
with range that is causing changes in the 7 variables in sheet FEED_ANALYSIS. Could not understand 2nd point in your comment., Did you actually manually changed any cells in the range E5:I11 in sheet FEED_ANALYSIS and still failed to trigger the code? It is working in my trial.Please share How the data is being handled and how you want to trigger the code for exploring other alternatives.
– Ahmed AU
Nov 24 '18 at 16:31
Execution of the code can be tested by changes in sheet LOG only. After manual change in FEED_ANALYSIS, go to LOG and check for changed value. Alternatively may useMsgbox
orDebug.Print
in various stages of the procedure and manually change critical cells in FEED_ANALYSIS to test.execution of the code..
– Ahmed AU
Nov 25 '18 at 1:02
Answer would read better with proper indenting. Kudos for getting rid of the
GOTO
!– AJD
Nov 24 '18 at 6:48
Answer would read better with proper indenting. Kudos for getting rid of the
GOTO
!– AJD
Nov 24 '18 at 6:48
Thank you very much. I am sure by viewing my code you have determined that I am completely new to this. I still have 2 issues. 1) FEED_ANALYSIS is a sheet that has no manual input fields therefore the code will not execute. 2) ive added the code to the FEED_ANALYSIS sheet (right click on name, view code, copy paste code), and now code does not execute at all in either debug (F8, or F5), or if i actualy manualy change fields in the change array. Any suggestions?
– GeorgiaCows
Nov 24 '18 at 9:45
Thank you very much. I am sure by viewing my code you have determined that I am completely new to this. I still have 2 issues. 1) FEED_ANALYSIS is a sheet that has no manual input fields therefore the code will not execute. 2) ive added the code to the FEED_ANALYSIS sheet (right click on name, view code, copy paste code), and now code does not execute at all in either debug (F8, or F5), or if i actualy manualy change fields in the change array. Any suggestions?
– GeorgiaCows
Nov 24 '18 at 9:45
May try the simplest approach to shift the code to the sheet where manual feeding in taking place. Then modify line
Set Xrg = Range("E5:I11")
with range that is causing changes in the 7 variables in sheet FEED_ANALYSIS. Could not understand 2nd point in your comment., Did you actually manually changed any cells in the range E5:I11 in sheet FEED_ANALYSIS and still failed to trigger the code? It is working in my trial.Please share How the data is being handled and how you want to trigger the code for exploring other alternatives.– Ahmed AU
Nov 24 '18 at 16:31
May try the simplest approach to shift the code to the sheet where manual feeding in taking place. Then modify line
Set Xrg = Range("E5:I11")
with range that is causing changes in the 7 variables in sheet FEED_ANALYSIS. Could not understand 2nd point in your comment., Did you actually manually changed any cells in the range E5:I11 in sheet FEED_ANALYSIS and still failed to trigger the code? It is working in my trial.Please share How the data is being handled and how you want to trigger the code for exploring other alternatives.– Ahmed AU
Nov 24 '18 at 16:31
Execution of the code can be tested by changes in sheet LOG only. After manual change in FEED_ANALYSIS, go to LOG and check for changed value. Alternatively may use
Msgbox
or Debug.Print
in various stages of the procedure and manually change critical cells in FEED_ANALYSIS to test.execution of the code..– Ahmed AU
Nov 25 '18 at 1:02
Execution of the code can be tested by changes in sheet LOG only. After manual change in FEED_ANALYSIS, go to LOG and check for changed value. Alternatively may use
Msgbox
or Debug.Print
in various stages of the procedure and manually change critical cells in FEED_ANALYSIS to test.execution of the code..– Ahmed AU
Nov 25 '18 at 1:02
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454312%2fsame-macro-on-two-different-worksheets-works-on-one-aborts-on-the-other%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please share your data.
– Michal Rosa
Nov 24 '18 at 1:33
Welcome to SO. I think it will fire always on the calculation event of the worksheet.
If Not Intersect(Xrg, Range("E5:I11 ")) Is Nothing Then
will be always true. From the code it may be assumed .that it is intended to fire fromWorksheets("FEED_ANALYSIS")
and the data will be feed into the range E5:I11. If this assumption is true it is better to use Change event ofWorksheets("FEED_ANALYSIS")
– Ahmed AU
Nov 24 '18 at 1:43