I would like to know if it is possible (AND HOW) to create a Excel template with a specific Macro to use this Macro automatically at Spreadsheets we are exporting from our measurement hardware.
We export the measurements from our hardware into an Excel Spreadsheet. Now we have to write for every Spreadsheet the same Macro to filter specific criteria we only need.
So we would like to have one Excel Template with this Macro saved in it, to import the Excel Spreadsheet from our hardware so it automatically filters the criteria every time, so we can use it immediately.
How can we arrange this?
If a assume your measurement hardware always produce the same output file name...
1 - You could have a template with the macro that imports and filter your data file.
In this scenario, the file with the macro would not server as a template per see
Sub LoadDataSheet()
Dim sWbkPath As String
sWbkPath = "PATH_TO_FILE\" & "FILE_NAME"
Dim wbkData As Workbook
Set wbkData = Workbooks.Open(sWbkPath)
DataFilteringMacro wbkData 'or sheet
End Sub
2 - You could have an addin that shows a ribbon only when the data file is open
In the "ThisWorkbook" module...
Dim WithEvents App As Application
Private Sub Workbook_Open()
Set App = Application
End Sub
Private Sub App_WorkbookActivate(ByVal Wb As Workbook)
'...
'if using a ribbon, you could put the code in the "GetVisible" callback
'and invalidate the ribbon in the App_WorkbookActivate()
'bVisible is the value set in the ribbon callback
Dim wbk As Workbook
Set wbk = ActiveWorkbook
If wbk.Name = "FILE_NAME" Then
'bVisible = True 'the data file was loaded
Else
'bVisible = False 'another file was loaded
End If
End Sub
Related
I use this piece of code:
Application.Workbooks(V_WBNameOutPut).Activate
to activate a particular excel file, I notice that this method goes in error if the "File name extension" (in the View tab of the Folder Menu) is flagged.
In order to be independent of this, what modification should I do/include to the code or what alternative method should I use?
This answer is based on the comment
I interchange many times during the macro run between 2 workbooks, input and output
excel files, and I need to activate the V_WBNameOutPut, to paste and elaborate, and > this is done multiple times during the run. From the input file, I create the > V_WBNameOutPut file.
As #brax said - capture the workbook when it's opened and you don't have to worry about the extension after that.
Sub Test()
'Open the first workbook and store reference to it.
Dim wrkBk1 As Workbook
Set wrkBk1 = Workbooks.Open("H:\Darren Bartrup-Cook\Test 1.xlsx")
'Open the second workbook and store reference to it.
Dim wrkBk2 As Workbook
Set wrkBk2 = Workbooks.Open("H:\Darren Bartrup-Cook\Test 2.xlsx")
'Copy/paste from wrkbk1 to wrkbk2.
wrkBk1.Worksheets("Sheet1").Range("A1").Copy Destination:=wrkBk2.Worksheets("Sheet1").Range("A4")
'Create a new sheet in wrkbk2.
Dim NewWrkSht As Worksheet
Set NewWrkSht = wrkBk2.Worksheets.Add
NewWrkSht.Name = "My New Sheet"
'Paste copy/paste values from wrkbk1 to wrkbk2.
wrkBk1.Worksheets("Sheet1").Range("A2").Copy
NewWrkSht.Range("A5").PasteSpecial Paste:=xlPasteValues
'Make A3 in wrkbk2 equal the value in wrkbk1 A3.
wrkBk2.Worksheets("Sheet1").Range("A3") = wrkBk1.Worksheets("Sheet1").Range("A3")
'Close the two workbooks.
wrkBk2.Close SaveChanges:=True
wrkBk1.Close SaveChanges:=False
End Sub
I'm trying to create an Excel macro that exports worksheets to PDF. I have a simple piece of code below that successfully exports the active sheet to the folder that I want. What I want to do - and can't find a solution for - is a way to give the user the option to export multiple worksheets to the same PDF. In my application the worksheets exported may have different names, may be created after the macro is written and may be a different number of sheets each time. I have tried to make arrays that use selection but this is beyond my own knowledge of macro writing, which is limited. In an ideal world, I'd like to use a pop-up selection box to choose the sheets to export, but I'll start with the basics of the code first.
Could someone please suggest a section of code that would suit my application?
Sub Export_PDF()
'File name
Dim saveName As String
saveName = Left(ActiveWorkbook.Name, InStrRev(ActiveWorkbook.Name, ".")) & "pdf"
'File path
Dim saveLocation As String
saveLocation = "C:\Users\" & Environ("username") & "\Temp Out\"
'Save Active Sheet(s) as PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=saveLocation & saveName
End Sub
Excel stores worksheets as a collection, so you can easily access all the worksheets that have been created in the workbook.
To allow user to select which ones he wants to export, you can create a UserForm with a ListBox that will read all available worksheets in the workbook and display them. Sample code that does that below (the UserForm has only one listbox created ListBox1 and nothing else).
Sub export_wsheets()
Dim wsheet As Worksheet
Dim wsheets_col As worksheets
Dim uForm As New UserForm1
Set wsheets_col = ThisWorkbook.worksheets
For Each wsheet In wsheets_col
uForm.ListBox1.AddItem wsheet.Name
Next
uForm.Show
End Sub
From then on you can just save user's choice and loop through the workbooks again exporting the ones that were selected. You can access particular worksheet by using it's name or ID.
It's not a complete solution but I hope it sheds some more light on your problem.
a lot of users in our network use an excel-workbook (.xlsm [office 2010]) created from a template.
Now, there are some important changes I've to do in the template and I want all the users to update their workbook but i'd like to avoid to contact all of them.
So, my Idea is to make an auto-update (copying the contents of their workbooks into new created workbooks and delete the former version).
Unfortunately there are no update-macros in the existing workbooks but they reference to a macro in another workbook.
Each time they open their workbooks the data connections become refreshed automatically.
Can I use this refreshing event to trigger a macro in the (data-source) excel-file (maybe by creating a WithEvents-class module)?
You can do something along these lines, where the user opens a workbook, but its job is to control the version. You can change this to have the code modify sheets etc.
The text file, correct, contains ver9, the workbook contains ver8 in the ver_cont worksheet.
Function get_version() As String
Open "c:\workspace\test_ver.txt" For Input As #1
Input #1, get_version
Close #1
End Function
Function check_version()
If get_version = Worksheets("Ver_cont").Range("a1") Then
' Open the workbook here
Else
' Copy the workbook
' Then open it
End If
End Function
You can try this. It uses withevents and runs when the data is updated.
First, you need to create a class name "clsQueryTable" and put this code in it
Option Explicit
Public WithEvents QTQueryTable As Excel.QueryTable
Private Sub QTQueryTable_BeforeRefresh(blnCancel As Boolean)
'Set blnCancel to true to stop the refresh
Debug.Print blnCancel
End Sub
Private Sub QTQueryTable_AfterRefresh(ByVal blnSuccess As Boolean)
'blnSuccess can be used to check for refresh success.
' I would put your update code here!
Debug.Print blnSuccess
End Sub
Then, you can put this code in your workbook_open event on ThisWorkbook
Option Explicit
Dim colQueryTables As Collection
Private Sub Workbook_Open()
Dim shtMySheet As Worksheet
Dim clsQT As clsQueryTable
Dim qtMyQuery As QueryTable
Dim loMyList As ListObject
Dim conn As WorkbookConnection
Set colQueryTables = New Collection
For Each shtMySheet In ThisWorkbook.Worksheets
For Each loMyList In shtMySheet.ListObjects
Set clsQT = New clsQueryTable
Set clsQT.QTQueryTable = loMyList.QueryTable
colQueryTables.Add clsQT
Next loMyList
Next shtMySheet
For Each conn In Connections
conn.Refresh
Next
End Sub
I have a weird situation that I haven't been able to find the solution for.
I am dealing with large amounts of data on multiple workbooks that need to be opened (let's say 3 workbooks). I need a Userform to be able to interact with all 3 workbooks.
I have made a ComboBox able to do that by when the userform is initialized, it will add the names of the workbooks to the Combobox:
'* initialize the userform *'
Private Sub UserForm_Initialize()
Dim wb As Workbook
'* get the name of all the workbooks and add to the combobox '*
For Each wb In Application.Workbooks
Me.PrimaryBook_ComboBox.AddItem wb.name
Next wb
Me.PrimaryBook_ComboBox = ActiveWorkbook.name
End Sub
Upon a change, it will activate that workbook:
Private Sub PrimaryBook_ComboBox_Change()
Application.ScreenUpdating = True
Dim wb As Workbook
If Me.PrimaryBook_ComboBox <> "" Then
Set wb = Workbooks(Me.PrimaryBook_ComboBox.Text)
wb.Activate
End If
Application.ScreenUpdating = False
End Sub
(this userform has two refedits in it)
When I select another workbook in the combobox, it brings that workbook to the front as it should. But immediately as I click into one of my RefEdit boxes, it goes back to the original workbook opened first.
Here's another part I don't understand, when I load this in Excel 2010 it's flawless. I can select which workbook I want and click on the RefEdit and that workbook will remain activated.
Is there something I'm missing? Any tips and/or tricks that I did not think of?
Thank you
[delete , posted solution did not fix problem]
I have tried everything I could think of, and other people's suggestions, as seen here to specify the sheet names when an SSRS report is exported to Excel by the report runner. So far nothing has worked.
Is there some event that I can tap into and write code (VBScript) for? Data values can be assigned or tweaked using VBScript, but is there a way, in SSRS, to write code for some event such as "OnGenerate" or such?
I have never used SSRS, so no idea how it exports.
However, you could attempt to set the application new_Workbook event to trap any new workbook that is opened and check it for some clue/feature that it is a product of the SSRS report tool.
Eg. You could check the workbook name(if there is a clear indication that it's a product of that tool.If you save upon export as something like "mySSRS_Report20160628.xlsx", then you could check for the "mySSRS_" string.
In order to do this you would have to have the 'pagename' appearing consistently in the same cell of the generated sheets, like $A$2 in my example.
You will need an add-in , so open a brand new workbook and make a new class module called cAppEvents and put this in it:
Option Explicit
Private WithEvents app As Application
Private Sub app_WorkbookOpen(ByVal Wb As Workbook)
Dim i As Long
If InStr(1, Wb.Name, "mySSRS_", vbTextCompare) Then
For i = 1 To Wb.Worksheets.Count
Wb.Worksheets(i).Name = Wb.Worksheets(i).Range("A2").Value
Next i
Wb.Save
End If
End Sub
Private Sub Class_Initialize()
Set app = Application
End Sub
In the ThisWorkbook module of that workbook put this code in:
Private newApp As cAppEvents
Private Sub Workbook_Open()
Set newApp = New cAppEvents
End Sub
Save it off as an Excel add-in. Go to File-->Options-->Addins to manage your add-ins, and check the box with the filename
Test with opening up workbooks with the name having the string "mySSRS_" in them, and with some relevant data in the cell $A$2.
You get the idea....