In the sample Sample15.cs downloaded from EPPlus, I see that EPPlus can write/inject VBA macros. However, I don't see how to read all the VBA macros from a .xlsm workbook.
Can EPPlus interpret VbaProject.bin and support this functionality?
PS: it seems that Open XML SDK cannot do this either (please correct me if I am wrong), that's why I am considering EPPlus...
If you just want to read the code in each modules you just have to go through the 'Workbook.VbaProject.Modules' collection like this:
var fi = new FileInfo(#"C:\temp\Book1.xlsm");
using (var pck = new ExcelPackage(fi))
{
var modules = pck.Workbook.VbaProject.Modules;
foreach (var module in modules)
{
Console.WriteLine($"Module Name: {module.Name}{Environment.NewLine}Code:");
Console.Write(module.Code);
Console.WriteLine("---------");
}
Which will give you this in the output (I just created an excel xlsm with two modules):
Module Name: ThisWorkbook
Code:
---------
Module Name: Sheet1
Code:
---------
Module Name: Module1
Code:
Public Sub proc1()
Dim wb As Workbook
Set wb = ActiveWorkbook
Dim ws As Worksheet
Set ws = wb.ActiveSheet
ws.Cells(1, 1).Value = "proc1"
End Sub
---------
Module Name: Module2
Code:
Public Sub proc2()
Dim wb As Workbook
Set wb = ActiveWorkbook
Dim ws As Worksheet
Set ws = wb.ActiveSheet
ws.Cells(2, 1).Value = "proc2"
End Sub
---------
Related
I have two workbooks in excel. I am trying to copy a worksheet from one workbook to another.
And after that I want to close the workbook where I had copied from.
What I have done so far:
Sub copy()
Workbooks.Open filename:= _
"C:\2016.xlsm"
ActiveWorkbook.Sheets("Grafic").Select
Selection.Copy Destination:=Workbooks("C:\Grafic.xlsx").Sheets("Sheet1").Range("A1")
End Sub
Thanks.
Maybe this helps
Option Explicit
Sub CopyIt()
Dim wb As Workbook
Dim copyWb As Workbook
Dim wks As Worksheet
Dim fileName As String, sheetName As String
fileName = "... complete filename ..."
sheetName = "... sheet name ..."
Set wb = Workbooks.Open(fileName:=fileName)
Set wks = wb.Sheets(sheetName)
Set copyWb = ThisWorkbook ' the workbook you would like to copy to
wks.copy before:=copyWb.Sheets(1)
wb.Close False
End Sub
Use
Application.Workbooks("2016.xlsm").Close
Close method has some parameters to set if you want to save changes or not.
More info:
Workbook.Close
I am trying to copy a sheet and after this, open the copy to change the first sheet's name.
This is the code inserted on Module1:
Sub CopiarNovaPlanilha()
Dim wkb As Workbook
ActiveWorkbook.SaveCopyAs "I:\CGP\DEOPEX\01 - Supervisão\10 - Alocação das equipes\Consulta Alocados\ALOCACAO TECNICOS.xlsx"
Set wkb = Workbooks.Open("I:\CGP\DEOPEX\01 - Supervisão\10 - Alocação das equipes\Consulta Alocados\ALOCACAO TECNICOS.xlsx")
wkb.Sheets(1).Name = "FUNCIONARIOS"
End Sub
The first part of the Sub to save a copy works perfectly. Although when I try to run the second part it gives me the following error:
Run-time error 1004: Excel cannot open the file
And it says that it can't understand the extension .xlsx or that maybe the file is corrupted, but I've checked and the file is ok.
Does anyone knows which problem is that?
This works for me
Private Sub derp()
Dim wbk As Workbook
Dim path As String
path = "C:\Users\dcoats\Desktop\Book1.xlsx"
Set wbk = Workbooks.Open(path)
wbk.Sheets(1).Name = "FUNCIONARIOS"
End Sub
So maybe check your string thats the path to your workbook?
EDIT
I just tried this and it works as well.
Private Sub derp()
Dim wbk As Workbook
Dim path As String
path = "C:\Users\dcoats\Desktop\CGP\DEOPEX\01 - Supervisão\10 - Alocação das equipes\Consulta Alocados\Book1.xlsx"
Set wbk = Workbooks.Open(path)
wbk.Sheets(1).Name = "FUNCIONARIOS"
End Sub
And I just tried this as well
Private Sub derp()
Dim wbk As Workbook
Dim path As String
path = "C:\Users\dcoats\Desktop\CGP\DEOPEX\01 - Supervisão\10 - Alocação das equipes\Consulta Alocados\yaybook.xlsm"
ThisWorkbook.SaveAs path, FileFormat:=52
Set wbk = Workbooks.Open(path)
wbk.Sheets(1).Name = "FUNCIONARIOS"
End Sub
Everything works, so we might be missing details
Don't want to Hijack Doug's answer, so here's a way to test:
Try comment out the second part of your code - leave the "saveas" and
run. After finish go check your file in the destination path - see if
you can open it successfully by manual.
Now in a NEW sub open up this file by code. Do NOT set the sheet name yet. Let
us know the result
Edit:
Try saving the worksheet to a new workbook and save the new workbook:
Sub test()
Dim wbk As Workbook
Dim nwb As Workbook
Dim path As String
path = "C:\Users\dcoats\Desktop\CGP\DEOPEX\01 - Supervisão\10 - Alocação das equipes\Consulta Alocados\"
Set wbk = ThisWorkbook
Set nwb = Workbooks.Add
wbk.Sheets("Sheet1").Copy before:=nwb.Sheets(1)
nwb.SaveAs path & "test2.xlsx"
End Sub
Update "Sheet1" to whatever worksheetname you're saving
I have two Excel Workbooks:
Source.xlsx
Tool.xlsm
Source.xlsx contains a Worksheet with the VBA Object Name shtTests:
Let's assume that in Tool.xlsm I have a variable that contains a reference to the Workbook stored in Source.xlsx:
Dim wkbSource as Workbook
Set wkbSource = GetSourceWorkbook() ' Some function that gives a reference to the workbook
Core Question: How can I reference shtTests within Tool.xlsm by using shtTests' VBA Name?
Or to formulate the question as code... assume you have this code snippet:
Dim wkbSourceShtTests as Worksheet
Set wkbSourceShtTests = GetShtTestsFromWkbSources(wkbSources)
Question: What does GetShtTestsFromWkbSources have to look like?
Note: I do not want to reference it by its Excel Name like you would do using wkbSources.Worksheets("Test Cloning") because people might change its Excel Name some day.
Is this what you are trying?
Sub Sample()
Dim wbThis As Workbook, wbThat As Workbook
Dim wsThat As Worksheet
Dim wsCodeName As String
Set wbThis = ThisWorkbook
Set wbThat = Workbooks("Book4") '<~~ Change this to relevant workbook
wsCodeName = "ShtSheets"
Set wsThat = wbThat.Worksheets(CStr(wbThat.VBProject.VBComponents(wsCodeName).Properties(7)))
Debug.Print wsThat.Name
End Sub
Note: For this to work, you need to enable access to Visual Basic Projects
On the File menu Excel, Click Options|Trust Center|Trust Center Settings|Macro Settings, Check the box "Trust Access to the VBA Project Object Model"
If you wanted a function instead of setting up the trusted access then this would probably work:
Sub example()
Dim wkbSource As Workbook
Set wkbSource = GetSourceWorkbook()
Dim wkbSourceShtTests As Worksheet
Set wkbSourceShtTests = GetShtTestsFromWkbSources(wkbSource, "shtTests")
End Sub
Function GetShtTestsFromWkbSources(wkbk As Workbook, codename As String) As Worksheet
For Each sht In wkbk.Sheets
If sht.codename = codename Then Set GetShtTestsFromWkbSources = sht
Next
End Function
I am trying to copy a worksheet from the workbook that the VBA is in (ThisWorkbook) and past it into another workbook that the user has open (ActiveWorkbook). I have written a function but I cannot get it to work. I have done something similar before and I have searched the internet but I cannot find a solution or why it is failing me. What am I doing wrong?
Function workBooks() As String
aWbkName = ActiveWorkbook.Name
tWbkName = ThisWorkbook.Name
Dim wbk1 As Workbook
Dim wbk2 As Workbook
Set wbk1 = tWbkName
Set wbk2 = aWbkName
wbk1.Sheet2.Copy After:=wbk2.Sheets(7)
End Function
Try this and see if it works. It will copy Sheet2 in ThisWorkbook and paste it after Sheet1 in the ActiveWorkbook
Option Explicit
Public Sub copy_sheet()
Dim source_worksheet As Worksheet
Set source_worksheet = ThisWorkbook.Worksheets("Sheet2")
Dim target_worksheet As Worksheet
Set target_worksheet = ActiveWorkbook.Worksheets("Sheet1")
source_worksheet.Copy After:=target_worksheet
End Sub
you don't need all that variable dimming and assigning:
Sub workBooks()
ThisWorkbook.Sheet2.Copy After:=ActiveWorkbook.Sheets(7)
End Sub
I would like to export a worksheet from Workbook A to another Workbook B which I need Excel to prompt me to choose. I am getting an error "Type Mismatch". Alternatively, I can export to an entirely new Workbook as well.
Sub savefile()
Worksheets("Test").Activate
Dim wb As Workbook
Dim filter As String
Dim linkf As Variant
Dim targetWorkbook As Workbook
Set targetWorkbook = Application.ActiveWorkbook
caption = "Please Select an output file "
linkf = Application.GetOpenFilename(filter, , caption)
If linkf = False Then Exit Sub
Set wb = Workbooks.Open(linkf)
targetWorkbook.Sheets("Test").Copy After:=Workbooks(wb).Sheets("Sample")
End Sub
Replace the following line
targetWorkbook.Sheets("Test").Copy After:=Workbooks(wb).Sheets("Sample")
to
targetWorkbook.Sheets("Test").Copy After:=wb.Sheets("Sample")
Replace your last line with this:
targetWorkbook.Sheets("Test").Copy After:=wb.Sheets("Sample")
Make sure that you have a worksheet called "Sample" in the second workbook