Error when activating a workbook

Myura

New Member
Joined
Feb 26, 2016
Messages
36
Hi!
I am trying to automate a painful process. I have a workbook that has a lot of sumifs for four countries. I will have to save workbook 4 times and value past certain ranges. I am trying to automate this process with following code.

Code:
Sub Hardcode_v1()
Dim z As String
Dim i As Integer
Dim w As String


z = ActiveWorkbook.Name


For i = 1 To 4


Sheets("T1").Select
Range("A1").Value = i


If Not Application.CalculationState = xlDone Then
    DoEvents
End If


    
    savename = Application.GetSaveAsFilename(fileFilter:="Exel Files (*.xlsx), *.xlsx")
     ActiveWorkbook.SaveCopyAs Filename:=savename
    
    
       
      Sheets("Exposures transition matrix").Select
                Range("c7:e17").Select
                Selection.Copy
                Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                    :=False, Transpose:=False
                Application.CutCopyMode = False


              
    Sheets("P11").Select
    Range("c12:o19").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False




    ActiveWorkbook.SaveAs Filename:=savename, FileFormat:=51
    
    
    Windows(z).Activate


           
    
    
    Next i
End Sub

I get an error at
Windows(z).Activate
. What would be the problem?

Thanks
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
deleted by Yongle
 
Last edited:
Upvote 0
I think the window you want is no longer there.
Code:
    ActiveWorkbook.SaveAs Filename:=savename, FileFormat:=51
saved the file under a different name thus renaming the active workbook :eek:
 
Last edited:
Upvote 0
I think the window you want is no longer there.
Code:
    ActiveWorkbook.SaveAs Filename:=savename, FileFormat:=51
saved the file under a different name thus renaming the active workbook :eek:

Thanks Yongle.

I cannot save as the workbook as my loop needs it to run for second country. That's why I chose to save a copy.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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