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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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 :eeek:
 
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 :eeek:

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,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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