Iterating through Excel sheets - vba

Here is my code. I'm new to VBA so, I am unsure how to iterate through multiple pages.
Here's my code:
Dim ws As Worksheet
Sub spellCheck()
For Each ws In ActiveWorkbook.Worksheets
Cells.CheckSpelling
Next
End Sub

Try this (this will simply activate each sheet):
Sub spellCheck()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Activate
' Do stuff...
Next
End Sub

Related

Refreshing Two Pivot Tables on a Sheet whenever the sheet is selected

I have two PivotTables on a sheet that I would like to have refreshed automatically whenever the sheet is open. I've put that macro this macro within that sheet. However, I am getting an error on this line "oPivot.RefreshTable"
Private Sub Worksheet_Activate()
Dim oSheet As Worksheet
Dim oPivot As PivotTable
Set oSheet = ActiveSheet
For Each oPivot In oSheet.PivotTables
oPivot.RefreshTable
Next oPivot
End Sub
This method works:
Private Sub Worksheet_Activate()
Dim oPivot As PivotTable
Dim xlsheet As Worksheet
Set xlsheet = ActiveSheet
For Each oPivot In xlsheet.PivotTables
oPivot.PivotCache.Refresh
Next oPivot
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

Edit all worksheets except Sheet1

I've got a script that deletes rows 1-4 on on every worksheet, but would like it to skip a worksheet if its name is "Sheet1"
Sub RowDelete()
Dim xWs As Worksheet
Set xWs = ActiveSheet
ThisWorkbook.Worksheets.Select
Rows("1:4").Select
Selection.Delete
xWs.Select
End Sub
Run it through a FOR EACH Loop:
Sub RowDelete()
Dim xWs As Worksheet
For Each xWs In Worksheets
If xWs.Name <> "Sheet1" Then
xWs.Rows("1:4").Delete
End If
Next xWs
End Sub
Btw, try to learn coding without using .Select.

How can I set a worksheet object based on a worksheet's codename?

The worksheet name may change. Because of that I want to set the worksheet object based on the worksheet's codename. How can I do this?
My best attempt so far is:
Sub UpdateNameDropdown()
Dim wksName As String
wksName = ThisWorkbook.Sheets(Sheet16).Name
Dim wks As Worksheet
Set wks = Sheets(wksName)
End Sub
But I get a type mismatch error on the row wksName = ThisWorkbook.Sheets.Sheet16.Name
This?
Sub Sample()
Dim wks As Worksheet
Set wks = Sheet16
With wks
Debug.Print .Name
'~~> Do what you want
End With
End Sub
This uses the codename as a String
Sub CodeIt()
Dim CodeName As String
CodeName = "Sheet1"
Dim WS As Worksheet, GetWorksheetFromCodeName As Worksheet
For Each WS In ThisWorkbook.Worksheets
If StrComp(WS.CodeName, CodeName, vbTextCompare) = 0 Then
Set GetWorksheetFromCodeName = WS
Exit For
End If
Next WS
MsgBox GetWorksheetFromCodeName.Name
End Sub
Thank you Gary's Student. I midified your function to the following:
Function GetWorksheetFromCodename(codeName As String) As Worksheet
Dim wks As Worksheet
For Each wks In ThisWorkbook.Worksheets
If StrComp(wks.codeName, codeName, vbTextCompare) = 0 Then
Set GetWorksheetFromCodename = wks
Exit For
End If
Next wks
End Function

Excel VBA loop & cell value match

What I am trying to do seems basic enough, however I don't know where I am going wrong with the code.
I want to run the selected cell through a loop of the worksheets and select the worksheet that matches the selected cell located in cell B1.
Dim SelectedCell as Range
Dim ws As Worksheet
Set SelectedCell = Range(ActiveCell.Address)
For Each ws In ActiveWorkbook.Worksheets
If ws.Range("B1").Value = SelectedCell.Value Then
ActiveSheet.Select
End If
Next ws
End Sub
Thanks in advance for all the help!
Try instead
Dim ws As Worksheet
SelectedCell = ActiveCell
For Each ws In ActiveWorkbook.Worksheets
If ws.cells(1,2) = SelectedCell Then
ws.Select
End If
Next ws
End Sub
Select cell run macro will select the sheet name that matches the selected cell. (Case sensitive)
Dim SelectCell As String
Dim ws As Worksheet
SelectCell = ActiveCell.Value2
For Each ws In ActiveWorkbook.Worksheets
If ws.Name = SelectCell Then
ws.Select
End ID
Next ws