Open excel sheet from form - vba

I have made a form with several command buttons which open specifics worksheets. The problem is when I open an excel file from the command button, if the form is not hidden then I'm not able to click on the opened file(Its not activated)
Even if I hide the form, I need to manually goto that file from taskbar it doesn't get activated.
The problem is:
I don't want my form to be hidden because I want the user to be able to open multiple sheets
The opened sheet doesn't get activated.
Here's my code:
Private Sub CommandButton1_Click()
Dim Wb As Excel.Workbook
Set Wb = Workbooks.Open(Filename:="D:/power system design/foo.xlsx", ReadOnly:=False)
UserForm1.Hide
Wb.Activate
Wb.Sheets("Sheet1").Cells(1, 1).Select
End Sub

This is a quick way to accomplish what you said you wanted done. It may not be the best way, but you should be able to drop it in and run with it:
Private Sub CommandButton1_Click()
Dim xls As Excel.Application
set xls = new Excel.Application
xls.Workbooks.Open "D:/power system design/foo.xlsx", ,False
xls.Visible = true
End Sub
or if you want to work with the opened workbook
Private Sub CommandButton1_Click()
Dim xls As Excel.Application
Dim wb as Excel.Workbook
set xls = new Excel.Application
set wb = xls.Workbooks.Open(Filename:="D:/power system design/foo.xlsx", ReadOnly:=False)
xls.Visible = true
End Sub
This will result in the sheet being opened in a new Excel application window.

Related

Opening workbook from file and updating link in original workbook

I'm trying to write a macro in VBA, that will open another Workbook using a PathFile specified in a cell (this works), updates link in workbook in which macro is used (doesn't work) and closes the PathFile workbook (works)
This is a code:
Sub UpdateRaw()
Dim CurrWb As Workbook
Dim FilePath As String
Dim book As Excel.Workbook
Set CurrWb = ActiveWorkbook
FilePath = Range("I1").Value
Dim app As New Excel.Application
app.Visible = True 'so we can see whether correct file is being opened
Set book = app.Workbooks.Open(FilePath)
CurrWb.Activate
Worksheets("Raw_vs_Actual").EnableCalculation = False
Worksheets("Raw_vs_Actual").EnableCalculation = True
book.Close SaveChanges:=False
app.Quit
Set app = Nothing
End Sub
Going step by step I found that command CurrWb.Activate doesn't take me back to my original Workfile. My suspicion is that by opening new Excel Application I can't get back to the CurrWb (ActiveWorkbook). Is there a workaround? I need this so my INDIRECT function doesn't return #REF.
I'm using Excel 2010 in case it's important.
I think Set book = app.Workbooks.Open(FilePath) shall be enough, but if not refresh the workbook:
book.RefreshAll
for opened workbook. For the workbook that contains the macro, use
ThisWorkbook.RefreshAll

VBA Code to Copy and Paste Between Separate Excel Instances

I am trying to copy data from one open instance in excel and load it into a separate open instance. I have the following code but it only copies data from the source workbook since the last save. Also this code can only be run from the destination workbook. Any help would be greatly appreciated.
Sub CollectA()
Dim oApp As Application
Dim oWb As Workbook
Set oWb = GetObject("Test two.xlsm")
Set oApp = oWb.Parent
oWb.Activate
oWb.ActiveSheet.Range("A1").Select
Selection.Copy
Workbooks("Test three.xlsm").Worksheets("Sheet1").Range("B1").PasteSpecial Paste:=xlPasteValues
End Sub
Avoid copy/paste whenever possible:
Sub CollectA()
Dim oWb As Workbook
Set oWb = GetObject("Test two.xlsm")
Workbooks("Test three.xlsm").Worksheets("Sheet1").Range("B1").Value = oWb.ActiveSheet.Range("A1").Value
End Sub
If you want the macro to be in "Test two":
Sub CollectA()
Dim oWb As Workbook
Set oWb = GetObject("Test three.xlsm")
owb.Worksheets("Sheet1").Range("B1").Value = Workbooks("Test two.xlsm").ActiveSheet.Range("A1").Value
End Sub

Using VBA to copy a spreadsheet from a secondary workbook into the primary one

