Unhide/Hide a sheet which uses VBA Code

Maya Maile

New Member
Joined
Jan 13, 2022
Messages
2
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. Mobile
Hi All,

I am having a recorded macro on a excel sheet.
I have a button on another sheet to run this macro.
When I run the macro it says the following error.
1642493829920.png


I want to add two lines. First to unhide the sheet where the macro is running and second hide it automatically.

(Recorded Macro Code)
VBA Code:
Sub DeleteDuplicates()
'
' DeleteDuplicates Macro
'

'
    Sheets("ABCTable").Visible = True
    Columns("H:H").Select
    Selection.Copy
    Range("I1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveSheet.Range("$I$1:$I$1048568").RemoveDuplicates Columns:=1, Header:= _
        xlNo
    Range("I3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Range("A3000").Select
    Selection.End(xlUp).Select
    Range("A3").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Sorting
    Sheets("ABCTable").Visible = False
End Sub


Thanks in advance.
 
Last edited by a moderator:

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try it
VBA Code:
Sub DeleteDuplicates()
'
' DeleteDuplicates Macro
'

If Sheets("ABCTable").Visible = False Then Sheets("ABCTable").Visible = True
With Sheets("ABCTable")
    .Range("H1:H" & .Range("H" & Rows.Count).End(3).Row).Copy
    .Range("I1").Paste
    .Range("I1:I" & .Range("I" & Rows.Count).End(3).Row).RemoveDuplicates Columns:=1, Header:= _
    xlNo
    .Range("I1:I" & .Range("I" & Rows.Count).End(3).Row).Copy
    .Range("A3").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Application.CutCopyMode = False
    Range("A3").Sort , xlAscending
End With
Sheets("ABCTable").Visible = False
End Sub
 
Upvote 0
I want to add two lines. First to unhide the sheet where the macro is running and second hide it automatically.
But surely that is what you have done already with these two lines.
VBA Code:
Sheets("ABCTable").Visible = True

   Sheets("ABCTable").Visible = False
Is the sheet protected?
 
Upvote 0
@kokano90
I had to make a couple of adjustments to get the code to run.
Changed the Paste to I1 to be:-
.Range("I1").PasteSpecial Paste:=xlPasteAll
Changed the sort line to be:
.Range("A3").Sort key1:=.Range("A3"), order1:=xlAscending

The code will run on a hidden sheet, so not sure that the unhide/hide really serves any purpose.
 
Upvote 0

Forum statistics

Threads
1,215,232
Messages
6,123,763
Members
449,120
Latest member
Aa2

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