I've tried using the following code to select all Worksheets in the Workbook:
Public Sub selectAllWS()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
ws.Select False
Next
End Sub
However, this doesn't seem to work -- it doesn't select multiple Worksheets.
(Sidenote: I know I can use Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select. I don't want to do this -- I'm experimenting if I can select Sheets across multiple Workbooks.
I'm running the latest version of Excel 2016.
This is a way you could accomplish this, and this does use the Worksheets(Array:
Workbooks("Book.xlsx").Activate
Workbooks("Book.xlsx").Worksheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
Workbooks("Book1.xlsx").Activate
Workbooks("Book1.xlsx").Worksheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
side note:
As asked in the comments I am unsure why you would do this, especially since using .Select is not the proper coding way in VBA.
Related
I have a VBA module where I want to select a worksheet, but only if it is present in the workbook.
So if I use activeworksheets code this gives an error if the worksheet is not in the workbook.
I have these for 3 worksheets, so I have tried if error but this only works if one of the worksheets is missing, as if error only handles the first case and cannot handle further cases.
Any suggestions?
There are many ways to solve this. My suggestion is doing
Dim ws as Worksheet
Set ws = Worksheets(1)
if ws.Name == "Worksheet I want"
'Do your thing
End if
Idk how many sheets you have. Use a for with Application.Sheets.Count if you need to.
Hope this helps
Situation
Trying to copy a range of cells that include formulas from worksheet called "Sheet1" to the rest of other worksheets I found that I could do it performing "Fill Across Worksheets". It worked fine, so my next step was to record a Macro for it to be more efficient and worked just fine too.
The Problem
The problem is that when I include a new worksheet and run the Macro, the Macro does not consider the new worksheet so this last worksheet doesn't get updated.
I am including below the code created by the macro. In it I can see that it's including only the worksheets I have now in the workbook, so this is where I need help.
( My excel is in Spanish so when you read Ctrl+Mayus+Q, Mayus means Shiftkey )
Help
What I need is a way to modify this Macro so when it runs it will check and update all worksheets. Or, maybe it's because a Macro can't do this I may need a VBA code ? If this VBA is the way to resolve it, can you help me here with this ?
I appreciate all help
Thank you
Javier
Sub Macro2()
'
' Macro2 Macro
'
' Acceso directo: Ctrl+Mayús+Q
'
Range("A5:D12").Select
Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4")).Select
Sheets("Sheet1").Activate
ActiveWindow.SelectedSheets.FillAcrossSheets Range:=Selection,
Type:=xlAll
Sheets("Sheet1").Select
End Sub
This is a work around, concerning that you want all the worksheets to have the value of the first worksheet in range A5:D12:
Sub TestMe()
Dim ws As Worksheet
Dim selAddress As String
selAddress = "A5:D12"
For Each ws In Worksheets
'ws.Range(selAddress).Value2 = Worksheets(1).Range(selAddress).Value2
ws.Range(selAddress).Formula = Worksheets(1).Range(selAddress).Formula
Next ws
End Sub
See How to avoid using Select in Excel VBA.
I need VBA code for Excel which: will be activated by a button in an empty workbook, loop through open workbooks, copies only sheets called "specificsheetname" from workbooks and pastes it into a new worksheet in the button activator workbook. So idea is that it will combine many worksheets from different workbooks into a one workbook. I tried this:
Sub workbookFetcher()
Dim book As Workbook, sheet, wsNew, wsCurr As Worksheet
Set wsCurr = ActiveSheet
For Each book In Workbooks
For Each sheet In book.Worksheets
If sheet.Name = "COOLING_RAW" Then
Set wsNew = Sheets.Add(After:=wsCurr)
book.Worksheets("COOLING_RAW").Copy
Set wsNew = book.Worksheets("COOLING_RAW")
End If
Next sheet
Next book
End Sub
It kind of works but it pastes all the copied worksheets to a new workbook. That's not what I want, I want them to pasted in the same workbook.
As I said in my comment:
Sub workbookFetcher()
Dim book As Workbook, sheet as Worksheet
For Each book In Workbooks
For Each sheet In book.Worksheets
If sheet.Name = "COOLING_RAW" Then
book.Worksheets("COOLING_RAW").Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
End If
Next sheet
Next book
End Sub
If you want it to be after the ActiveSheet and the ActiveSheet is in the middle of other sheets, you can still use your wsCurr and just increment the index.
If you've got Excel 2016, then the newly bundled PowerQuery functionality under the Get & Transform part of the ribbon is by far the best way to do this. Suggest you google something like PowerQuery Combine Workbooks and you'll see heaps of great tutorials showing you exactly what to do. It pretty much makes lots of VBA redundant, and it is childs-play to learn compared to VBA.
If you've got any other version of Excel from 2010 up and have admin rights on your machine, you can download and install PowerQuery from Microsoft's site...it's a free add-in
you don't need to iterate through each single workbook worksheets, while you just try to get the wanted sheet and copy it if it actually exists
moreover you want to avoid searching ThisWorkbook itsel for wanted worksheet, too!
Option Explicit
Sub workbookFetcher()
Dim book As Workbook, sht As Worksheet
For Each book In Workbooks
If book.Name <> ThisWorkbook.Name Then ' skip ThisWorkbook and avoid possible worksheet duplication
If GetWorksheet(book, "COOLING_RAW", sht) Then sht.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) ' if currently searched workbook has wanted worksheet then copy it to ThisWorkbook
End If
Next
End Sub
Function GetWorksheet(book As Workbook, shtName As String, sht As Worksheet) As Boolean
On Error Resume Next ' prevent subsequent statement possible error from stoping the function
Set sht = book.Worksheets(shtName) ' try getting the wanted sheet in the passed workbook
GetWorksheet = Not sht Is Nothing ' return 'True' if successfully got your sheet in the passed workbook
End Function
I'm an average/intermediate VBA developer, mostly able to modify existing code and re-use code for expanding Excel projects.
We ran into a situation where someone recorded some Macro's in Excel 2013 but they won't work in Excel 2003. They HAVE to work in Excel 2003 due to the legacy nature of the machine this sheet will be primary on.
So, I did some research and re-wrote the code for the Macros so it would on 2003. However, of the 4 worksheets in this workbook, it only worked on the first page. It drove me crazy trying to figure out why. Finally noticed that on the other 3 worksheets, the first column and row are PROTECTED. If I unprotect them, the re-written Macros work.
So need a little advice on how to make these re-written macros work with the first row and column protected.
This is a sample of the Macro (there are multiple ones like this with just range differences):
Sub KA24()
' Had to Convert Macro to work with the older version of Excel 2003 in the
shop - BW - 8/31/2017
Dim Ws As Worksheet
Set Ws = Worksheets("24 GA KY")
Application.ScreenUpdating = False
Ws.Range("D2:J9").Sort Key1:=Ws.Range("G2"), Order1:=xlAscending,
Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom,
DataOption1:=xlSortNormal
Application.ScreenUpdating = True
End Sub
You can unprotect the Worksheet for use in your VBA code using the UserInterFaceOnly method. This assumes your sheet is password protected with the password "Something", if not just leave out the password part.
Dim Ws As Worksheet
Set Ws = Worksheets("24 GA KY")
Ws.Protect Password:="Something", UserInterFaceOnly:=True
...
I have an Excel workbook (1) with around 9 sheets that is pulling in and manipulating data from a second workbook (2).
After pulling in the data from workbook (2) I need to be able to replace the formulas in workbook (1) to the resulting values that the formulas have produced, from here I will then save the workbook (1) with the results.
Is there a macro that can do this for me?
On your new workbook some basic code such as:
Sub Value()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
ws.UsedRange.Value = ws.UsedRange.Value
Next
End Sub
While the OP is dated, I want to make note of a non-loop method that is useful. In certain scenarios, loops can really slow down the code execution. To replace formulas in a cell --without a loop-- try:
With Sheets("example").Range("A1:C1000")
.value = .value
End With
You can revise the reference as necessary, but the execution is seamless, fast, and as a bonus - prevents range highlighting that cannot be cleared if you pursued the .copy + .pastespecial xlPasteValues approach.
What seems to work for me is to use concatenate()
So, for example, the formula I have referencing a cell from another sheet is:
=arrayformula(iferror(index('To Be Processed'!X:X,small(if($A$1='To Be
Processed'!$Y2,row('To Be Processed'!X:X)),row((2:2))),"")))
and if I change to the formula to:
=concatenate(arrayformula(iferror(index('To Be
Processed'!X:X,small(if($A$1='To Be Processed'!$Y2,row('To Be
Processed'!X:X)),row((2:2))),""))))
and it puts in the text value from the reference cell into my second sheet.
Which may or may not be helpful depending on how you populate your sheets--I'm not very good with VBA, though, which means I do more things manually :)