VBA Can't Loop Through All Worksheets - vba

I'm new to VBA, and I got stuck with a simple problem. I want to select all cells with data in the worksheet, paste them by value, then replace "#NUM!" with ""(nothing) and repeat all the above steps in all worksheets. The issue is that it wouldn't loop through all worksheets. Please help.
Sub Test()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
'Select all, copy and paste-by-value
ws.Range("A1").CurrentRegion.Value = ws.Range("A1").CurrentRegion.Value
'Find and Replace
Cells.Replace What:="#NUM!", Replacement:=""
Next ws
End Sub

Related

VBA For Loop Only Altering One Sheet

I'm trying to alter the values in a range of cells for specifically named worksheets only.
The workbook I am editing has around 95 sheets, and I only want to change the sheets with the period actual information (named P1W1, P1W2 etc, up to P12W5).
When i execute the below it only alters the first sheet and then exits the macro.
Any help is much appreciated
Option Explicit
Public Sub periodclear()
Dim ws As Worksheet
Dim r As Range
On Error Resume Next
Set r = Range("c10:i30")
For Each ws In Worksheets
If ws.Name Like ("P#W#") Or ws.Name Like ("P##W#") Then
r.Value = ""
End If
Next ws
End Sub
Try this. Your r was defined only in terms of one sheet so needs to be brought inside the loop.
Public Sub periodclear()
Dim ws As Worksheet
For Each ws In Worksheets
If ws.Name Like ("P#W#") Or ws.Name Like ("P##W#") Then
ws.Range("c10:i30").ClearContents
End If
Next ws
End Sub

Copy/Paste Macro Pasting Randomly

I am having an issue copying and pasting a drop-down data validation menu from one sheet into all selected sheets. The drop-down menu seems to paste randomly instead of pasting into sheet "B22" of the selected sheets.
Sub TEST()
Dim sht As Worksheet
Sheets("Sheet2").Range("B22").Copy
'Sheets selection should be done before running macro
Selection.Range("B22").PasteSpecial xlPasteValidation
Application.CutCopyMode = False
End Sub
Any suggestions on how to tackle this? I am having some difficulty finding the error in my code.
In case you want to work with the workbook and avoid using copy/paste..
Option Explicit
Sub Test()
Dim excel_sheet As Worksheet
Dim sht As Worksheet
Dim drop_down_value As Range
Set sht = ThisWorkbook.Sheets("Sheet2")
Set drop_down_value = sht.Range("B22")
'Sheets selection should be done before running macro
For Each excel_sheet In ThisWorkbook.Windows(1).SelectedSheets
excel_sheet.Range("B22").Value = drop_down_value.Value
Next
End Sub
Try the following, you need to loop through all selected Sheets instead of using Selection.Range:
Sub TEST()
Dim sht As Worksheet
Sheets("Sheet2").Range("B22").Copy
'Sheets selection should be done before running macro
For Each sht In ActiveWindow.SelectedSheets
sht.Range("B22").PasteSpecial xlPasteValidation
Application.CutCopyMode = True
Next
End Sub

Copy formulae of range of cells to same range in other sheets

I'm trying to figure out how to copy the formulae of a range of cells to all other or selected sheets.
For example, I want the Formulae of Sheet1.A9:B250 copied to all other Sheets (SheetX.A9:B250)
I tried the following but it seems pretty off
Sub CopyAll()
Dim sht As Worksheet
Sheets("Albert").Range("A9:B250").Formula.Copy
For Each sht In Worksheets
sht.Range("A9:B250").Formula.PasteSpecial xlPasteValues
Next
Application.CutCopyMode = False
End Sub
Is there an elegant solution to this? Thank you in advance
Why not bypass copy and paste all together?
this should do the job
Sheets("Sheet1").Range("A9:B250").Formula = Sheets("Sheet2").Range("A9:B250").Formula
Your code would look something like...
Sub CopyAll()
Dim sht As Worksheet
For Each sht In Worksheets
sht.Range("A9:B250").Formula = Sheets("Albert").Range("A9:B250").Formula
Next
End Sub
When working with Copy >> Paste of formula you can use the following solution:
'.....
Sheets("Albert").Range("A9:B250").Copy
'.....
sht.Range("A9:B250").PasteSpecial xlPasteFormulas
'.....

Excel vba to copy and paste a cells from different worksheets to new worksheet

i need a macro to copy and paste a certain cells(b5:d10) from multiple worksheets and paste it into a new single worksheet. just i want to collide the contents .thanks in advance
sub copyrange()
range("b5:d10).copy
range("e1").select
activesheet.paste
application.cutcopymode=false
endsub
my code doesnot copy all the worksheets data. kindly help me to copy and paste it
Sub copyrange()
Dim rngTarget As Range, wksTemp As Worksheet
Set rngTarget = ThisWorkbook.Worksheets(1).Range("A2:C7")
For Each wksTemp In ActiveWorkbook.Worksheets
rngTarget.Value = wksTemp.Range("B5:D10").Value
Set rngTarget = rngTarget.Offset(6)
Next wksTemp
End Sub

Copy All values VBA script failing

I am trying to create a VBA script that copies all data in a whole workbook as pastes as values, then saves as a new workbook, thus removing all formulas.
Here is my code:
Sub MakeAllVals()
Dim wSheet As Worksheet
For Each wSheet In Worksheets
With wSheet
.UsedRange.Copy
.PasteSpecial xlValues
End With
Next wSheet
Application.Dialogs(xlDialogSaveAs).Show
End Sub
I'm getting a runtime error 1004 on the .PasteSpecial xlValues command but I can't work out why.
How can I accomplish the goal of pasting all data as values and saving as a new workbook?
You just need to paste to a range in the new sheet. Currently you are not pasting within the new book and you are not pasting within a range.
Sub MakeAllVals()
Dim oldBook As Workbook, oldSheet As Worksheet
Dim newBook As Workbook, newSheet As Worksheet
Set oldBook = ThisWorkbook ' Set to the formula workbook you want to copy from
Set newBook = Workbooks.Add ' Make the new workbook you want only values in
For Each oldSheet In oldBook.Sheets ' Loop through all of the sheets in the formula book
Set newSheet = newBook.Sheets.Add ' Make a new sheet in the newbook to add the values to
newSheet.Name = oldSheet.Name & " Values"
oldSheet.UsedRange.Copy
newSheet.Range("A1").PasteSpecial xlValues ' Paste in a range on the new sheet as Values
Next oldSheet
Application.Dialogs(xlDialogSaveAs).Show ' Show Save As Dialog window
End Sub
You were close just have to move UsedRange up next to wSheet
Sub MakeAllVals()
Dim wSheet As Worksheet
For Each wSheet In ActiveWorkbook.Worksheets
With wSheet.UsedRange
.Copy
.PasteSpecial xlValues
End With
Next wSheet
Application.Dialogs(xlDialogSaveAs).Show
End Sub