I'm extremely new to VBAs and cannot figure out how to add a value to the next row if there's already data previous row. I'm sure I'm overthinking it, but I cannot seem to figure it out. Any help would be appreciated.
Below is the macro I'm using. Not sure if I need to offset the data or maybe add an if then statement of some sort.
Sub Archive_2()
Range("A2").Select
Selection.Copy
Sheets("Campaign Rate").Select
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Use below sub-
Sub CopyPaste()
Dim sh As Worksheet
Dim lRng As Range
Set sh = Worksheets("Campaign Rate")
Set lRng = sh.Cells(sh.Rows.Count, 1).End(xlUp)
Range("A2").Copy
lRng.Offset(1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Set sh = Nothing
Set lRng = Nothing
End Sub
When I run my code I get a "400" error message. but it is only the last part of my code that doesn't work("creating template"). Im trying to copy cells A1:BA1000 from the sheet "Data" to a new sheet which is named based on the value of cell EU12 in the data sheet.
'copy the last values from december
Range("HI5:IA1000").Select
Selection.Copy
Range("IC5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'copy the date to new workbook.
Range("EU8").Select
Selection.Copy
Range("EU12").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'copy sheet to new workbook
sheettocopy = Range("EU10").Value
Worksheets(sheettocopy).Copy
'adding sheet and renaming
Sheets.Add After:=ActiveSheet
ActiveSheet.Name = Sheets("Data").Range("EU12").Value
'creating template
Sheets("Data").Select
Range("A1:BA1000").Select
Selection.Copy
ActiveWorkbook.Sheets(Worksheets("Data").Range("EU12").Value).Activate
Range("A1").Select
ActiveSheet.Paste
EDIT:
made a new macro just for the creation of a template, the code works(when stored in a module) but when i copy my first sheet in the beginning the module doesnt follow:
Sub addinfo()
'creating template
Set wk = ThisWorkbook
Dim template As String
template = Sheets("Data").Range("EU12").Value
wk.Sheets("Data").Range("A1:BA1000").Copy wk.Sheets(template).Range("A1")
End Sub
I am trying to copy the range of value from one sheet in excel to another. I have copied this formula from another part of my sheet that works however i cam coming up with the run time error 9.
Sub SaveJambStudEC()
'
' SaveCalcsJambEC Macro
'
Dim page As Integer
page = Cells(4, "T").Value
Range("A70:AN70").Select
Selection.Copy
Range("A71").Select
ActiveCell.Offset(page, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1:O63").Select
Selection.Copy
Sheets("10.3 JambCalcs EC").Select
Range("A1").Select
ActiveCell.Offset((page - 1) * 63, 0).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats
Sheets("9.3 Jamb Design EC").Select
Range("T5").Select
Selection.Copy
Range("N9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("T31").Value = 0
Call JambECsetDesignOptions
Call CopyJambOptiValues
Range("J9").Activate
End Sub
using the below code you will check if there is a sheet with that name. if you dont receive any message box means that there is no sheet with such name
Option Explicit
Sub test()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name = "10.3 JambCalcs EC" Then
MsgBox "Sheet Appears"
Exit Sub
End If
Next ws
End Sub
Note
'ThisWorkbook' refer to the workbook that the code included. If you want to clearly declare the workbook you could declare a variable 'Dim wb as Workbook' and then set the workbook 'Set wb=Workbooks("workbook name")'
I have a workbook of 80 worksheets,and I want to select only 4 worksheets with selected range. I want to copy this range a new workbook automatically. I have run a macro and this code below.
Sub TestTest()
'
' TestTest Macro
'
'
Sheets("Summary").Select
Range("A1:O54").Select
Selection.Copy
Workbooks.Add
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
ActiveWorkbook.SaveAs Filename:="Newworkbook.xlsx",
FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
Windows("").Activate
Sheets("").Select
Range("").Select
Selection.Copy
Windows("Newworkbook.xlsx").Activate
Sheets.Add After:=ActiveSheet
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("").Activate
Sheets("").Select
Range("").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Newworkbook.xlsx").Activate
Sheets.Add After:=ActiveSheet
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("").Activate
Sheets("").Select
Range("").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Newworkbook.xlsx").Activate
Sheets.Add After:=ActiveSheet
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("").Activate
Sheets("").Select
ActiveWindow.SmallScroll Down:=-18
Range("").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Newworkbook.xlsx").Activate
Sheets.Add After:=ActiveSheet
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("").Activate
Range("N29").Select
End Sub
I believe it should work as you expect it to, it will copy all five ranges and paste them into a new workbook, renaming each of the sheets appropriately and then save the newly created workbook to your desired location, you might want to add something to the name of the new workbook you are creating, such as a date, so as to not give you any issues the next time you run the code, alternatively you could delete the previous report and overwrite it:
Sub Test()
Dim wsSummary As Worksheet: Set wsSummary = ThisWorkbook.Sheets("Summary")
Dim wsMeasured As Worksheet: Set wsMeasured = ThisWorkbook.Sheets("1. Measured Work")
Dim wsPreliminaries As Worksheet: Set wsPreliminaries = ThisWorkbook.Sheets("2. Preliminaries")
Dim wsFees As Worksheet: Set wsFees = ThisWorkbook.Sheets("3. Fees")
Dim wsContingency As Worksheet: Set wsContingency = ThisWorkbook.Sheets("4. Contingency")
Dim NewWorkBook As Workbook
'above declare and set the worksheets and workbook you are working with
Application.ScreenUpdating = False
Set NewWorkBook = Workbooks.Add
'add a new workbook
wsSummary.Range("A1:O54").Copy 'copy first range
NewWorkBook.Sheets(1).Range("A1").PasteSpecial xlPasteValues
NewWorkBook.Sheets(1).Range("A1").PasteSpecial xlPasteFormats
'paste into first sheet in new workbook
NewWorkBook.Sheets(1).Name = "Summary" 'rename the sheet in the new workbook
wsMeasured.Range("B1:Q76").Copy 'copy second range
NewWorkBook.Sheets.Add After:=ActiveSheet 'add a new sheet to the new workbook
NewWorkBook.Sheets(2).Range("A1").PasteSpecial xlPasteValues
NewWorkBook.Sheets(2).Range("A1").PasteSpecial xlPasteFormats
'paste into second sheet in new workbook
NewWorkBook.Sheets(2).Name = "1. Measured Work" 'rename the sheet in the new workbook
wsPreliminaries.Range("B1:Q48").Copy 'copy third range
NewWorkBook.Sheets.Add After:=ActiveSheet 'add a new sheet to the new workbook
NewWorkBook.Sheets(3).Range("A1").PasteSpecial xlPasteValues
NewWorkBook.Sheets(3).Range("A1").PasteSpecial xlPasteFormats
'paste into third sheet in new workbook
NewWorkBook.Sheets(3).Name = "2. Preliminaries" 'rename the sheet in the new workbook
wsFees.Range("B1:Q47").Copy 'copy fourth range
NewWorkBook.Sheets.Add After:=ActiveSheet 'add a new sheet to the new workbook
NewWorkBook.Sheets(4).Range("A1").PasteSpecial xlPasteValues
NewWorkBook.Sheets(4).Range("A1").PasteSpecial xlPasteFormats
'paste into fourth sheet in new workbook
NewWorkBook.Sheets(4).Name = "3. Fees" 'rename the sheet in the new workbook
wsContingency.Range("B1:Q46").Copy 'copy fifth range
NewWorkBook.Sheets.Add After:=ActiveSheet 'add a new sheet to the new workbook
NewWorkBook.Sheets(5).Range("A1").PasteSpecial xlPasteValues
NewWorkBook.Sheets(5).Range("A1").PasteSpecial xlPasteFormats
'paste into fifth sheet in new workbook
NewWorkBook.Sheets(5).Name = "4. Contingency" 'rename the sheet in the new workbook
Application.CutCopyMode = False 'deselect copied range
NewWorkBook.SaveAs Filename:="NewWorkBook.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
'save the newly created workbook
NewWorkBook.Close
'close the newly created workbook
Application.ScreenUpdating = True
End Sub
I have the following code shown below that allows me to save a copy of a workbook as an XLSX file with a file name that is different each time the code is ran. The code works to save the file correctly with the correct name. However, when it comes to the part where it should be copying and pasting the data in the new file as values it doesn't do it to the new file, only the original. My goal is to have a copy of the original file that does not have any macros or queries in it.
Can someone help create a way for the code to realize that it needs to do the copy and pasting in the new file?
Sub Macro1()
Dim PathName As String
Dim FileName As String
Dim AWorkbook As String
AWorkbook = "Operational Dashboard Worksheet"
PathName = Sheet4.Range("B7").Value
FileName = Sheet4.Range("B5").Value
Workbooks(AWorkbook).Save
Workbooks(AWorkbook).Sheets(Array("Dashboard", "Extra Details", "Worksheet", "Occupancy", "Shrinkage", _
"SL Impact", "VBA Codes")).Copy
ActiveWorkbook.SaveAs PathName & FileName & ".xlsx", FileFormat:=51
Workbooks(FileName).Activate
Sheet2.Range("Q:AD").Copy
Workbooks(FileName).Activate
Sheet2.Range("Q:AD").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Workbooks(FileName).Activate
Sheet3.Range("B:AI").Copy
Workbooks(FileName).Activate
Sheet3.Range("B:AI").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Workbooks(FileName).Activate
Sheet7.Range("N:AQ").Copy
Workbooks(FileName).Activate
Sheet7.Range("N:AQ").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Workbooks(FileName).Activate
Sheet5.Range("A:G").Copy
Workbooks(FileName).Activate
Sheet5.Range("A:G").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Workbooks(FileName).Activate
Sheet5.Range("AB:AS").Copy
Workbooks(FileName).Activate
Sheet5.Range("AB:AS").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Workbooks(FileName).Activate
Sheet5.Range("AX:CQ").Copy
Workbooks(FileName).Activate
Sheet5.Range("AX:CQ").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Workbooks(AWorkbook).Activate
Application.CutCopyMode = False
ActiveWorkbook.Close savechanges:=False
End Sub
Using Worksheet.Activate works, but it can get confusing swapping back and forth. Creating a reference to your Source and Destination Workbooks and sheets makes it easier (imo) to keep track of things, and it can also speed things up a little since you're just dealing with the data and not the gui.
' eg
Dim SourceBook As Wokbook
Set SourceBook = ThisWorkbook
I think your problem could be using SheetN.Range in your code to copy the data. SheetN probably looks to ThisWorkbook and not ActiveWorkbook for the data. So you are doing stuff to your original workbook.
The sheet index used in my code might not match up with your original code. You can replace the Index Sheets(Index) with the name of the sheet your trying to copy data on.
Sub Macro1()
Dim PathName As String
Dim FileName As String
Dim AWorkbook As String
AWorkbook = "Operational Dashboard Worksheet"
PathName = Sheet4.Range("B7").Value
FileName = Sheet4.Range("B5").Value
Workbooks(AWorkbook).Save
Workbooks(AWorkbook).Sheets(Array("Dashboard", "Extra Details", "Worksheet", "Occupancy", "Shrinkage", _
"SL Impact", "VBA Codes")).Copy
ActiveWorkbook.SaveAs PathName & FileName & ".xlsx", FileFormat:=51
Dim Book As Workbook
Set Book = Workbooks(FileName)
Book.Sheets(2).Range("Q:AD").Copy
Book.Sheets(2).Range("Q:AD").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Book.Sheets(3).Range("B:AI").Copy
Book.Sheets(3).Range("B:AI").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Book.Sheets(7).Range("N:AQ").Copy
Book.Sheets(7).Range("N:AQ").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Book.Sheets(5).Range("A:G").Copy
Book.Sheets(5).Range("A:G").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Book.Sheets(5).Range("AB:AS").Copy
Book.Sheets(5).Range("AB:AS").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Book.Sheets(5).Range("AX:CQ").Copy
Book.Sheets(5).Range("AX:CQ").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Book.Save
Application.CutCopyMode = False
ActiveWorkbook.Close savechanges:=False
End Sub
Update
I don't really use the SheetN (code name) so my understanding was not quite right. It's the name defined in the VBA editor for the sheet and when used does directly reference the sheet. (you can edit them in the VBA editor too so Sheet1->Dashboard?)
After testing it seems that when you use Workbooks(..).Worksheets(...).Copy like in your code and mine too it also copies those name to the new workbook. This is good, but you cant directly reference code names from another workbook.
Below is a modified version of my code that indirectly references them using some code I found. (not tested and not very pretty)
Sub Macro1()
Dim PathName As String
Dim FileName As String
Dim AWorkbook As String
AWorkbook = "Operational Dashboard Worksheet"
PathName = Sheet4.Range("B7").Value
FileName = Sheet4.Range("B5").Value
Workbooks(AWorkbook).Save
Workbooks(AWorkbook).Sheets(Array("Dashboard", "Extra Details", "Worksheet", "Occupancy", "Shrinkage", _
"SL Impact", "VBA Codes")).Copy
ActiveWorkbook.SaveAs PathName & FileName & ".xlsx", FileFormat:=51
Dim Book As Workbook
Set Book = Workbooks(FileName)
Dim Sheet2N As Worksheet
Set Sheet2N = GetWsFromCodeName(Book, "Sheet2")
Dim Sheet3N As Worksheet
Set Sheet3N = GetWsFromCodeName(Book, "Sheet3")
Dim Sheet5N As Worksheet
Set Sheet5N = GetWsFromCodeName(Book, "Sheet4")
Dim Sheet7N As Worksheet
Set Sheet7N = GetWsFromCodeName(Book, "Sheet7")
Sheet2N.Range("Q:AD").Copy
Sheet2N.Range("Q:AD").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheet3N.Range("B:AI").Copy
Sheet3N.Range("B:AI").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheet7N.Range("N:AQ").Copy
Sheet7N.Range("N:AQ").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheet5N.Range("A:G").Copy
Sheet5N.Range("A:G").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheet5N.Range("AB:AS").Copy
Sheet5N.Range("AB:AS").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheet5N.Range("AX:CQ").Copy
Sheet5N.Range("AX:CQ").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Book.Save
Application.CutCopyMode = False
ActiveWorkbook.Close savechanges:=False
End Sub
'http://yoursumbuddy.com/using-worksheet-codenames-in-other-workbooks/
Function GetWsFromCodeName(wb As Workbook, CodeName As String) As Excel.Worksheet
Dim ws As Excel.Worksheet
For Each ws In wb.Worksheets
If ws.CodeName = CodeName Then
Set GetWsFromCodeName = ws
Exit For
End If
Next ws
End Function
I haven't fully tested this but when I try to execute Workbooks("Stores").Activate in Immediate Pane when I know that Stores is open throws a Run-time error '9': Subscript out of range error.
If I add the file extension Workbooks("Stores.xlsx").Activate it works fine and executing ?ActiveWorkbook.Name returns Stores.xlsx.
So your:
Workbooks(FileName).Activate
Should be:
Workbooks(FileName & ".xlsx").Activate
Or you could add the extension when defining your FileName:
FileName = Sheet4.Range("B5").Value & "xlsx"
That said, you rarely need to Activate anything in VBA. For example:
Workbooks(FileName).Activate
Sheet2.Range("Q:AD").Copy
Workbooks(FileName).Activate
Sheet2.Range("Q:AD").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
could be replaced with:
Workbooks(FileName).Sheet2.Range("Q:AD").Copy
Workbooks(FileName).Sheet2.Range("Q:AD").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False