Excel VBA - Copy/Paste range from one sheet to all proceeding sheets - vba

First time asking a question so please let me know if I'm missing anything.
I found this code from another SO post. I'm trying to copy the entire worksheet from "DNU" into each proceeding worksheet. The issue I'm having is that this will paste values but I'm looking to do a regular paste to keep the formatting and formulas. I've tried changing from the "Value" to Copy and Paste but this end up in an error. Any help is appreciated. Thank you.
Here is the code:
~
Dim wsVar As Worksheet
Dim i as Integer
For i = 6 to ThisWorkbook.Worksheets.Count
ThisWorkbook.Worksheets(i).Range("A1:y200").Value = ThisWorkbook.Worksheets("DNU").Range("A1:Y200").Value
Next i
End Sub

Use Copy() method of Range object
Dim wsVar As Worksheet
Dim i as Integer
With ThisWorkbook
For i = 6 to .Worksheets.Count
.Worksheets("DNU").Range("A1:Y200").Copy destination:=.Worksheets(i).Range("A1:Y200")
Next
End With

Related

Copy Range to Another Worksheet Using Input Box to Choose Range

I am writing a VBA macro where I have an InputBox come up, the user will select a range which will be a full column, and then the macro will paste that range in a particular place on another worksheet. I have been trying to make this code work, but I keep getting different errors depending on what I try to fix, so I was wondering if someone could help me out. I have pasted the relevant parts of the code:
Sub Create_CONV_Files()
Dim NewCode As Range
Set NewCode = Application.InputBox(Prompt:="Select the column with the code numbers", Title:="New Event Selector", Type:=8)
Dim RawData As Worksheet
Set RawData = ActiveSheet
Dim OffSht As Worksheet
Set OffSht = Sheets.Add(After:=Sheets(Sheets.Count))
OffSht.Name = "offset.sac"
Worksheets(RawData).Range(NewCode).Copy _
Destination:=OffSht.Range("A:A")
End Sub
I have tried making the input a string instead, but I am also getting errors there and am not sure how to fix that. I was hoping to use roughly the method I have outlined as my full code has multiple destination sheets and ranges.
Thank you very much for any help you can offer!
once you have set a Range object it brings with it its worksheet property so there's no need to qualify its worksheet
Sub Create_CONV_Files()
Dim NewCode As Range
Set NewCode = Application.InputBox(prompt:="Select the column with the code numbers", title:="New Event Selector", Type:=8)
Dim OffSht As Worksheet
Set OffSht = Sheets.Add(After:=Sheets(Sheets.count))
OffSht.Name = "offset.sac"
NewCode.Copy _
Destination:=OffSht.Range("A1")
End Sub

Copy value form sheet, use as worksheet names (in a loop) - VBA Excel Macro

I am attempting to Copy values in a Row, one by one from each cell, then use those values as worksheet names, one by one. One of the issues I had was skipping the first batch of sheets, so I attempted to hide the ones I don't want renamed. All other sheets should be renamed (34 of them).
I can get it all the way "ws.PasteSpecial xlPasteValues" using F8 before I get an error message that says "Run-time error '1004'L Method 'PasteSpecial' of object'_Worksheet' failed.
I have also tried using "Activesheet.PasteSpecial xlPasteValues" but it gave the same error.
Any suggestions at all are very much appreciated, this is driving me nuts. :) My back up plan is simply using the macro record and doing every rename manually, but it's not a very elegant or simple code that way, so I'd prefer not to do that.
Here is the Code:
Dim ws As Worksheet
Dim TitleID As String
Dim TID As String
Sheets("SheetName1").Activate
Set ws = ActiveSheet
Dim rng As Range, cell As Range
Set rng = ws.Range("C5", "AJ5")
For Each cell In rng
cell.Copy
Sheets("SheetName1").Visible = False
ws.Next.Select
ws.PasteSpecial xlPasteValues
Next cell
If I understand your question, properly, the below code should work.
Dim ws As Worksheet
Set ws = Sheets("SheetName1")
Dim rng As Range, cell As Range
Set rng = ws.Range("C5","AJ5")
Dim i as Integer
i = 5 'this is an arbitrary number, change to whatever number of worksheets
'you wish to exclude that are at the beginning (left most side) of your workbook
'also assumes "SheetName1" is before this number.
For Each cell In rng
Sheets(i).Name = cell.Value
i = i + 1
Next cell

