Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving an updated excelfile 1

Status
Not open for further replies.

kalle82

Technical User
Apr 2, 2009
163
SE
Okay so now i have made a file for storing data that will be shown in the userform. My code also fetches and reads what it says, and displays it in my textboxes.

I have been trying to save these values somewhere else but as im a nooby programmer, ill have to stick with what i know..

Suggestions that helps me store without saving the data in an excelfile is more than welcome!

--QUESTION--
How do make my macro to save the excelfile before quitting the application?

--CODE--
Sub updateWorkbook()

Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
Dim ExcelWasNotRunning As Boolean
Dim WorkbookToWorkOn As String
Dim adresser As String
'specify the workbook to work on
WorkbookToWorkOn = "K:\Kassaregister\metafil.xls"

'If Excel is running, get a handle on it; otherwise start a new instance of Excel
On Error Resume Next
Set oXL = GetObject(, "Excel.Application")

If Err Then
ExcelWasNotRunning = True
Set oXL = New Excel.Application
End If

On Error GoTo Err_Handler

'If you want Excel to be visible, you could add the line: oXL.Visible = True here; but your code will run faster if you don't make it visible
oXL.Visible = True
'Open the workbook
Set oWB = oXL.Workbooks.Open(FileName:=WorkbookToWorkOn)

'Process each of the spreadsheets in the workbook
For Each oSheet In oXL.ActiveWorkbook.Worksheets
'put guts of your code here

Set oWB = oXL.Workbooks.Open(FileName:=WorkbookToWorkOn)
Set oSheet = oWB.Worksheets(1)

oSheet.Range("A1") = TextBox6
oSheet.Range("A2") = TextBox7


DoEvents

'Textlåda
'MsgBox "Adr = " & oSheet.Range("B2")


'get next sheet
Next oSheet

If ExcelWasNotRunning Then
oXL.Quit
End If




'Make sure you release object references.
Set oRng = Nothing
Set oSheet = Nothing
Set oWB = Nothing
Set oXL = Nothing

'quit
Exit Sub

Err_Handler:
MsgBox WorkbookToWorkOn & " caused a problem. " & Err.Description, vbCritical, _
"Error: " & Err.Number
If ExcelWasNotRunning Then
oXL.Quit
End If




End Sub
 
How do make my macro to save the excelfile before quitting the application
oWB.Save

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top