Code to Unfilter data from all sheets present in the Workbook - vba

I have a bunch of worksheets with only one table in each worksheet. I want to run a code that will unfilter/show all the data of all worksheet. so far i have written below code but it is not giving me the desire result.
dim ws1 as worksheet
On Error Resume Next
For Each ws1 In Worksheets
If ws1.FilterMode = True Then ws1.ShowAllData
Next ws1
On Error GoTo 0
Kindly review the above code and ammend.
Thanks
Salman

You may need to tackle this from the Range.Hidden Property point of view as well as the Worksheet.ShowAllData method. aspect. Data may have been hidden by other means than Range.AutoFilter Method.
Dim w As Long
For w = 1 To Worksheets.Count
With Worksheets(w)
.UsedRange.Cells.EntireRow.Hidden = False
If .AutoFilterMode Then .ShowAllData
End With
Next w

Use AutoFilterMode property..
Dim ws1 As Worksheet
On Error Resume Next
For Each ws1 In Worksheets
If ws1.AutoFilterMode = True Then ws1.AutoFilterMode = False
Next ws1
On Error GoTo 0

Try this code for your requirement.
Sub test1()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook
For Each ws In wb.Worksheets
If Not ws.AutoFilterMode = False Then
ws.AutoFilterMode = False
End If
Next
End Sub

Related

VBA - error when deleting multiple worksheet

I wrote the VBA code to delete multiple sheets. It can delete as my purpose but i got the error below when it complete the deleting
Here is my code:
Option Explicit
Sub deletesheet()
Dim wb As Workbook
Dim sh1 As Worksheet
Dim i As Long
Dim ws As Worksheet
Set wb = ThisWorkbook
Application.DisplayAlerts = False
'On Error Resume Next
For Each ws In ThisWorkbook.Worksheets
If ws.name <> "sheet1" Then
ws.Delete
End If
Next ws
On Error GoTo 0
Application.DisplayAlerts = True
End Sub
If I put "On Error Resume Next" it can skip this issue, but i really want to know why it happen this and how to solve it ? Can you please help look ?

Setting all selected sheets to same visible area

Attempting a macro that will set all selected sheets to have same cells visible as in the active sheet.
Example: if top-left cell is L76 on active sheet, then running this macro will set all selected worksheets to show L76 as the top left cell.
Cobbled this code together from examples found online but not sufficiently advanced in VBA to make it work.
Sub SetAllSelectedSheetsToSameRowColCell()
Dim rngSel As Range
Dim intScrollCol As Integer
Dim intScrollRow As Long
Dim oSheet As Object
If TypeName(Sh) = "Worksheet" Then
Set oSheet = ActiveSheet
Application.EnableEvents = False 'Unsure what this line is for
Sh.Activate
With ActiveWindow
intScrollCol = .ScrollColumn
intScrollRow = .ScrollRow
Set rngSel = .RangeSelection
End With
oSheet.Activate
Application.EnableEvents = True
End If
'Loop thru rest of selected sheets and update to have same cells visible
Dim oWs As Worksheet
For Each oWs In Application.ActiveWindow.SelectedSheets
On Error Resume Next
oWs.Range(rngSel.Address).Select
.ScrollColumn = intScrollCol
.ScrollRow = intScrollRow
Next
End Sub
References:
https://excel.tips.net/T003860_Viewing_Same_Cells_on_Different_Worksheets.html
VBA Macro To Select Same Cell on all Worksheets
Try this:
Sub ResetAllSheetPerspectives()
Dim ws As Worksheet
Dim lRow As Long
Dim lCol As Long
Dim dZoom As Double
lRow = ActiveWindow.ScrollRow
lCol = ActiveWindow.ScrollColumn
dZoom = ActiveWindow.Zoom
For Each ws In Application.ActiveWindow.SelectedSheets
ws.Activate
ActiveWindow.Zoom = dZoom
Application.Goto ws.Cells(lRow, lCol), True
Next ws
End Sub
Maybe this will help. Sets the top left cell of other sheets depending on the first sheet.
Sub Macro1()
Dim r As Range, ws As Worksheet
Sheets(1).Activate
Set r = ActiveWindow.VisibleRange.Cells(1)
For Each ws In Worksheets
If ws.Index > 1 Then
ws.Activate
ActiveWindow.ScrollRow = r.Row
ActiveWindow.ScrollColumn = r.Column
End If
Next ws
End Sub
This procedure sets the same visible range as the active worksheet for all selected worksheets. It excludes any Chart sheet in the selection and adjusts the zoom of the selected sheets to ensure all worksheets have the same visible area.
Sub SelectedWorksheets_ToSameVisibleRange()
Dim ws As Worksheet
Dim oShs As Object, oSh As Object
Dim sRgAddrs As String
On Error Resume Next
Set ws = ActiveSheet
On Error GoTo 0
If ws Is Nothing Then
MsgBox "Active sheet must be a worksheet type" & String(2, vbLf) _
& String(2, vbTab) & "Process will be cancelled.", _
vbCritical, "Worksheets Common Range View"
Exit Sub
End If
With ActiveWindow
Set oShs = .SelectedSheets
sRgAddrs = .VisibleRange.Address 'Get address of Active Sheet visible range
End With
For Each oSh In oShs
If TypeName(oSh) = "Worksheet" And oSh.Name <> ws.Name Then 'Excludes any chart sheet and the active sheet
With oSh.Range(sRgAddrs)
Application.Goto .Cells, 1 'Activate Worksheet targeted visible range
ActiveWindow.Zoom = True 'Zoom Worksheet to make visible same range as the "active worksheet"
Application.Goto .Cells(1), 1 'Activate 1st cell of the visible range
End With: End If: Next
ws.Select 'Ungroups selected sheets
End Sub