Is it possible to copy data from a cell into a macro?

I have written data in a cell that is updated by a macro - it appends a reference depending on what somebody has called a new sheet.
In Cell A1 I end up with macro code that is updated with the new sheet name. Currently users have to copy this text and open another macro and paste the code in, however they keep doing it wrong and breaking it.
What I would like to do is write a macro to copy the contents of Cell A1 and paste them into the original macro.
If it possible to do this?
Based on a suggestion at the MrExcel.com forum:
Sub aaa()
For i = 1 To Application.VBE.CodePanes.Count
Application.VBE.CodePanes(i).CodeModule.ReplaceLine 1, "alert('Yo') ' New code"
Next i
I know this is not an answer to your question but I'm just offering some information. I would suggest that you don't have users create a macro in each sheet. You can access anything on any sheet from a module. I am not sure what you whole process is but you could think of it more along the lines of looking for the sheets you want to change.
Public sub ProcessSheets()
Dim ws As Excel.Worksheet
Dim iIndex As Integer
'Loop through all the worksheets in the workbook.
For iIndex = 1 To ActiveWorkbook.Worksheets.count
'Activate the current sheet, if you need to.
Set ws = Worksheets(iIndex)
ws.Activate
'Check the name of the worksheet.
If Left(ws.Name, 2) = "HD" or Left(ws.Name, 2) = "ER" Then
'Call the function that changes the worksheet here.
'Maybe feed it the worksheet name.
UpdateWorksheet ws.Name
End if
Next iIndex
End Sub

Using cell value as range VBA

I am trying to have a range (call it "A5:L10") be picked up from a cell. In other words, my code looks something similar to that below:
Dim summ_rng1 As String
summ_rng1 = Sheet11.Cells(17, 3).Value
Workbooks(wb).Sheets(summ).Range(summ_rng1).......
Where summ_rng1 = "A5:L10"
I have done this same thing for the workbooks and sheets in my code and it works fine, but when I try to replace the range reference with the variable summ_rng1 it does not work.
Any idea on how to get the code to run with the range value as a variable, like that above? Thanks for the help!
Your code is working for me. I think it is related to your wb object, which may contain the workbook itself, rather than the name of the workbook. Try this :
Sub testSub()
Dim myRange As String
Dim wb As Workbook
Set wb = ThisWorkbook
myRange = wb.Sheets(1).Cells(1, 1)
wb.Sheets(1).Range(myRange).Select
End Sub

For Each method in VBA not working

I am currently struggling to get the For Each method working in my VBA in Excel 2010.
I have used the methoed before and had no problem but for the life of me I cannot seem to get the new function I have written to work.
Here it is:
Sub A2_Color_Bob()
Dim sh As Worksheet
For Each sh In Sheets
Range("A2").Value = "Bob"
Next sh
End Sub
The reason why it's not working is that you haven't specified which sheet in this line
Range("A2").Value = "Bob"
Therefore, the default sheet will be used, i.e. the ActiveSheet
Hence, as others have also mentioned, it should be changed to
sh.Range("A2").Value = "Bob"
I think your issue is that's your Range is global, but you're trying to set a specific cell's value.
Did some checking and this works.
Sub A2_Color_Bob()
Dim sh As Worksheet
For Each sh In Sheets
sh.Cells(1, 1).Value = "bob"
Next sh
End Sub
change for each sh in sheets , to ...in worksheets
or can also do
dim i as long
for i=1 to worksheets.count
sheets(i).range("A2")=bob"
next i