Skip to content

Instantly share code, notes, and snippets.

@angelovstanton
Created September 6, 2015 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angelovstanton/81dc39620bf089f73517 to your computer and use it in GitHub Desktop.
Save angelovstanton/81dc39620bf089f73517 to your computer and use it in GitHub Desktop.
private static string GetMacro(int macroId, int row, int endCol)
{
StringBuilder sb = new StringBuilder();
string range = "ActiveSheet.Range(Cells(" + row + "," + 3 +
"), Cells(" + row + "," + (endCol + 3) + ")).Select";
sb.AppendLine("Sub Macro" + macroId + "()");
sb.AppendLine("On Error Resume Next");
sb.AppendLine(range);
sb.AppendLine("ActiveSheet.Shapes.AddChart.Select");
sb.AppendLine("ActiveChart.ChartType = xlLine");
sb.AppendLine("ActiveChart.SetSourceData Source:=" + range);
sb.AppendLine("On Error GoTo 0");
sb.AppendLine("End Sub");
return sb.ToString();
}
private static void AddChartButton(MSExcel.Workbook workBook, MSExcel.Worksheet xlWorkSheetNew,
MSExcel.Range currentRange, int macroId, int currentRow, int endCol, string buttonImagePath)
{
MSExcel.Range cell = currentRange.Next;
var width = cell.Width;
var height = 15;
var left = cell.Left;
var top = Math.Max(cell.Top + cell.Height - height, 0);
MSExcel.Shape button = xlWorkSheetNew.Shapes.AddPicture(@buttonImagePath, MsoTriState.msoFalse,
MsoTriState.msoCTrue, left, top, width, height);
VBIDE.VBComponent module = workBook.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);
module.CodeModule.AddFromString(GetMacro(macroId, currentRow, endCol));
button.OnAction = "Macro" + macroId;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment