This is my first post here.
I am trying to copy data from one sheet to a new sheet in the same workbook after filtering in the main sheet.
I also have to copy the sheet template from a template sheet onto this new sheet where I am copying the data to before I copy the data.
This is the VBA code shown in my macro:
Sub Macro7()
'
' Macro7 Macro
'
'
Sheets("Template").Select
Rows("1:3").Select
Application.CutCopyMode = False
Selection.Copy
Dim sSheetName As String
Sheets.Add After:=Sheets(Sheets.Count)
sSheetName = ActiveSheet.Name
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("N13").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("sSheetName").Select
Range("D4").Select
ActiveSheet.Paste
Columns("D:D").EntireColumn.AutoFit
Sheets("Sheet1").Select
Range("A13").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("sSheetName").Select
Range("C4").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("D13").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("sSheetName").Select
Range("E4").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("B13").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("sSheetName").Select
Range("F4").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("H13").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("sSheetName").Select
Range("G4").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range("F13").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("sSheetName").Select
Range("I4").Select
ActiveSheet.Paste
Range("A4").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "Fives Cinetic Corp"
Columns("B:B").Select
Columns("A:A").ColumnWidth = 17.57
Range("A4").Select
Selection.AutoFill Destination:=Range("A4:A5")
Range("A4:A5").Select
Range("D10").Select
End Sub
I get an error: Run-time Error 9: Subscript out of range
Guess its something to do with sheet numbers but not able to figure it out exactly what it is.
Just incase you haven't solved this already, take a look at the code I've adapted for you. When making new VBA projects, play around with some of these methods, you'll find they're a lot faster and more reliable than your previous version. Compare them side by side.
This took a lot of assumption on my part so make a backup before running this to test it and make sure everything is going to the right place.
Sub Macro7()
Sheets.Add After:=Sheets(Sheets.Count)
NewSheet = ActiveSheet.Name
Sheets("Template").Rows("1:3").Copy Destination:=ActiveSheet.Range("A1")
Sheets("Sheet1").Activate
Range("N13").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Destination:=Sheets(NewSheet).Range("D4")
With Sheets(NewSheet)
Columns("D:D").EntireColumn.AutoFit
End With
Range("A13").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Destination:=Sheets(NewSheet).Range("C4")
Range("D13").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Destination:=Sheets(NewSheet).Range("E4")
Range("B13").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Destination:=Sheets(NewSheet).Range("F4")
Range("H13").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Destination:=Sheets(NewSheet).Range("G4")
Range("F13").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Destination:=Sheets(NewSheet).Range("I4")
Sheets(NewSheet).Activate
Range("A4").Value = "Fives Cinetic Corp"
Columns("A:A").AutoFit
Range("A4").AutoFill Destination:=Range("A4:A5")
Range("D10").Select
End Sub
Related
I recorded a macro to create a button that would filter my contracts to find only the ones with a - in them and copy and paste them into another sheet and got this:
Sub Contracts_Hyphen()
'
' Contracts_Hyphen Macro
'
'
Sheets("Transactions").Select
ActiveSheet.Range("$A$1:$AA$31579").AutoFilter Field:=5, Criteria1:=Array( _
"17030-89", "39975-41468-43641-45775-48215-49324", "40011-41747-46077", _
"43642-45773", "43773-46237", "46078-46771", "46238-46409"), Operator:= _
xlFilterValues
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Contracts").Select
Range("A1").Select
ActiveSheet.Paste
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.AutoFilter
Range("A1").Select
End Sub
The problem with this code is that if I added another contract with the number for example "18936-87645" this macro would not find it. So what I changed it to was:
Sub Contracts_Hyphen()
'
' Contracts_Hyphen Macro
'
'
Sheets("Transactions").Select
ActiveSheet.Range("$A$1:$AA$31579").AutoFilter Field:=5, Criteria1:="-"
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Contracts").Select
Range("A1").Select
ActiveSheet.Paste
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.AutoFilter
Range("A1").Select
End Sub
But now with that change it is not finding anything. So how can I setup my macro to include numbers with hyphens even potentially newly added numbers?
I use the following VBA to extend the deadline of over-due (due today) tasks in the end of the day. However, I realized that applying the script twice (I linked the script to a button, which I accidentally pressed twice) results in all task-dates (and also the tasks with no date assigned) to be repalaced by tomorrow's date or get a date (next day).
How can I avoid this unwanted behavior? It seems the selection process of the dates to be changed is distored when applying the script twice.
Sub To_Do_Add_Day_Deadline()
'
' To_Do_Add_Day_Deadline Makro
'
'
Range("C2").Select
ActiveSheet.ListObjects("Tabelle113").Range.AutoFilter Field:=3, Criteria1 _
:=xlFilterToday, Operator:=xlFilterDynamic
Range("C4").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("C4").Select
ActiveCell.FormulaR1C1 = "=TODAY()+1"
Range("C4").Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("E6").Select
ActiveSheet.ListObjects("Tabelle113").Range.AutoFilter Field:=3
Range("C4").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("E5").Select
End Sub
I replace Range("C4") with Range("C2"), because when you apply a filter, the table constraint their rows, and ever start at the next line after the header Range(""). I test this in a Table with a Range("A1:E25") with a header.
Sub To_Do_Add_Day_Deadline()
Range("C2").Select
ActiveSheet.ListObjects("Tabelle113").Range.AutoFilter Field:=3, Criteria1 _
:=xlFilterToday, Operator:=xlFilterDynamic
Range("C4").Select
Range(Selection, Selection.End(xlDown)).Select
Selection = CDate(Left(CDate(Now) + 1, 10))
Application.CutCopyMode = False
Range("E6").Select
ActiveSheet.ListObjects("Tabelle113").Range.AutoFilter Field:=3
End Sub
Edit: I reduce the code and replace "=TODAY()+1" with CDate(Left(CDate(Now) + 1, 10))
I changed the approach and now use the following code, which works fine:
Sub On_Hold_Add_One_Day()
'
' On_Hold_Add_One_Day
'
'
Columns("E:E").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("E4").Select
ActiveCell.FormulaR1C1 = "=IF([#Deadline]=TODAY(),[#Deadline]+1,[#Deadline])"
Range("E4").Select
ActiveWindow.SmallScroll Down:=-9
Range("E4").Select
ActiveWindow.SmallScroll Down:=0
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ActiveWindow.SmallScroll Down:=-36
Range("C4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWindow.SmallScroll Down:=-9
Columns("E:E").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Cells.Select
Selection.Rows.AutoFit
Range("E6").Select
End Sub
I have a MS Visual Basic macro for my excel sheet, I have a child workbook and a parent workbook. I want to copy the cells from the child worksheet "account" into the parent worksheet "account". the cells in the child sheet have some blank cells, currently with this code, it stops at the blank cell, I want it to miss the blank cell and go to the next cell with values and then keep copying.
Sub Button1_Click()
'Field Name
Windows("childsheet.xlsm").Activate
Range("A3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("parentsheet.xlsm").Activate
Range("A3").Select
ActiveSheet.Paste
'API Name
Windows("childsheet.xlsm").Activate
Range("B3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("parentsheet.xlsm").Activate
Range("B3").Select
ActiveSheet.Paste
'Type
Windows("childsheet.xlsm").Activate
Range("C3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("parentsheet.xlsm").Activate
Range("C3").Select
ActiveSheet.Paste
'Length
Windows("childsheet.xlsm").Activate
Range("D3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("parentsheet.xlsm").Activate
Range("D3").Select
ActiveSheet.Paste
'Required
Windows("childsheet.xlsm").Activate
Range("E3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("parentsheet.xlsm").Activate
Range("E3").Select
ActiveSheet.Paste
'Read Only?
Windows("childsheet.xlsm").Activate
Range("F3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("parentsheet.xlsm").Activate
Range("F3").Select
ActiveSheet.Paste
End Sub
it works. it copies each column that i specify but when it gets to a column that has empty cells it copy any info. in that cell from top to bottom but if it encounters a blank it stops there and then moves onto the next column. I want it to copy all info.
Instead of
Windows("childsheet.xlsm").Activate
Range("A3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("parentsheet.xlsm").Activate
Range("A3").Select
ActiveSheet.Paste
Use something like
With Workbooks("childsheet.xlsm").ActiveSheet
.Range("A3", .Cells(.Rows.Count, "A").End(xlUp)).Copy Destination:=Workbooks("parentsheet.xlsm").ActiveSheet.Range("A3")
.Range("B3", .Cells(.Rows.Count, "B").End(xlUp)).Copy Destination:=Workbooks("parentsheet.xlsm").ActiveSheet.Range("B3")
'… and so on …
End With
A further improvement is to replace .ActiveSheet with the sheet name like .Worksheets("YourSheetName") so your code is more reliable.
I've created a macro to organize a data set and compile into another sheet in a way that makes more sense for doing analyses. The set originally is comprised of columns for user, timestamp and 3 possible events. The user could appear on multiple rows but I wanted to look at this data set by user and have a separate column for each timestamp. The macros I've made can successfully clean, filter by event type, and separate by event type into separate worksheets (no matter how many rows of data) but I'm having trouble with compiling data into one sheet using vlookup AND accounting for a variable number of rows. I have looked at other answers to this question and tried this:
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],'Email Opened'!R1C1:R" & LastRow0 & "C3,2,FALSE)"
... but it keeps giving me errors.
What I have below (Vlookup_events2) works but just not for the entire variable number of rows.Please help me adjust the code for the vlookup so it will work no matter how many rows.
Here is the code below for separating data (just for reference), then the problem macro - compiling it with vlookup. I would really appreciate some help, I know there's an amazing VBA expert out there!
Sheets.Add
Sheets("Sheet1").Name = "Email Sent"
ActiveSheet.Next.Select
Selection.AutoFilter
ActiveCell.Offset(0, 2).Range("A1").Select
ActiveSheet.Range("$A$1:$D$1000000").AutoFilter Field:=3, Criteria1:= _
"=Campaign Created", Operator:=xlOr, Criteria2:="=Email Sent"
ActiveCell.Offset(0, -2).Range("A1:D2355").Select
ActiveCell.Activate
Selection.Copy
ActiveSheet.Previous.Select
Range("A1").Select
ActiveSheet.Paste
ActiveSheet.Next.Select
Range("A1").Select
ActiveSheet.Range("$A$1:$D$1000000").AutoFilter Field:=3, Criteria1:= _
"=Campaign Created", Operator:=xlOr, Criteria2:="=Email Opened"
Sheets.Add
Sheets("Sheet2").Name = "Email Opened"
ActiveSheet.Next.Select
ActiveCell.Range("A1:D1000000").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Previous.Select
Range("A1").Select
ActiveSheet.Paste
ActiveCell.Offset(0, 3).Range("A1").Select
Application.CutCopyMode = False
Selection.ClearContents
ActiveCell.Columns("A:A").EntireColumn.Select
Selection.Delete Shift:=xlToLeft
Sheets.Add
Sheets("Sheet3").Name = "Clicked Link"
ActiveSheet.Next.Select
ActiveSheet.Next.Select
ActiveCell.Offset(0, 2).Range("A1").Select
ActiveSheet.Range("$A$1:$D$1000000").AutoFilter Field:=3, Criteria1:= _
"=Campaign Created", Operator:=xlOr, Criteria2:="=Clicked Link"
ActiveCell.Offset(0, -2).Range("A1:D1000000").Select
ActiveCell.Activate
Selection.Copy
ActiveSheet.Previous.Select
ActiveSheet.Previous.Select
Range("A1").Select
ActiveSheet.Paste
ActiveCell.Offset(0, 3).Columns("A:A").EntireColumn.Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("A1").Select
End Sub
Sub Vlookup_events2()
' Vlookup_events2 Macro
ActiveSheet.Previous.Select
Range("A2").Select
ActiveCell.FormulaR1C1 = "user"
Range("A3").Select
ActiveSheet.Next.Select
Range("A2").Select
ActiveCell.FormulaR1C1 = "user"
Range("A3").Select
ActiveSheet.Next.Select
Range("A2").Select
ActiveCell.FormulaR1C1 = "user"
Range("A3").Select
Sheets.Add
Sheets("Sheet4").Name = "Compiled Events"
ActiveSheet.Previous.Select
ActiveSheet.Previous.Select
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ActiveSheet.Next.Select
ActiveSheet.Next.Select
Range("A1").Select
ActiveSheet.Paste
Columns("C:D").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("B1").Select
ActiveCell.FormulaR1C1 = "Email Sent Time"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Email Opened Time"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Clicked Link Time"
Range("A1").Select
Application.Goto Reference:="R2C3"
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],'Email Opened'!R1C1:R601C3,2,FALSE)"
Range("C3").Select
Range(Selection, Selection.End(xlUp)).Select
Columns("C:C").Select
Selection.FormulaR1C1 = "=VLOOKUP(RC[-2],'Email Opened'!R1C1:R601C3,2,FALSE)"
Columns("D:D").Select
Selection.FormulaR1C1 = "=VLOOKUP(RC[-3],'Clicked Link'!R1C1:R56C3,2,FALSE)"
Columns("C:C").Select
Selection.NumberFormat = "m/d/yyyy h:mm"
Columns("D:D").Select
Selection.NumberFormat = "m/d/yyyy h:mm"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Email Opened Time"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Clicked Link Time"
Range("C2").Select
End Sub
Hello I have this Excel Macro of generating a new sheet by copy pasting certain stuff from an existing sheet. Basically the first three paragraphs are being repeated thrice, and the final 10th paragraph is just to populate a column. However for the 7th, 8th and 9th para, its showing me
'Error 1004 copy and paster areas are not the same' whereas it is working above without any such problem.
Sub Macro1()
'
' Macro1 Macro
'
'
Sheets.Add After:=ActiveSheet
Sheets("Sheet1").Select
Sheets("Sheet1").Move Before:=Sheets(2)
Range("A1").Select
ActiveCell.FormulaR1C1 = "Product Number"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Product Desc"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Service Type"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Service Level"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Service P/N"
Range("F1").Select
ActiveCell.FormulaR1C1 = "APAC(USD)"
Range("F2").Select
Columns("A:A").ColumnWidth = 14.27
Columns("B:B").ColumnWidth = 15.13
Columns("C:C").ColumnWidth = 13.27
Columns("D:D").ColumnWidth = 13.13
Columns("E:E").ColumnWidth = 14.33
Columns("F:F").ColumnWidth = 12.07
Sheets("1. SMARTnet ").Select
Range("B9:C20694").Select
Selection.Copy
Sheets("Sheet1").Select
Range("A2").Select
ActiveSheet.Paste
Columns("B:B").ColumnWidth = 40
Sheets("1. SMARTnet ").Select
Range("D9:E20694").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("E2").Select
ActiveSheet.Paste
Range("D2").Select
Sheets("1. SMARTnet ").Select
Range("D1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("D2").Select
ActiveSheet.Paste
Columns("D:D").ColumnWidth = 17.07
Range("D3").Select
ActiveSheet.Paste
Range("D2:D3").Select
Selection.AutoFill Destination:=Range("D2:D20687"), Type:=xlFillDefault
Range("D2:D20687").Select
Sheets("1. SMARTnet ").Select
Range("B9:C20694").Select
Selection.Copy
Sheets("Sheet1").Select
Range("A20688").Select
ActiveSheet.Paste
Sheets("1. SMARTnet ").Select
Range("F9:G20694").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("E20688").Select
ActiveSheet.Paste
Range("D2").Select
Sheets("1. SMARTnet ").Select
Range("F1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("D20688").Select
ActiveSheet.Paste
Range("D20689").Select
ActiveSheet.Paste
Range("D20688:D20689").Select
Selection.AutoFill Destination:=Range("D20688:D41373"), Type:=xlFillDefault
Range("D20688:D41373").Select
Sheets("1. SMARTnet ").Select
Range("B9:C20694").Select
Selection.Copy
Sheets("Sheet1").Select
Range("A41374").Select
ActiveSheet.Paste
Sheets("1. SMARTnet ").Select
Range("H9:I20694").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("E41374").Select
ActiveSheet.Paste
Range("D2").Select
Sheets("1. SMARTnet ").Select
Range("H1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("D41374").Select
ActiveSheet.Paste
Range("D41375").Select
ActiveSheet.Paste
Range("D41374:D41375").Select
Selection.AutoFill Destination:=Range("D41374:D62059"), Type:=xlFillDefault
Range("D41374:D62059").Select
Sheets("1. SMARTnet ").Select
Range("B9:C20694").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("A62060").Select
ActiveSheet.Paste
Sheets("1. SMARTnet ").Select
Range("P9:P20694,U9:U20694").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("E62060").Select
ActiveSheet.Paste
Range("D2").Select
Sheets("1. SMARTnet ").Select
Range("P1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("D62060").Select
ActiveSheet.Paste
Range("D62061").Select
ActiveSheet.Paste
Range("D62060:D62061").Select
Selection.AutoFill Destination:=Range("D62060:D82745"), Type:=xlFillDefault
Range("D62060:D82745").Select
Range("C2").Select
ActiveCell.FormulaR1C1 = "SMARTnet"
Selection.AutoFill Destination:=Range("C2:C82745"), Type:=xlFillDefault
Range("C2:C82745").Select
End Sub
Any hint will be appreciated :)
I think you should simplifi your code by not using "select"
for Example
instead of
Sheets("1. SMARTnet ").Select
Range("B9:C20694").Select
Selection.Copy
Sheets("Sheet1").Select
Range("A2").Select
ActiveSheet.Paste
You can write as
Sheets("1. SMARTnet ").Range("B9:C20694").Copy Sheets("Sheet1").Range("a2")
Then you will be able to find the error better
This will also run faster because it saves screen updating time.