Excel not showing open excel documents through VBA - excel

I'm trying to get excel 2013 to display a list of all open workbooks. through a user form using the following code.
Private Sub UserForm_Activate()
Dim wkBook As Workbook
For Each wkBook In Workbooks
If Not Windows(wkBook.name).Visible Then
ListBox1.AddItem wkBook.name
ElseIf Windows(wkBook.name).Visible Then
ListBox1.AddItem wkBook.name
Else
ListBox1.AddItem wkBook.name
End If
MsgBox wkBook.name
Next wkBook
End Sub
I dont know if the work books are simply not visible or hidden. is there any way to force excel to show all open excel docs?
It works, however it does not display all open workbooks. it will tend to show all workbooks if the previous workbooks are opened before opening the workbook with the code.
this is part of an overall greater import feature. where the user selects from all open workbooks which workbook to import the data from. however the workbook will not display the workbooks if the main workbook is opened first. the workbook in which the data is imported is an exported database from ie to excel.
all workbooks listed in a userform. The workbook for the information is hidden I believe but still editable. how can i unhide this using vba from a different workbook?
I hope i explained this correctly.
Cheers

Try
For Each wkBook In Workbooks
wkBook.Activate
ActiveWindow.WindowState = xlMaximized
Also try
wkBook.Windows(1).Visible = True
or
ActiveWindow.Visible = True

Related

Selecting sheets for export

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.

Modify embedded Excel workbook in another Excel workbook VBA

I have an Excel workbook in which I have embedded another Excel workbook. I am able to open it with VBA, but I have no idea how to refer and edit some cells in embedded workbook. Any idea how to do that? Thanks a lot in advance.
Sub openembeddedXL2()
Sheets("sheet1").OLEObjects("SalesFile").Activate
End Sub
As long as a workbook is open you can directly refer to it by its name.
Workbooks("workbook name") etc.
Since you open the workbook with Sheets("sheet1").OLEObjects("SalesFile").Activate the workbook related to the object will then be opened as a file called "Worksheet in your current workbook". You can therefore use:
Dim wb as workbook
Sheets("sheet1").OLEObjects("SalesFile").Activate
set wb = Workbooks("Worksheet in " & ThisWorkbook.Name)
Thisworkbook.sheets("Sheet1").Range("A1").value = wb.sheets("Sheet1").range("A1").Value 'etc. etc.
wb.Close
Thisworkbook is a handy tool here, as it will always refer to the workbook the macro is in, despite which workbook is currently active.

Excel 2016 VBA workbook activates but then deactivates

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]

Create Macro template to use on Spreadsheets

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

Send User Form in Excel to another Workbook?

I am very new to VB coding and creating forms in Excel. I have created a very simple form in Excel that now captures user data and saves to a sheet within the Form's workbook. Is there a way to code the command button so that it will save the user data to separate workbook that is saved on a network server?
One way is open other workbook and insert data, for example
Sub ExtractData()
Dim nwb As Workbook
Set nwb = Workbooks.Open("\\Your\Server\Path\YourFile.xlsx")
With nwb.Sheets(1)
.Range("A1").Value = YourForm.Textbox1.Text
.Range("B1").Value = YourForm.Textbox2.Text
.Range("C1").Value = YourForm.Textbox3.Text
End With
nwb.Close True
End Sub
you can also use ADO to use Insert statement with TSQL, to create a connection see this

Resources