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

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

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

How do I copy information of one cell to a cell on another worksheet, when a specific cell is clicked?

I've been trying to create a VBA macro that when I click on a certain cell (in excel), the information on another worksheet can get copied into the designated cells.
For instance, if I click on A1 in Worksheet1, then B20:C20 on worksheet3 is copied to B20:C20 of Worksheet2. Can anyone help me create this macro?
Try to follow this example below:
Private Sub CommandButton1_Click()
Dim ws As Worksheet, ws1 As Worksheet
Set ws = Sheets("KeyInformation")
Set ws1 = Sheets("Factsheet")
ws.Range("A2:Q2").Copy
ws1.Range("B9").PasteSpecial Paste:=xlPasteAll, Transpose:=True
Application.CutCopyMode = False
ws1.Activate
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
'.....

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

Repeating macro code

I recorded this macro:
Sheets("Sheet1").Select
Range("D4:E4").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("ALB3").Select
Range("C1").Select
ActiveSheet.Paste
I want to make a loop to repeat the process.
From range D4:E4 to D200:E200 when do select
To paste that on respective sheet name from ALB3 to ALB196.
My data in sheet 1.
Column a is sheets name, column d4 and e4, is the data that I want to paste on every sheet already created.
If you're trying to copy a range from one sheet to another, you don't need a loop and you don't need to select. You can use copy syntax that doesn't use your clipboard.
Try this:
Sub CopyRangeToAnotherSheet()
Dim source As Worksheet
Dim target As Worksheet
Set source = ActiveWorkbook.Sheets("Sheet1")
Set target = ActiveWorkbook.Sheets("Sheet2")
source.Range("D4:E200").Copy target.Range("ALB3")
End Sub
To copy the source range to all sheets in the workbook except the source worksheet, try this:
Sub CopyToAllSheets()
Dim ws As Worksheet
For Each ws In Worksheets
CopyRangeToAnotherSheet (ws.Name)
Next
End Sub
Sub CopyRangeToAnotherSheet(targetName As String)
Dim source As Worksheet
Dim target As Worksheet
Set source = ActiveWorkbook.Sheets("Sheet1")
Set target = ActiveWorkbook.Sheets(targetName)
If target.Name <> source.Name Then
source.Range("D4:E200").Copy target.Range("ALB3")
End If
End Sub