Basic-Excel-R-Toolkit icon indicating copy to clipboard operation
Basic-Excel-R-Toolkit copied to clipboard

Link graphic to cell when calling from VBA

Open ChristianBrauchli opened this issue 6 years ago • 0 comments

Using BERT, I've written a function to plot data with the two columns "Filename" and "Value" in a Bargraph

Bert Plot-Function using ggplot2:


MakeBargraphDiscrete = function(PlotData, label=NULL){

BERT.graphics.device(cell=T);
PlotData = range.to.data.frame(PlotData, TRUE)
PlotData[[1]] = as.character(PlotData[[1]])
PlotData[[2]] = as.numeric(PlotData[[2]])

library(ggplot2)

BarGraphDiscrete = ggplot(data=PlotData, aes(PlotData[[1]], PlotData[[2]])) + 
  geom_col() +
  xlab(paste("\n",colnames(PlotData)[1])) +
  ylab(paste(colnames(PlotData)[2],"\n"))

print(BarGraphDiscrete)

dev.off();

T; }


The function runs as desired when using it directly in a worksheet and links the graphic to the cell:

Excel


Now I would like to call this function via VBA so that an user could later simply "click a button" to plot the data.

Code in VBA:


Sub BargraphDiscret()

Dim rng As Range Set rng = Application.InputBox("Select data with headers", "Obtain data", Type:=8) ActiveCell = Application.Run("BERT.Call", "MakeBargraphDiscrete", rng)

End Sub


However, this throws an "#Value" Error in Excel with the following Error message in the BERT console:

Error in paste0(gsub("\[.*?\]", "", sheetnm), " R", ref@R1, " C", ref@C1) : trying to get slot "R1" from an object of a basic class ("NULL")with no slots

Calling from VBA works when I comment the line containing "BERT.graphics.device(cell=T);" in my Plot-Function which prints the plot in a new window.

However, the goal is to call my plot-function via VBA and link the Plot to a cell. Any idea what I'm doing wrong? Thanks for your help!!!

ChristianBrauchli avatar Jun 11 '19 08:06 ChristianBrauchli