How to activate a window without crashing?

sharonng

New Member
Joined
Jul 14, 2021
Messages
18
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. Web
I want to activate a window to edit the content. However, when I use
VBA Code:
'open Aggregated Files
Workbooks.Open FileName:= _
    "J:\recoding\report_002\P09P05\Aggregated Files.xlsx", UpdateLinks:=0

'open D001A
Workbooks.Open FileName:= _
    "J:\recoding\report_002\P09P05\List of RI match with D001A in 2021Q2 SEV.xlsx", UpdateLinks:=0
    
'copy RI from D001A
Windows("List of RI match with D001A in 2021Q2 SEV.xlsx").Activate
Range("a1:c1692").Copy
            
'paste new quarter content to old quarter file and close the new quarter file
Windows("Aggregated Files.xlsx").Activate
Range("A1").PasteSpecial Paste:=xlPasteValues
Windows("List of RI match with D001A in 2021Q2 SEV.xlsx").Close

Windows("Aggregated Files.xlsx").Close
the program keeps running forever. What can I do?
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,917
Office Version
  1. 2019
Platform
  1. Windows
Hi,
untested but see if update to to your code does what you want

VBA Code:
Sub Sharonng()
    Dim FileNames(1 To 2)   As String
    Dim wbFiles             As Workbook, wbSEV As Workbook
  
    FileNames(1) = "J:\recoding\report_002\P09P05\Aggregated Files.xlsx"
    FileNames(2) = "J:\recoding\report_002\P09P05\List of RI match With D001A in 2021Q2 SEV.xlsx"
  
    Application.ScreenUpdating = False
    On Error GoTo myerror
    'open Aggregated Files
    Set wbFiles = Workbooks.Open(Filename:=FileNames(1), UpdateLinks:=0, ReadOnly:=False)
  
    'open D001A
    Set wbSEV = Workbooks.Open(Filename:=FileNames(2), UpdateLinks:=0, ReadOnly:=True)
  
    'copy RI from D001A
    wbSEV.Worksheets(1).Range("a1:c1692").Copy
  
    'paste new quarter content to old quarter file and close the new quarter file
    wbFiles.Worksheets(1).Range("A1").PasteSpecial Paste:=xlPasteValues
   
myerror:
 'clear clipboard
    Application.CutCopyMode = False

    'close & save if no error
    If Not wbFiles Is Nothing Then wbFiles.Close CBool(Err = 0)
    'close without saving
    If Not wbSEV Is Nothing Then wbSEV.Close False
  
    Application.ScreenUpdating = True
    'report errors
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub

Dave
 
Solution

sharonng

New Member
Joined
Jul 14, 2021
Messages
18
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. Web
Hi,
untested but see if update to to your code does what you want

VBA Code:
Sub Sharonng()
    Dim FileNames(1 To 2)   As String
    Dim wbFiles             As Workbook, wbSEV As Workbook
 
    FileNames(1) = "J:\recoding\report_002\P09P05\Aggregated Files.xlsx"
    FileNames(2) = "J:\recoding\report_002\P09P05\List of RI match With D001A in 2021Q2 SEV.xlsx"
 
    Application.ScreenUpdating = False
    On Error GoTo myerror
    'open Aggregated Files
    Set wbFiles = Workbooks.Open(Filename:=FileNames(1), UpdateLinks:=0, ReadOnly:=False)
 
    'open D001A
    Set wbSEV = Workbooks.Open(Filename:=FileNames(2), UpdateLinks:=0, ReadOnly:=True)
 
    'copy RI from D001A
    wbSEV.Worksheets(1).Range("a1:c1692").Copy
 
    'paste new quarter content to old quarter file and close the new quarter file
    wbFiles.Worksheets(1).Range("A1").PasteSpecial Paste:=xlPasteValues
  
myerror:
 'clear clipboard
    Application.CutCopyMode = False

    'close & save if no error
    If Not wbFiles Is Nothing Then wbFiles.Close CBool(Err = 0)
    'close without saving
    If Not wbSEV Is Nothing Then wbSEV.Close False
 
    Application.ScreenUpdating = True
    'report errors
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub

Dave
Thank you. It works except for Application.CutCopyMode = False and Application.ScreenUpdating = True.
 

Forum statistics

Threads
1,147,633
Messages
5,742,235
Members
423,716
Latest member
melpotter22

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
Top