I am trying to create a new workbook based on the formatting as that of an existing excel file and i am using the below code
sub newWorkBook()
Dim newWorkBook as New WorkBook
Set newWorkBook = WorkBooks.Add("Template.xls")
/*It is erroring on the above line*/
newWorkBook.Application.DisplayAlerts = False
With newWorkBook
.SaveAs "C:\new.xls"
End With
domainWorkBook.Save
domainWorkBook.Application.DisplayAlerts = True
domainWorkBook.Close
Set newWorkBook = Nothing
End Sub
It errors on the line Set newWorkBook = WorkBooks.Add("Template.xls") saying subscript out of range..
It also opens a new excel named Template1.xls and throws error...Any suggestions?
Why not just open the Template and SaveAs a new name?
Sub qwerty()
Workbooks.Open "C:\TestFolder\Template.xls"
ActiveWorkbook.SaveAs "C:\TestFolder\new.xls"
ActiveWorkbook.Close
End Sub
Related
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
Dim wkbk As Workbook
Dim NewFile As Variant
NewFile = Application.GetOpenFilename(FileFilter:="Microsoft Excel Files
(*.xlsx; *.xls), (*.xlsx; *.xls), All Files, *.*", FilterIndex:=1)
If NewFile <> False Then
Set wkbk = Workbooks.Open(NewFile)
End If
Worksheets("Sunday").Unprotect "bunny"
Dim Sh As Worksheet
For Each Sh In ThisWorkbook.Worksheets
If Sh.Visible = True Then
Sh.Activate
Sh.Cells.Copy
Sh.Range("A1").PasteSpecial Paste:=xlValues
Sh.Range("A1").Select
End If
Next Sh
Application.CutCopyMode = False
Worksheets("Sunday").Protect "bunny", True, True
Application.ScreenUpdating = True
End Sub
Hello, pretty knew to VBA, can do tiny thing in it. I'm trying to make something which allows you to select an excel file to open, opens it, un-protects the sheet "Sunday", pastes over the data, re-protects, then saves and closes the document.
The main bit of code works on my current sheet if I don't include the portion to open a file, but when I am trying to use it on the workbook I have just opened, I am getting a 'Subscript out of range' error message. I am not too sure if I am the newly opened file is 'active', but when I tried insert a line of vba to make the new document active, it made no difference. Any advice would be appreciated.
I looked here and tried many solutions but could not get my code run as expected.
I have one macro enabled workbook with one module on a sheet named "Original"
I created have a second sheet as master where a add a button to copy the the "original" sheet and save the copy as macro enabled.
When I open the copied file the macro still refer to the initial file. I want the macro be just in the copied file because I cannot distribute the source file to the users.
Following is my code
==>How i copy the file
Sub createNew(fineName As String)
Dim mybook As Workbook
Set mybook = ThisWorkbook
Set newBook = Workbooks.Add
mybook.Sheets("Original").Copy Before:=newBook.Sheets(1)
Set newWs = newBook.Sheets("Original")
newWs.Name = Left(fineName, 30)
End Sub
==>How I save the file
Sub savefile(fname As String, compid As Long)
fname = "PS_" & fname
Set newBook = ActiveWorkbook
Application.DisplayAlerts = False
newBook.SaveAs fname, FileFormat:=xlOpenXMLWorkbookMacroEnabled
Application.DisplayAlerts = True
closefile newBook
End Sub
I will appreciate your help!
Thank you all. I could solve the problem. Thanks to your comments I realized I had I had to copy the module to the newly copied file
Sub CopyOneModule()
Dim FName As String
With Workbooks("Book2")
*** FName = .Path & "\code.txt"
*** .VBProject.VBComponents("Module1").Export FName
End With
Workbooks("book1").VBProject.VBComponents.Import FName
End Sub
I have a macro that opens a new Workbook and then activate (focus) to first Workbook.
Code:
Set mainWorkbook = ActiveWorkbook
Set bdWorkbook = Workbooks.Open(FileName:="Another.xlsm", ReadOnly:=True)
mainWorkbook.Activate
I've got this code working in Excel 2007, but I've encountered an issue with the open workbook in Excel 2010 and later. The problem happens because Workbooks.Open returns to VBA before Excel Activate the new workbook [it works fine using debugger].
I can make an workarround using a Application.Wait (Now + TimeValue("0:00:01")), but......
EDIT: My code that dosen't work in Excel 2016
Sub Sample()
Dim path As String
path = "A_PATH_FROM_MY_SERVER"
actualScreenUpdate = Application.ScreenUpdating
Application.ScreenUpdating = False
Set MainWB = ActiveWorkbook
Workbooks.Open fileName:=path, UpdateLinks:=False, ReadOnly:=isReadOnly
Set bdWB = ActiveWorkbook
DoEvents
MainWB.Activate
Application.ScreenUpdating = actualScreenUpdate
With Sheets(MY_BD_SHEET)
bdWB.ActiveSheet.UsedRange.Copy .[A1]
'....
End With
End Sub
Is this what you want? (Tried And Tested)
This will open the relevant workbook and minimize it thereby returning focus to your main workbook.
Sub Sample()
Dim wbThis As Workbook, wbThat As Workbook
Set wbThis = ThisWorkbook
Set wbThat = Workbooks.Open("C:\Users\Siddharth\Desktop\Sample.xlsx")
DoEvents
Application.WindowState = xlMinimized
' OR
ActiveWindow.WindowState = xlMinimized
End Sub
EDIT
After seeing your current Edit.
MainWB.Activate
Application.ScreenUpdating = actualScreenUpdate
You are activating when the ScreenUpdating = False? Set it to True and then Activate it :)
From a workbook, I am attempting to open another workbook, duplicate the master worksheet in that workbook and rename it. The problem being that no matter what I try it doesn't seem to work when I Copy the master sheet.
Attempt One - Using the copy method.
Sub individualStats()
'Initialize
Dim app As New Excel.Application
app.Visible = False
Dim objWorkbook As Excel.Workbook
Set objWorkbook = app.Workbooks.Add("S:\MH\Stats\Jordan Individual Stats.xlsm")
'Test if Worksheet exists already
Set wsTest = Nothing
On Error Resume Next
Set wsTest = objWorkbook.Worksheets("Test Worksheet")
On Error GoTo 0
'If worksheet does not exist then duplicate Master and rename
If wsTest Is Nothing Then
objWorkbook.Worksheets("Master").Copy After:=objWorkbook.Worksheets(Worksheets.count)
' ^ This is the line I get the error on.
ActiveSheet.Name = "Test Worksheet"
End If
'Save and close workbook.
app.DisplayAlerts = False
objWorkbook.SaveAs Filename:="S:\MH\Stats\Jordan Individual Stats.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
objWorkbook.Close SaveChanges:=False
app.Quit
Set app = Nothing
app.DisplayAlerts = True
End Sub
I have marked the line I get the error on. The error is
"Run-time error '9': Subscript out of range."
Attempt Two - Calling a Macro from the Workbook
Inside of the "Jordan Individual Stats.xlsm" workbook I have created this Macro.
Sub duplicateMaster()
Sheets("Master").Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = "Test Worksheet"
End Sub
This sub works completely fine if I run it within that workbook.
But when I try to call this module from the original workbook it doesn't work.
...
If wsTest Is Nothing Then
Application.Run ("'S:\MH\Stats\Jordan Individual Stats.xlsm'!duplicateMaster")
End If
...
The error comes up on the line in the duplicateMaster module on the line "Sheets("Master").Copy After:=Sheets(Sheets.Count)".
The error is the same "Run-time error '9': Subscript out of range."
How can I fix this?
objWorkbook.Worksheets("Master").Copy _
After:=objWorkbook.Worksheets(Worksheets.count)
here Worksheets.count will refer to the active workbook, but not in the new instance of Excel you created. It will instead refer to the active workbook in the instance where your code is running.
Try this:
objWorkbook.Worksheets("Master").Copy _
After:=objWorkbook.Worksheets(objWorkbook.Worksheets.count)
You don't need to create a new instance of Excel to do this, and not doing so will prevent this type of easily-overlooked problem.
I am trying to copy sheets from VBA. Here is my code:
'Create a new excel workbook and copy two result sheets.
Public Function copyAsSaveResultSheets(filePath As String)
'Remove alert for confirmations
Application.DisplayAlerts = False
'Get new work book
Dim newWorkBook As Workbook
Set newWorkBook = Workbooks.Add
'Copy result sheet
'When click "Debug" cursor is here.
ThisWorkbook.Sheets("ResultProcess").Copy Before:=newWorkBook.Sheets(1)
ThisWorkbook.Sheets("ResultCode").Copy Before:=newWorkBook.Sheets(1)
'Delete default sheet
For Each sheet In Worksheets
Select Case sheet.Name
Case "Sheet1", "Sheet2", "Sheet3"
sheet.Delete
End Select
Next
'Save work book
newWorkBook.SaveAs filePath
'Close new work book
ActiveWorkbook.Close
'Available showing alert from application
Application.DisplayAlerts = True
End Function
When running this code, I get this error:
I didn't find any error in my code. The "ResultProcess" sheet and "ResultCode" sheet are protected according to requirement. Does this matter?