Switching Views Between Windows

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,566
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I feel a bit silly asking this as it should be one of the more basic Excel VBA commands.

I have an open maximized workbook (wb1). VBA code within this workbook creates a copy of one of it's worksheets to a new workbook (wb2) through a userform (uf1) and saves it.
After wb2 is saved, it remains open, but it's not visible. I have to manually switch windows.

How can I keep the userform open but with wb2 behind it rather than wb1?

I thought I could simply use ...
Rich (BB code):
wb2.activate
but this visibly did nothing.

Note: wb1 is open with two userforms with modal properties set to false.

Rich (BB code):
Private Sub uf2_create_Click()
    Dim f_range As Range
    Dim ui1 As String
    
    With ws_salt
        .AutoFilterMode = False 'turn off autofilter if on
        f_lr = ws_sheet2.Cells(i, 11)
        f_ur = ws_sheet2.Cells(i, 12)
        last_col = .Range("A1").CurrentRegion.Columns.Count
        Set f_range = .Range(.Cells(2, 1), .Cells(2, last_col))
        With f_range
            .AutoFilter field:=1, Criteria1:=">=" & f_lr, Operator:=xlAnd, Criteria2:="<=" & f_ur
        End With

   ...
    
    m1 = Application.WorksheetFunction.Match(uf2_srf.uf2cb_worange.Value, ws_sheet2.Range("O2:O21"), 0)
    rng_low = Application.WorksheetFunction.Index(ws_sheet2.Range("K2:O21"), m1, 1)
    rng_hi = Application.WorksheetFunction.Index(ws_sheet2.Range("K2:O21"), m1, 2)
    sname = Format(rng_low, "ddmmmyy") & "-" & Format(rng_hi, "ddmmmyy")
    
    Application.ScreenUpdating = False
    With ws_srf
        .Visible = True
        .Copy
        .Visible = False
    End With
    Application.ScreenUpdating = True
    ActiveSheet.Name = sname
    Set ws_tsrf = Worksheets(sname)
    
    With ws_tsrf
        .Unprotect
        sname = "SRF_WP_" & sname & ".xlsm"
        fname = "H:\Materials Tracking\Reports\" & sname
TryAgain:
        ui1 = Application.InputBox("Please enter user's initials:", "REQUIRED INFORMATION", "INITIALS")
        If Len(ui1) < 2 Or Len(ui1) > 3 Then
            MsgBox "Enter proper user initials (2-3 characters)."
            GoTo TryAgain
        End If
        un1 = UCase(un1)
        .Range("AB1") = uf2_srf.uf2cb_worange.Value
        .Range("B32") = "   Waterloo Park (" & un1 & ")"
        
        tr = 8
        nr = 0
        For i = 51 To 72
            If ws_sheet2.Cells(i, 3) > 0 Then
                .Cells(tr - 1, 1) = "90000"
                .Cells(tr - 1, 3) = "Salt"
                .Cells(tr - 1, 2) = ws_sheet2.Cells(i, 3)
                .Cells(tr - 1, 4) = ui1
                wo = ws_sheet2.Cells(i, 2)
                    .Cells(tr, 11) = Right(wo, 1)
                    .Cells(tr, 10) = Mid(wo, 5, 1)
                    .Cells(tr, 9) = Mid(wo, 4, 1)
                    .Cells(tr, 8) = Mid(wo, 3, 1)
                    .Cells(tr, 7) = Mid(wo, 2, 1)
                    .Cells(tr, 6) = Left(wo, 1)
                nr = nr + 1 'change to 1
                If nr = 12 Then 'add page
                    .Rows("1:33").Copy .Range("34:34")
                    .Range("A41:AN64") = ""
                    tr = 41
                Else
                    tr = tr + 2
                End If
            End If
        Next i
        .Protect
        
        MsgBox "New report visible behind this window.", vbExclamation, "REPORT COMPLETED"
        uf2_srf.uf2_create.Enabled = False
        .SaveAs Filename:=fname, FileFormat:=XlFileFormat.xlOpenXMLWorkbookMacroEnabled
        Set wb_tsrf = Workbooks(sname)
        wb_tsrf.Activate
    End With
    
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Forum statistics

Threads
1,223,707
Messages
6,174,000
Members
452,542
Latest member
Bricklin

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