I need to take a spreadsheet and compare it with a spreadsheet from another workbook. I know that I can do this using VBA, but I will need to copy a spreadsheet from another workbook so that both spreadsheets will reside within the same workbook and be accessible for comparison. How do I copy a spreadsheet from one workbook into another using VBA?
You don't need to copy the worksheets over to compare them. Simply open both WorkBooks and and set reference to there WorkSheets.
Sub CompareWorkBooks()
Dim wbPending As Workbook
Dim wsPending As Worksheet
Set wbPending = Workbooks("Items Pending")
Set wsPending = wsPending.Worksheet("Items")
Dim wbRecieved As Workbook
Dim wsRecieved As Worksheet
Set wbRecieved = Workbooks("Items Recieved")
Set wsRecieved = Worksheet("Items")
End Sub
If you provide names for Workbooks & WorkSheets and provide column information, we can give you better answers in a shorter period of time. Don't be afraid to post your code either.
If you want a user to select the target workbook and worksheet:
Create a userform
Declare targetWorkbook and tartgetWorksheet at the top of the code module
Add a command button and a combo box
When the button is clicked a file dialog is opened
A reference is now set to the open file
The names of all the worksheets are added to the combo
Changing the combo box value will set the refernce to tartgetWorksheet
Option Explicit
Public targetWorkbook As Workbook
Public tartgetWorksheet As Worksheet
Private Sub CommandButton1_Click()
Dim targetWorkbook As Workbook
Dim ws As Worksheet
Set targetWorkbook = getTargetWorkbook
If targetWorkbook = Nothing Then
MsgBox "Sowmthing went wrong", vbCritical, "Try Again"
Else
For Each ws In targetWorkbook.Worksheets
ComboBox1.AddItem ws.Name
Next
End If
End Function
Function getTargetWorkbook() As Workbook
With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False
.Show
On Error Resume Next
Set getTargetWorkbook = Application.Workbooks.Open(.SelectedItems(1))
On Error GoTo 0
End With
End Function
Private Sub ComboBox1_Change()
Set tartgetWorksheet = targetWorkbook.Worksheets(ComboBox1.Value)
End Sub

Excel VBA - Cannot Close New Workbook created using Workbook.Add

I have the following code that creates a new workbook and populates it with some data.
Dim wb as Workbook
Set wb = Workbooks.Add
wb.Range("A1") = "Dummy Text" 'Some code that populates data
wb.Activate 'wb.Activate or ThisWorkbook.Activate has no effect
The new workbook opens fine. I am able to minimise it, use the menus, etc. but am unable to click the close button.
If I go back to the original workbook that has the macro and come back to the new workbook, I can close.
How do I overcome this problem? Out of ideas unfortunately.
My desidred use case
Step 1 : User clicks a button -> A new workbook opens up with data
Step 2 : User reads the information in the new workbook and decides to save / close
But the close button in the new workbook won't work until the user goes back to the original workbook and comes again to the new workbook.
after your code
Dim wb as Workbook
Set wb = Workbooks.Add
wb.Range("A1") = "Dummy Text"
insert
set wb = nothing
this will release your new workbook and you can close using the "X" in the top right.
P.S.: Sorry my english
Sub Button1_Click()
Dim wb As Workbook
Set wb = Workbooks.Add
wb.Sheets("Sheet1").Range("A1") = "Dummy Text" 'Some code that populates data
wb.Activate 'wb.Activate or ThisWorkbook.Activate has no effect
End Sub
That worked fine on my excel. I click the close button and it has asked me if i want to save or not.
I think you need to add this line:
Workbooks("source").Close
if you want to close the current workbook , try this :
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If ActiveCell.Value = "CLOSE" Then
ThisWorkbook.Saved = True
ThisWorkbook.Close
End If
End Sub
When displaying your userform, make sure you use ".Show 0".

Vb2010 Excel Not saving

i currently have a form and a button. when i click the button, it creates an excel spread sheet and adds "hi" to range("A1"). The problem is that i am trying to save and close the file but it still prompts me "If i would like to save", is there s way i can get rid of this message? is it not saving for some reason?
i am using the code:
xlWorkbook.save()
Excelapp.quit()
i have also tried using the code below but it still doesn't work:
me.excelapp.displayalerts=false
for some reason it does not want to be saved
Imports Excel = Microsoft.Office.Interop.Excel
Dim ExcelApp As New Microsoft.Office.Interop.Excel.Application
Dim WB As Microsoft.Office.Interop.Excel.Workbook
Dim WS As Microsoft.Office.Interop.Excel.Worksheet
sub button1() WB = ExcelApp.Workbooks.Open("Path_To_File")
WS = WB.Worksheets("Sheet1")
'code to change some values here and there
WB.Save()
ExcelApp.Quit() end sub
The SaveAs works fine for me...
WB.SaveAs("filename.xls", True)