macro closing when it should'nt

dmheller

Board Regular
Joined
May 26, 2017
Messages
156
Office Version
  1. 365
I have this and it runs when the workbook is open but it closes after it runs. Any help would be great thanks.
Private Sub Workbook_open()

Dim path As String
Dim filename1 As String

' copy and paste
Sheets("Sheet1").Cells.Copy
Sheets("Sheet1").Cells.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False

'end copy and paste

path = "C:\Users\hellerda\Desktop\test"
filename1 = Range("C9").Text
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=path & filename1 & "nox.xlsx", FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True


End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Re: macro closing when it shoundt

.
Aside from when the workbook is opened .... at what other time/s do you want the macro to run ?
 
Upvote 0
Re: macro closing when it shoundt

I guess really only when someone wants to run it. In the pig picture, I am wanting to have task scheduler open it, then I have to put a hold on it before it runs then it runs and I would like the new file that it makes emailed out everyday. I guess I would like a command button too for email the file when someone wants an update.
 
Upvote 0
Re: macro closing when it shoundt

.
To run the macro when you want, after the workbook opens ...

Paste this macro in the ThisWorkbook module :

Code:
Option Explicit


Private Sub Workbook_Open()
    cpyPaste
End Sub


Paste this macro in a regular module :

Code:
Option Explicit


Sub cpyPaste()
Dim path As String
Dim filename1 As String


' copy and paste
Sheets("Sheet1").Cells.Copy
Sheets("Sheet1").Cells.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False


'end copy and paste


path = "C:\Users\hellerda\Desktop\test"
filename1 = Range("C9").Text
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=path & filename1 & "nox.xlsx", FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True
End Sub

Then place a CommandButton on a worksheet and connect it to the cpyPaste macro.
 
Upvote 0
Re: macro closing when it shoundt

Ok, sorry to be new at this VBA but how would I connect the commabdbutton to the cpypaste macro? I know it will give me a new sub and a new end sub. do I just delete that or what would it be.
 
Upvote 0
Re: macro closing when it shoundt

[h=2]Insert VBA code to Excel Workbook[/h][FONT=&quot]For this example, we are going to use a VBA macro to remove line breaks from the current worksheet.[/FONT]

  1. Open your workbook in Excel.
  2. Press Alt + F11 to open Visual Basic Editor (VBE).
    visual-basic-editor.png
  3. Right-click on your workbook name in the "Project-VBAProject" pane (at the top left corner of the editor window) and select Insert -> Module from the context menu.
    insert-vba-module.png
  4. Copy the VBA code (from a web-page etc.) and paste it to the right pane of the VBA editor ("Module1" window).
    add-vba-code.png
  5. After the above, close the VBE and go back to the worksheet. Insert a CommandButton on the sheet. Once you place the button on the sheet
  6. a small window will open with all macros listed. Click on the macro you just pasted then click OK. You are done.
  7. When you save the workbook with the macro, be sure to save it as 'macro enabled'.
  8. Resource : https://www.ablebits.com/office-addins-blog/2013/12/06/add-run-vba-macro-excel/
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top