Sub won't End

skull_eagle

Board Regular
Joined
Mar 25, 2011
Messages
89
Hi,

I'm using the attached sub to copy visible cells to a new sheet.

The problem is I want to display the new sheet for the user but as soon as I make it active or minimize the original sheet the sub won't end.

I have to access the code and manually push the stop button.

VBA Code:
Private Sub Display_BTN_Click()

Dim OrWS As Workbook

Set OrWS = ThisWorkbook


    Sheets.Add After:=ActiveSheet
    ActiveSheet.Name = "Results"
    Sheets("RawData").Select
    ActiveSheet.ListObjects("Table_RawData").Range.Select
    Selection.SpecialCells(xlCellTypeVisible).Copy
    Sheets("Results").Select
    Range("A1").Select
    ActiveSheet.Paste
    Cells.EntireColumn.AutoFit
    
    Columns("CB:CB").Delete Shift:=xlToLeft
    Columns("CA:CA").Delete Shift:=xlToLeft
    Columns("B:B").Delete Shift:=xlToLeft
    Columns("A:A").Delete Shift:=xlToLeft
    
    
    Range("A1").Select
    Sheets("Results").Move
    
OrWS.Activate
Sheets("RawData").Select
ActiveWindow.WindowState = xlMinimized


End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Maybe try it as below:
VBA Code:
Private Sub Display_BTN_Click()
    Dim wsResult As Worksheet

    Set wsResult = Sheets.Add(, Sheets(Sheets.Count))
    
    With wsResult
        .Name = "Results"
        Sheets("RawData").ListObjects("Table_RawData").Range.SpecialCells(xlCellTypeVisible).Copy
        .Range("A1").PasteSpecial xlPasteValues
        .Cells.EntireColumn.AutoFit
        .Columns("CA:CB").Delete xlToLeft
        .Columns("A:B").Delete xlToLeft
        .Move
    End With
    ActiveWindow.WindowState = xlMinimized
End Sub
 
Upvote 0
Dim wsResult As Worksheet Set wsResult = Sheets.Add(, Sheets(Sheets.Count)) With wsResult .Name = "Results" Sheets("RawData").ListObjects("Table_RawData").Range.SpecialCells(xlCellTypeVisible).Copy .Range("A1").PasteSpecial xlPasteValues .Cells.EntireColumn.AutoFit .Columns("CA:CB").Delete xlToLeft .Columns("A:B").Delete xlToLeft .Move End With ActiveWindow.WindowState = xlMinimized

Thank you for the help, much neater code.

Unfortunately it didn't help with the original problem.

New sheet now minimizes
Sub still doesn't End
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,435
Members
448,961
Latest member
nzskater

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