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

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
'.....

Related

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

Unhide worksheet

I am creating a spreadsheet to record unit finances. The spreadsheet I am using as a template/demo for mine has a hidden worksheet that I'm trying to view.
I have googled, and tried what has been suggested, including VBA.
What can I do to see this worksheet? (I would upload the file, but I don't know how)
For a specific sheet:
Worksheets("MySheet").Visible = xlSheetVisible
Make all sheets in the workbook visible:
Sub MakeAllSheetsVisible()
Dim sht As Worksheet
For Each sht In Worksheets
If Not sht.Visible Then
sht.Visible = xlSheetVisible
End If
Next sht
End Sub

VBA Can't Loop Through All Worksheets

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

Copy/paste values from multiple sheets, but not all sheets, into one sheet

I am needing to copy cells B3:W400 from multiple sheets (will have varying names each time it is run) and paste values into "CombinedPlans", appending each new selection under the last. I need 3 sheets excluded from the code: IBExport, MonthlyIBs, and Combined Plans.
A lot of googling with trial and error has given me the following code, which I got to work in my "practice" workbook. Now that I have put it into my production workbook, it is no longer copying any sheets. It just skips straight to the message box. What am I doing wrong?
Sub consolidatetest()
Sheets("CombinedPlans").Select
Range("B3:W1048576").Select
Selection.ClearContents
Dim J As Integer
Dim sh As Worksheet
Const excludeSheets As String = "QBExport,MonthlyIBs,CombinedPlans"
On Error Resume Next
For Each sh In ActiveWorkbook.Worksheets
If IsError(Application.Match(sh.Name, Split(excludeSheets, ","))) Then
Application.GoTo Sheets(sh.Name).[b3]
Range("B3:W400").Select
Selection.Copy
Worksheets("CombinedPlans").Activate
Range("B1048576").End(xlUp).Offset(rowOffset:=1, columnOffset:=0).PasteSpecial xlPasteValues
End If
Next
Application.CutCopyMode = False
MsgBox "Complete!"
End Sub
This should work. If you have still problems, make sure that the Sheet CombinedPlans is indeed so named.
Sub consolidatetest()
Dim wb As Workbook
Dim sh_CombPlans As Worksheet
Set wb = ThisWorkbook
Set sh_CombPlans = wb.Sheets("CombinedPlans")
sh_CombPlans.Range("B3:W1048576").ClearContents
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
Select Case sh.Name
Case "QBExport", "MonthlyIBs", "CombinedPlans":
'Do Nothing
Case Else
sh.Range("B3:W400").Copy
sh_CombPlans.Range("B1048576").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
End Select
Next
Application.CutCopyMode = False
MsgBox "Complete!"
End Sub

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