VBA - Excel Forces Restart

I am having a major issue with my code that is supposed to reset the worksheets, not shut down the entire workbook and force a restart. This has not been an issue, and has only occured since I added the last bit of code starting at On Error Resume Next.
Sub Reset()
Application.ScreenUpdating = False
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name = "Set Up" Or ws.Name = "Report" Then
Else:
Application.DisplayAlerts = False
ws.Delete
End If
Next
Worksheets("Report").Cells.ClearContents
On Error Resume Next
Application.DisplayAlerts = False
ThisWorkbook.Charts.Delete
Application.DisplayAlerts = True
On Error GoTo 0
End Sub
Thanks in advance
If you have chart sheets, then you should use the following code:
Sub DeleteChartSheets()
Dim ch As Chart
For Each ch In ThisWorkbook.Charts
ch.Delete
Next
End Sub
this deletes the charts in a workhseet, say activesheet for example:
Sub DeleteallCharts()
Dim chtObj As ChartObject
For Each chtObj In ActiveSheet.ChartObjects
chtObj.Delete
Next
End Sub
if you want to delete all of the charts in the workbook, then you have to loop through the worksheet too like this:
Sub DeleteallChartsInWorkbook()
Dim chtObj As ChartObject
Dim WS As Worksheet
For Each WS in Thisworkbook.Worksheets
For Each chtObj In WS.ChartObjects
chtObj.Delete
Next chtObj
Next WS
End Sub

Worksheet CodeName not assigned

I'm trying to assign the code name of a worksheet to a vriable.
Some times it gets the code name correctly, and sometimes it doesn't, the variable stays null.
Dim sCodeName As String
sCodeName = Worksheets(atar).CodeName
atar is a variable that contains the worksheet name.
When i stop the code running, and coninue in debug mode, it works fine.
What can be the reason?
Worksheets without qualification refers to same as ActiveWorkbook, perhaps you're assuming too much about which workbook is active in some instances.
It is recommended to fully qualify Worksheets. So get a reference to a Workbook object , say wbFoo and then use sCodeName = wbFoo.Worksheets(atar).CodeName
You could try something like the code below to verify that atar does exist in one of the worksheet names in ThisWorkbook.
Option Explicit
Sub GetWorksheetCodeName()
Dim sCodeName As String
Dim atar As String
Dim Sht As Worksheet
'atar = "Sheet3" '<-- for tests only
' loop through all worksheets in ThisWorkbook
For Each Sht In ThisWorkbook.Worksheets
If Sht.Name Like atar Then
sCodeName = Worksheets(atar).CodeName
Exit For
End If
Next Sht
End Sub
CREATE MULTIPLE WORKSHEET BUT SOMETIMES NOT WORK WITH MACRO FROM WORKSHEET ONLY WORK WITH VBA CODE RUN
Sub WSCreate()
Dim WSA As String
Dim WSB As String
Dim WS As Worksheet
WSA = "SheetA"
WSB = "SheetB"
Application.DisplayAlerts = False
On Error Resume Next
'DELETE MULTIPLE WORKSHEETS (IF Already Exist)
Set WS = Nothing
Set WS = Sheets(WSA)
WS.Delete
Set WS = Nothing
Set WS = Sheets(WSB)
WS.Delete
Set WS = Nothing
'CREATE MULTIPLE WORKSHEETS
With Application.ThisWorkbook
.Worksheets.Add.Name = WSA 'WSA = SheetA
.VBProject.VBComponents(Worksheets(WSA).CodeName).Name = WSA
.Worksheets.Add.Name = WSB 'WSB = SheetB
.VBProject.VBComponents(Worksheets(WSB).CodeName).Name = WSB
End With
If Err <> 0 Then Exit Sub
End Sub

VBA Deleting blank worksheets where the worksheets have been renamed in Spanish?

I am working with a file where the worksheets have been renamed. Instead of Sheet1(generic name) it is Hoja1(generic name).
Wondering if this is stopping my code from working.
My code is very simple. I dont know what other error I could be having.
Sub Macro1()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Application.DisplaysAlerts = False
If LenB(ActiveSheet.Range("A5")) = "" Then ActiveSheet.Delete
Application.DisplayAlerts = True
Next ws
End Sub
Thank you.
If it's definitely going to have "Hoja" in the sheet name, this should work.
Sub Macro1()
Dim WS As Worksheet
Application.DisplayAlerts = False
For Each WS In ActiveWorkbook.Sheets
If InStr(WS.Name, "Hoja") <> 0 Then WS.Delete
Next
Application.DisplayAlerts = True
End Sub
Got some help from someone.
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Application.DisplayAlerts = False
If LenB(ws.Range"A5")) = 0 Then ws.Delete
Application.DisplayAlerts = True
Next ws
The spanish wasn't the issue.
To get the name Hoja1 if the sheet appears as Hoja1(1) in the VBA browser you can use the .CodeName property:
'code borrowed from Alex's answer
Sub Macro1()
Dim WS As Worksheet
Application.DisplayAlerts = False
For Each WS In ActiveWorkbook.Sheets
If InStr(WS.CodeName, "Hoja") <> 0 Then WS.Delete
Next
Application.DisplayAlerts = True
End Sub