Limit macro to active sheet
I have a worksheet within a multi-worksheet workbook Workbook1 which contains some VBA code that executes when certain calculated cells change in value. All works well. However, if that workbook is active and I open another workbook Workbook2 unrelated to Workbook1, it seems the VBA code for Workbook1 attempts to execute and I get a message box with Runtime Error:
"9: Subscript out of range"
message. As If the code for Workbook1 is being executed. If I press the DEBUG button on the runtime error, I see the code for workbook1.
I need to limit the execution of the specific VBA code to Workbook1, FEED_ANALYSIS Sheet.
Included is the Workbook1 code which is attached to the FEED_ANALYSIS sheet.
Thank you in advance.
Private Sub Worksheet_Calculate()
Dim Cost_Per_day
Dim COST_kg
Dim COST_GROSS_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_Per_day = Worksheets("FEED_ANALYSIS").Range("E7").Value
COST_kg = Worksheets("FEED_ANALYSIS").Range("F7").Value
COST_GROSS_kg = Worksheets("FEED_ANALYSIS").Range("G7").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) = COST_GROSS_kg
.Cells(Rw, 5) = AVG_SALES_PRICE
.Cells(Rw, 6) = COST_NET_PURCHASE
.Cells(Rw, 7) = PROFIT_GROSS
.Cells(Rw, 8) = PROFIT_NET
.Cells(Rw, 9) = 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 worksheet within a multi-worksheet workbook Workbook1 which contains some VBA code that executes when certain calculated cells change in value. All works well. However, if that workbook is active and I open another workbook Workbook2 unrelated to Workbook1, it seems the VBA code for Workbook1 attempts to execute and I get a message box with Runtime Error:
"9: Subscript out of range"
message. As If the code for Workbook1 is being executed. If I press the DEBUG button on the runtime error, I see the code for workbook1.
I need to limit the execution of the specific VBA code to Workbook1, FEED_ANALYSIS Sheet.
Included is the Workbook1 code which is attached to the FEED_ANALYSIS sheet.
Thank you in advance.
Private Sub Worksheet_Calculate()
Dim Cost_Per_day
Dim COST_kg
Dim COST_GROSS_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_Per_day = Worksheets("FEED_ANALYSIS").Range("E7").Value
COST_kg = Worksheets("FEED_ANALYSIS").Range("F7").Value
COST_GROSS_kg = Worksheets("FEED_ANALYSIS").Range("G7").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) = COST_GROSS_kg
.Cells(Rw, 5) = AVG_SALES_PRICE
.Cells(Rw, 6) = COST_NET_PURCHASE
.Cells(Rw, 7) = PROFIT_GROSS
.Cells(Rw, 8) = PROFIT_NET
.Cells(Rw, 9) = PROFIT_NET_X
.Cells(Rw, 11) = .Cells(Rw - 1, 1)
NoUpd:
End With
End If
End Sub
excel vba
1
You need to add the workbook qualification in front of your worksheet references. e.g.Workbooks("WorkbookName").WorkSheets("FEED_ANALYSIS").etc
– Freeflow
Nov 25 '18 at 11:29
1
And you need to make sure that you also qualify all methods that implicitly refer to the active worksheet (e.g. Range("E5:I11"), Sheets("LOG")
– Freeflow
Nov 25 '18 at 11:34
add a comment |
I have a worksheet within a multi-worksheet workbook Workbook1 which contains some VBA code that executes when certain calculated cells change in value. All works well. However, if that workbook is active and I open another workbook Workbook2 unrelated to Workbook1, it seems the VBA code for Workbook1 attempts to execute and I get a message box with Runtime Error:
"9: Subscript out of range"
message. As If the code for Workbook1 is being executed. If I press the DEBUG button on the runtime error, I see the code for workbook1.
I need to limit the execution of the specific VBA code to Workbook1, FEED_ANALYSIS Sheet.
Included is the Workbook1 code which is attached to the FEED_ANALYSIS sheet.
Thank you in advance.
Private Sub Worksheet_Calculate()
Dim Cost_Per_day
Dim COST_kg
Dim COST_GROSS_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_Per_day = Worksheets("FEED_ANALYSIS").Range("E7").Value
COST_kg = Worksheets("FEED_ANALYSIS").Range("F7").Value
COST_GROSS_kg = Worksheets("FEED_ANALYSIS").Range("G7").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) = COST_GROSS_kg
.Cells(Rw, 5) = AVG_SALES_PRICE
.Cells(Rw, 6) = COST_NET_PURCHASE
.Cells(Rw, 7) = PROFIT_GROSS
.Cells(Rw, 8) = PROFIT_NET
.Cells(Rw, 9) = PROFIT_NET_X
.Cells(Rw, 11) = .Cells(Rw - 1, 1)
NoUpd:
End With
End If
End Sub
excel vba
I have a worksheet within a multi-worksheet workbook Workbook1 which contains some VBA code that executes when certain calculated cells change in value. All works well. However, if that workbook is active and I open another workbook Workbook2 unrelated to Workbook1, it seems the VBA code for Workbook1 attempts to execute and I get a message box with Runtime Error:
"9: Subscript out of range"
message. As If the code for Workbook1 is being executed. If I press the DEBUG button on the runtime error, I see the code for workbook1.
I need to limit the execution of the specific VBA code to Workbook1, FEED_ANALYSIS Sheet.
Included is the Workbook1 code which is attached to the FEED_ANALYSIS sheet.
Thank you in advance.
Private Sub Worksheet_Calculate()
Dim Cost_Per_day
Dim COST_kg
Dim COST_GROSS_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_Per_day = Worksheets("FEED_ANALYSIS").Range("E7").Value
COST_kg = Worksheets("FEED_ANALYSIS").Range("F7").Value
COST_GROSS_kg = Worksheets("FEED_ANALYSIS").Range("G7").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) = COST_GROSS_kg
.Cells(Rw, 5) = AVG_SALES_PRICE
.Cells(Rw, 6) = COST_NET_PURCHASE
.Cells(Rw, 7) = PROFIT_GROSS
.Cells(Rw, 8) = PROFIT_NET
.Cells(Rw, 9) = PROFIT_NET_X
.Cells(Rw, 11) = .Cells(Rw - 1, 1)
NoUpd:
End With
End If
End Sub
excel vba
excel vba
edited Nov 26 '18 at 7:10
Pᴇʜ
23.8k62952
23.8k62952
asked Nov 25 '18 at 10:17
GeorgiaCowsGeorgiaCows
62
62
1
You need to add the workbook qualification in front of your worksheet references. e.g.Workbooks("WorkbookName").WorkSheets("FEED_ANALYSIS").etc
– Freeflow
Nov 25 '18 at 11:29
1
And you need to make sure that you also qualify all methods that implicitly refer to the active worksheet (e.g. Range("E5:I11"), Sheets("LOG")
– Freeflow
Nov 25 '18 at 11:34
add a comment |
1
You need to add the workbook qualification in front of your worksheet references. e.g.Workbooks("WorkbookName").WorkSheets("FEED_ANALYSIS").etc
– Freeflow
Nov 25 '18 at 11:29
1
And you need to make sure that you also qualify all methods that implicitly refer to the active worksheet (e.g. Range("E5:I11"), Sheets("LOG")
– Freeflow
Nov 25 '18 at 11:34
1
1
You need to add the workbook qualification in front of your worksheet references. e.g.Workbooks("WorkbookName").WorkSheets("FEED_ANALYSIS").etc
– Freeflow
Nov 25 '18 at 11:29
You need to add the workbook qualification in front of your worksheet references. e.g.Workbooks("WorkbookName").WorkSheets("FEED_ANALYSIS").etc
– Freeflow
Nov 25 '18 at 11:29
1
1
And you need to make sure that you also qualify all methods that implicitly refer to the active worksheet (e.g. Range("E5:I11"), Sheets("LOG")
– Freeflow
Nov 25 '18 at 11:34
And you need to make sure that you also qualify all methods that implicitly refer to the active worksheet (e.g. Range("E5:I11"), Sheets("LOG")
– Freeflow
Nov 25 '18 at 11:34
add a comment |
1 Answer
1
active
oldest
votes
When starting the macros the first time, have it get the workbook name in a global variable. Then use that variable in qualifying the wokbook tou want to work in. If you now open another workbook, the macro will work on the correct wokbook:
Dim gWorkBookName As String
Private Sub Worksheet_Calculate()
'...
If (gWorkBookName = "") Then
gWorkBookName= ActiveWorkbook.Name
End If
'...
With Workbooks(gWorkBookName)
Cost_Per_day = .Worksheets("FEED_ANALYSIS").Range("E7").Value
'...
End With
'...
End Sub
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%2f53466523%2flimit-macro-to-active-sheet%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
When starting the macros the first time, have it get the workbook name in a global variable. Then use that variable in qualifying the wokbook tou want to work in. If you now open another workbook, the macro will work on the correct wokbook:
Dim gWorkBookName As String
Private Sub Worksheet_Calculate()
'...
If (gWorkBookName = "") Then
gWorkBookName= ActiveWorkbook.Name
End If
'...
With Workbooks(gWorkBookName)
Cost_Per_day = .Worksheets("FEED_ANALYSIS").Range("E7").Value
'...
End With
'...
End Sub
add a comment |
When starting the macros the first time, have it get the workbook name in a global variable. Then use that variable in qualifying the wokbook tou want to work in. If you now open another workbook, the macro will work on the correct wokbook:
Dim gWorkBookName As String
Private Sub Worksheet_Calculate()
'...
If (gWorkBookName = "") Then
gWorkBookName= ActiveWorkbook.Name
End If
'...
With Workbooks(gWorkBookName)
Cost_Per_day = .Worksheets("FEED_ANALYSIS").Range("E7").Value
'...
End With
'...
End Sub
add a comment |
When starting the macros the first time, have it get the workbook name in a global variable. Then use that variable in qualifying the wokbook tou want to work in. If you now open another workbook, the macro will work on the correct wokbook:
Dim gWorkBookName As String
Private Sub Worksheet_Calculate()
'...
If (gWorkBookName = "") Then
gWorkBookName= ActiveWorkbook.Name
End If
'...
With Workbooks(gWorkBookName)
Cost_Per_day = .Worksheets("FEED_ANALYSIS").Range("E7").Value
'...
End With
'...
End Sub
When starting the macros the first time, have it get the workbook name in a global variable. Then use that variable in qualifying the wokbook tou want to work in. If you now open another workbook, the macro will work on the correct wokbook:
Dim gWorkBookName As String
Private Sub Worksheet_Calculate()
'...
If (gWorkBookName = "") Then
gWorkBookName= ActiveWorkbook.Name
End If
'...
With Workbooks(gWorkBookName)
Cost_Per_day = .Worksheets("FEED_ANALYSIS").Range("E7").Value
'...
End With
'...
End Sub
answered Nov 25 '18 at 12:19
Paul OgilviePaul Ogilvie
18.4k21235
18.4k21235
add a comment |
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%2f53466523%2flimit-macro-to-active-sheet%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
1
You need to add the workbook qualification in front of your worksheet references. e.g.Workbooks("WorkbookName").WorkSheets("FEED_ANALYSIS").etc
– Freeflow
Nov 25 '18 at 11:29
1
And you need to make sure that you also qualify all methods that implicitly refer to the active worksheet (e.g. Range("E5:I11"), Sheets("LOG")
– Freeflow
Nov 25 '18 at 11:34