I want to have a button replace the info on MOST sheets with the info from a master sheet when a button is clicked. However, I want it to skip some sheets.
I have the below code that works, but there are 2 sheets I want it to skip when running. How can I specify that is skip the sheets named "Dates" and "Monthly"
Sub Button4_Click()
Dim wsVar As Worksheet
For Each wsVar In ThisWorkbook.Sheets
With wsVar
.Range("B9:M30").Value = Worksheets("BASE").Range("B9:M30").Value
End With
Next wsVar
End Sub
You can use an IF statement to check the name of the worksheet and then act accordingly.
For Each wsVar In ThisWorkbook.Sheets
If wsVar.Name = "foo" Or wsVar.Name = "bar" Then
' do nothing
Else
With wsVar
.Range("B9:M30").Value = Worksheets("BASE").Range("B9:M30").Value
End With
End If
Next wsVar
This code will exclude all sheets listed in the Exclude array. Note that sheets name comparisons are carried out case-insensitive and leading or trailing blanks are deemed unintentional and removed.
Sub Button4_Click()
Dim wsVar As Worksheet
Dim Exclude() As String
Dim i As Integer
Exclude = Split("Dates,Monthly", ",")
For Each wsVar In ThisWorkbook.Worksheets
With wsVar
For i = UBound(Exclude) To 0 Step -1
If StrComp(wsVar.Name, Trim(Exclude(i))) = 0 Then Exit For
Next i
If i >= 0 Then .Range("B9:M30").Value = Worksheets("BASE").Range("B9:M30").Value
End With
Next wsVar
End Sub
With a small change, you might convert this same function to process only listed sheets. To identify sheets for action positively, rather than negatively, is the safe way, generally speaking. But if you decide to do that, please also do rename the array :-)
Related
I have an Excel workbook with several worksheets.
I would like the macro to look at the value of cell "A1" of each worksheet.
If the cell value is less than 8, A1 must be adjusted to 8.
If the cell value is greater than 8, nothing needs to be adjusted.
I have two macros:
Sub LoopCertain() 'Excel VBA to exclude sheets(1-3)
Dim sh As Worksheet
For Each sh In Sheets
Select Case sh.Name
Case Is = "Blad1", "Blad2", "Blad3"
'No Code here if excluded
Case Else
Call X2
End Select
Next sh
End Sub
and
Sub X2()
'declare a variable
Dim ws As Worksheet
Set ws = ActiveSheet
'calculate if a cell is less than a specific value
If ws.Range("A1") < 8 Then
ws.Range("A1") = 8
Else
End If
End Sub
The problem is that only the active worksheet is done and the rest of the worksheets are not looked at. The macro also does not check whether the worksheet should not be included.
If you want using two subs, please try the next way. Your code only use the active sheet in the second sub:
Sub LoopCertain() 'Excel VBA to exclude sheets(1-3)
Dim sh As Worksheet
For Each sh In Sheets
Select Case sh.name
Case "Blad1", "Blad2", "Blad3"
'No Code here if excluded
Case Else
Call X2(sh)
End Select
Next sh
End Sub
Sub X2(ws As Worksheet)
'calculate if a cell is less than a specific value
If ws.Range("A1").value < 8 Then ws.Range("A1") = 8
End Sub
But for such a simple processing, no need of the second since everything can be done in the first one:
Sub LoopCertain() 'Excel VBA to exclude sheets(1-3)
Dim sh As Worksheet
For Each sh In Sheets
Select Case sh.name
Case "Blad1", "Blad2", "Blad3"
'No Code here if excluded
Case Else
If sh.Range("A1").value < 8 Then sh.Range("A1") = 8
End Select
Next sh
End Sub
The cleanest way to do this with the code in its current form would be to pass the sheet object to the other sub:
Sub LoopCertain() 'Excel VBA to exclude sheets(1-3)
Dim sh As Worksheet
For Each sh In Sheets
Select Case sh.Name
Case Is = "Blad1", "Blad2", "Blad3"
'No Code here if excluded
Case Else
Call X2(sh)
End Select
Next sh
End Sub
and then
Sub X2(ByVal sh As Worksheet)
'calculate if a cell is less than a specific value
If sh.Range("A1") < 8 Then
sh.Range("A1") = 8
End If
End Sub
I've assumed you have a reason in the real-world use for having separate sub-routines, but once you understand this concept of passing the objects around then I would suggest just doing this in a single routine.
I am working on a macro that will cycle through all of the sheets in the active workbook and will then clear a certain part of a particular worksheet, based on whether one of the relevant keywords is contained in the worksheet name. In each case the worksheet name will be different, but any I want to clear will contain one of the key words below.
I have set up a separate macro to clear the range of cells in each case. If the Worksheet name does not contain any of the keywords, I want the macro to move onto the next worksheet.
My ultimate aim is to be able to apply this to numerous different workbooks, as the project I am working on is split by region, with a separate Excel file per region.
The code I have been trying is below. There are no errors appearing when I run the code, the code does not seem to run either, in fact nothing at all happens!
Any guidance or advice would be greatly appreciated.
Sub Loop_Customer_Sheets()
Dim ws As Integer
Dim i As Integer
ws = ActiveWorkbook.Worksheets.Count
For i = 1 To ws
If ActiveSheet.Name Like "*ABC*" Then
Call ABCInfoClear
ElseIf ActiveSheet.Name Like "*DEF*" Then
Call DEFInfoClear
ElseIf ActiveSheet.Name Like "*GHI*" Then
Call GHIInfoClear
Else:
End If
Next i
End Sub
"Nothing at all happens" - fixing the issue with your code:
Your issue is that you are looping through the number of sheets, but you are only checking the ActiveSheet, which never changes! Replace your code with
ws = ActiveWorkbook.Worksheets.Count
For i = 1 To ws
With ActiveWorkbook.WorkSheets(i)
If .Name Like "*ABC*" Then
ABCInfoClear
ElseIf .Name Like "*DEF*" Then
DEFInfoClear
ElseIf ActiveSheet.Name Like "*GHI*" Then
GHIInfoClear
End If
End With
Next i
Note: you don't need the Call keyword, you can just call subs as presented above.
Alternative solutions
A better option than having numerous macros might be to create a generic sub like
Sub ClearRangeInSheet(rangeAddress As String, sh As WorkSheet)
Dim myRange As Range
Set myRange = sh.Range(rangeAddress)
myRange.ClearContents
' Any other cell clearing code e.g. for formatting here
End Sub
Then call in the loop
Dim wsCount as Long
wsCount = ActiveWorkbook.WorkSheets.Count
For i = 1 to wsCount
With ActiveWorkbook
If .WorkSheets(i).Name Like "*ABC*" Then
' Always pass ".WorkSheets(i)", but change the range address as needed
ClearRangeInSheet("A1:A20", .WorkSheets(i))
ElseIf ' Other worksheet name conditions ...
End If
End With
Next I
As suggested in the comments, you could ditch indexing the sheets, and just loop through the sheet objects themselves:
Dim wksht as WorkSheet
For Each wksht In ActiveWorkbook.WorkSheets
If wksht.Name Like "*ABC*" Then
' Always pass wksht but change the range address as needed
ClearRangeInSheet("A1:A20", wksht)
ElseIf ' Other worksheet name conditions ...
End If
Next wksht
I have a worksheet named "Photo Sheet" that i would like to declare in my codes.
Const myWorksheet = "Photo Sheet"
My question, if i have another sheet called "Photo Sheet (2)" is there a way to declare the variable as wildcard that would take any sheet starting with "Photo Sheet*" ?
Not quite clear what you want to do, but you can iterate over the worksheets, using the Like operator to select the ones which have the appropriate name:
Sub test()
Dim ws As Worksheet
For Each ws In Worksheets
If ws.Name Like "Photo Sheet*" Then Debug.Print ws.Name
Next ws
End Sub
This will print the names of all worksheets that begin "Photo Sheet". Of course, rather than printing their names you could e.g. put these worksheets in a collection for further processing.
There isn't if you use an equity operator for testing it, but you can do something similar with Like:
Const SHEET_NAME = "Photo Sheet*"
Sub WhateverYourThingDoes()
Dim ws As Worksheet
For Each ws in Worksheets
If ws.Name Like SHEET_NAME Then
'Your code here.
End If
Next
End Sub
You can't use a wildcard to declare a worksheet directly, so no set shtPhotos = Sheets(Worksheet & "*"). A declaration like that has to be unambiguous or it would potentially return a collection, which can't be assigned to a non-array variable.
So, no wildcards. What you can do is loop through all your worksheets and check whether the sheet's name contains whatever text you're looking for:
Sub FindPhotos()
Const csSheet As String = "Photo Sheet"
Dim shtPhotos As Worksheet
For Each shtPhotos In ActiveWorkbook.Sheets
If InStr(1, shtPhotos.Name, csSheet) <> 0 Then
'do something
End If
Next shtPhotos
End Sub
This is going to look at every worksheet in the active workbook and see if their name contains the text in the constant. This will work fine if you want to perform the same steps with every worksheet that begins "Photo Sheets"; if you only want it to perform those steps with one such sheet, you'd add an Exit For after performing your steps:
For Each shtPhotos In ActiveWorkbook.Sheets
If InStr(1, shtPhotos.Name, csSheet) <> 0 Then
'do something
Exit For
End If
Next shtPhotos
You could also look for a flag on the sheets that are found, so that only sheets that have, say, today's date in a specified cell would be processed:
For Each shtPhotos In ActiveWorkbook.Sheets
If InStr(1, shtPhotos.Name, csSheet) <> 0 AND _
shtphotos.range("A1").value = Date Then
'do something
End If
Next shtPhotos
I'd like to find the position of a worksheet as it is displayed in a workbook.
For example, assume I have a workbook starting with Sheet1, Sheet2 and Sheet3 in that order. Then a user drags Sheet2 to left, before Sheet1.
I want Sheet2 to return 1, Sheet1 to return 2 (and Sheet3 still to return 3).
I can't find a way to determine this in VBA.
This should do it:
Worksheets("Sheet1").Index
https://msdn.microsoft.com/en-us/library/office/ff836415.aspx
You can just iterate the Worksheets collection of the Workbook object. You can test yourself by running the following code, switch the order around in the UI, then run it again:
Option Explicit
Sub IterateSheetsByOrder()
Dim intCounter As Integer
Dim wb As Workbook
Set wb = ThisWorkbook
For intCounter = 1 To wb.Worksheets.Count
Debug.Print wb.Worksheets(intCounter).Name
Next intCounter
End Sub
To loop through all worksheets in a workbook use For Each WS in ThisWorkbook.Worksheets where WS is a worksheet object. Hence to obtain order of Excel worksheets as shown, we may also use the following code:
Sub LoopThroughWorksheets()
Dim WS As Worksheet
For Each WS In ThisWorkbook.Worksheets
Debug.Print WS.Name
Next
End Sub
To obtain an output like Worksheets("Sheet1").Index then you may use this code
Sub IndexWorksheet()
Dim WS As Worksheet, n As Long
For Each WS In ThisWorkbook.Worksheets
n = n + 1
If WS.Name = "Sheet1" Then Debug.Print n
Next
End Sub
You can use the Sheets object. In your example, reading Sheets(2).Name should return Sheet1.
Right answer provided by Anastasiya-Romanova, but missing some important details.
There are two methods of doing this. First, with a For Each loop:
Sub ListSheetNames()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Debug.Print ws.Name
Next ws
End Sub
Second, with a basic For loop:
Sub ListSheetNames
Dim i As Long
For i = 1 to ThisWorkbook.Worksheets.Count
Debug.Print ThisWorkbook.Worksheets(i).Name
Next i
End Sub
You will find the second method will always output the names in the sheet index order, which is generally the order the sheets were created in unless you change the index. Simply rearranging the sheets from the workbook window won't change the index.
Therefore, the first method is the correct way to do this. It will always follow the tab order as you see on your screen.
Below code works even if sheet is renamed or its sequence is changed.
Sub Display_Sheet_Tab_Number()
Dim WorksheetName As String
Dim n As Integer
WorksheetName = Sheet1.Name
MsgBox Worksheetname
n = Sheets(WorksheetName).Index 'n is index number of the sheet
MsgBox "Index No. = " & n
End Sub
I have a materials register I am creating
Due to regulation when a material (each material has its own worksheet with a 3 digit random number added on the end to allow the same name multiple times) is deleted it cannot actually be deleted, so to work around this my workbook hides the sheet and using a deletion check on the summary page hides the appropriate row.
However what I am struggling with is a function to restore the sheet,
I have the code I need to do this however I cannot find any function to list hidden sheets.
This list can be put into the work book in a hidden column so I can reference it with my macro but as I said I cannot find anyway to list only sheets that are hidden.
Thanks for your help
You could add to your code that does the hiding to write the name of the sheet that it is hiding to your other hidden tab, and add the reverse to your code that unhides it.
Not sure if the below is applicable to your situation, but you could also put some code in worksheet events to capture when the sheet is being made invisible
Private Sub Worksheet_Deactivate()
If Me.Visible = xlSheetHidden Then MsgBox "I have been hidden"
End Sub
Does this help ..
' Function to be used in array formula on sheet to list hidden sheets
Public Function ListHiddenSheets()
Dim hiddenSheets As New dictionary
Dim sheet As Worksheet
For Each sheet In Worksheets
If sheet.Visible <> xlSheetVisible Then hiddenSheets.Add sheet.Name, Null
Next sheet
Dim vRes() As Variant
ReDim vRes(0 To hiddenSheets.Count, 0 To 0)
Dim idx As Integer
For idx = 0 To hiddenSheets.Count - 1
vRes(idx, 0) = hiddenSheets.keys(idx)
Next idx
ListHiddenSheets = vRes
End Function
?
Hidden sheets can be Hidden or VeryHidden, to capture these:
ub ListEm()
Dim ws As Worksheet
Dim StrHid As String
Dim strVHid As String
For Each ws In ActiveWorkbook.Worksheets
Select Case ws.Visible
Case xlSheetVisible
Case xlSheetHidden
StrHid = StrHid & ws.Name & vbNewLine
Case Else
strVHid = strVHid & ws.Name & vbNewLine
End Select
Next
If Len(StrHid) > 0 Then MsgBox StrHid, vbOKCancel, "Hidden Sheets"
If Len(strVHid) > 0 Then MsgBox strVHid, vbOKCancel, "Very Hidden Sheets"
End Sub