From 8a1d0b0cf87144d25a362b68877c400f7a26a28b Mon Sep 17 00:00:00 2001 From: dfinke Date: Fri, 31 May 2019 11:21:44 -0400 Subject: [PATCH] Added simple vba example --- Examples/VBA/HelloWorldVBA.ps1 | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Examples/VBA/HelloWorldVBA.ps1 diff --git a/Examples/VBA/HelloWorldVBA.ps1 b/Examples/VBA/HelloWorldVBA.ps1 new file mode 100644 index 0000000..aa3c1e4 --- /dev/null +++ b/Examples/VBA/HelloWorldVBA.ps1 @@ -0,0 +1,38 @@ +$xlfile = "$env:temp\test.xlsm" +Remove-Item $xlfile -ErrorAction SilentlyContinue + +$Excel = ConvertFrom-Csv @" +Region,Item,TotalSold +West,screwdriver,98 +West,kiwi,19 +North,kiwi,47 +West,screws,48 +West,avocado,52 +East,avocado,40 +South,drill,61 +North,orange,92 +South,drill,29 +South,saw,36 +"@ | Export-Excel $xlfile -PassThru -AutoSize + +$wb = $Excel.Workbook +$sheet = $wb.Worksheets["Sheet1"] +$wb.CreateVBAProject() + +$code = @" +Public Function HelloWorld() As String + HelloWorld = "Hello World" +End Function + +Public Function DoSum() As Integer + DoSum = Application.Sum(Range("C:C")) +End Function +"@ + +$module = $wb.VbaProject.Modules.AddModule("PSExcelModule") +$module.Code = $code + +Set-Format -WorkSheet $sheet -Range "h7" -Formula "HelloWorld()" -AutoSize +Set-Format -WorkSheet $sheet -Range "h8" -Formula "DoSum()" -AutoSize + +Close-ExcelPackage $Excel -Show \ No newline at end of file