I have a very long code in a notebook nb1 in which all the initialization cells are tagged. I have another notebook nb2 where I want to evaluate the initialization cells in nb1 and get the results in nb2 by using CellTags.

I tried the following code, which does something but I cannot see the results in nb2.

myButton[tag_] := 
Button["Evaluate " <> tag, 
SA_NO_UK_2000_2015_run_1.m", EvaluationElements -> {"Tags" -> {tag}}],
Method -> "Queued"];
Grid[{Map[myButton, {"SectorIOMarketChains", "DensityPlot"}]}]
ExpressionCell[Defer[Print["SectorIOMarketChains"]], "Input", 
CellTags -> {"SectorIOMarketChains"}, ShowCellTags -> True]]
ExpressionCell[Defer[Print["DensityPlot"]], "Input", 
CellTags -> {"DensityPlot"}, ShowCellTags -> True]]

Why does not this code give me the results in nb2?


1 Answer 1


Your file "database_IOtables _SA _NO _UK _ 2000_ 2015_run _ 1.m" isn't a Notebook, but a Package - right? Hence the EvaluationElements option is simply ignored, because Packages has no Cells and associated CellTags.

Obviously, you are trying to adapt the code from the section Examples ► Applications on the Documentation page for NotebookEvaluate.

Here is how it is supposed to work:

  1. Create a Notebook with your cells:

    nb = CreateDocument[{
           ExpressionCell[Defer[Print["SectorIOMarketChains"]], "Input", 
            CellTags -> {"SectorIOMarketChains"}, ShowCellTags -> True],
           ExpressionCell[Defer[Print["DensityPlot"]], "Input", CellTags -> {"DensityPlot"}, 
            ShowCellTags -> True]
  2. In current Notebook create Buttons for evaluating tagged cells from that Notebook:

    myButton[nb_, tag_] := 
      Button["Evaluate " <> tag, 
       NotebookEvaluate[nb, EvaluationElements -> {"Tags" -> {tag}}], Method -> "Queued"];
    Grid[{Map[myButton[nb, #] &, {"SectorIOMarketChains", "DensityPlot"}]}]


  3. Sequentially press the buttons, the results will appear in current Notebook:


When working with an existing Notebook, on the first step you should use NotebookOpen (possibly with the option Visible -> False if you don't want it to be displayed):

nb = NotebookOpen[filePath, Visible -> False];

Do not forget to close the invisible Notebook when it is no longer needed:


You can get the list of currently opened Notebooks (including invisible) via Notebooks:


Note that NotebookEvaluate is a relatively old function, introduced when CellObject wasn't available. Similar (but not exactly the same) effect can be achieved with Cells and ToExpression:

ToExpression[NotebookRead[Cells[nb, CellTags -> {"SectorIOMarketChains"}]][[1, 1]]]


You can see what ToExpression returns using its three-argument form:

 NotebookRead[Cells[nb, CellTags -> {"SectorIOMarketChains"}]][[1, 1]], StandardForm, Hold]
  • $\begingroup$ @TugrulTemel In my explanation "current Notebook" means a new Notebook (nb2). The existing Notebook I call simply nb (it should be a NotebookObject), you don't need to modify it in any way. If the existing Notebook is already open, you shouldn't use its name at all, - but you should use its NotebookObject which you can get from Notebooks[] (it returns the complete list of NotebookObjects for all opened Notebooks, including invisible Notebooks). $\endgroup$ Commented Dec 7, 2019 at 14:56
  • 1
    $\begingroup$ @TugrulTemel You can have a trouble with selecting the existing Notebook from the list returned by Notebooks[]. This is why I recommend to save your existing Notebook and use nb = NotebookOpen[filePath]; for opening it and getting the NotebookObject (for the start). It isn't nesessary, but it will simplify your start. $\endgroup$ Commented Dec 7, 2019 at 15:03
  • $\begingroup$ @TugrulTemel When you receive the NotebookEvaluate::nbnfnd message, is your Notebook opened (really)? Please check, whether the Notebook opened with NotebookOpen is really opened. $\endgroup$ Commented Dec 7, 2019 at 15:44
  • $\begingroup$ @TugrulTemel I observed similar behavior initially, but later the nb wasn't closed after pressing the button. I think that it is a bug. If you stably reproduce it, please report it to the tech support: NotebookEvaluate shouldn't close an already opened Notebook which is supplied as the first argument. $\endgroup$ Commented Dec 7, 2019 at 17:29
  • 1
    $\begingroup$ @TugrulTemel No, it isn't a good practice. With default settings all opened Notebooks have "Global`" context (you can check it by evaluating Context[]) and use the same WolframKernel process. Hence all variables are shared between all opened Notebooks, and nb will be available in nb2 after evaluating nb=NotebookOpen[filePath] in nb1. $\endgroup$ Commented Dec 7, 2019 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.