I'm having trouble trying to get this code to work. It does work as intended without the (ByValue Target as Range) portion along with the ActiveCell.Value, but with these included in, I'm getting the error listed.
I have a number of other functions depending on this and would like to see what I could fix.
Thanks a lot!
Private Sub CT(ByVal Target As Range)
'
' CT Macro
'
Sheets("Outbound Tactics").Select
If ActiveCell.Value = "Yes" Then
ActiveCell.Select
Range(Selection, ActiveCell.Offset(0, 23)).Select
Selection.Copy
Sheets("Completed Tactics").Select
ActiveSheet.Range("C4").Select
Selection.End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Select
Application.CutCopyMode = False
Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
Sheets("Outbound Tactics").Select
ActiveCell.Select
Range(Selection, ActiveCell.Offset(0, 23)).Select
Selection.Delete
End If
End Sub
PS: From what I understand, the beginning portion is there in order to have the macro run continuously without being called in.
Seeing as you don't actually use the Target object in your code I'm assuming you copy/pasted this from somewhere and are not actually sure what it's there for.
Simple answer: remove it.
Private Sub CT()
'// your code here
End Sub
Extended Answer: incorporate it into your code.
Private Sub CT(ByVal Target As Range)
If Target.Value = "Yes" Then
With Sheets("Completed Tactics").Range("C4").End(xlDown).Offset(1, 0)
.Resize(1, 24).Value = Target.Resize(1, 24).Value
.Offset(1, 0).EntireRow.Insert CopyOrigin:=xlFormatFromLeftOrAbove
End With
Target.Resize(1, 24).Delete
End If
End Sub
Related
I have 2 worksheets (Input & Record), I just want to copy some data from "Input" to "Record", it worked but if I protect "Record".... Paste method of Worksheet class Failed comes up.
So I added script for unprotected sheets and protect sheets, but '1004' stil comes up.
Here the detail of my project.
Input sheet is area for me to input some value to a row. In 1 row at least have 10 values at different column.
Maximum row that I can add is ten rows.
Record sheet is database as Table1 based on how much row from Input sheet that i'll add.
Here my script
Sub adddata() 'this sub code from button on "Input" sheet
Sheets("Input").Select
Range("C15").Offset(1, 0).Select 'select range start from C16
If Range("M27") = 1 Then 'value for how much row that i'll add
Range(Selection, Selection.End(xlToRight)).Select
Else
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
End If
Selection.Copy
Sheets("Record").Select
Worksheets("Record").Unprotect Password:="4321"
If Range("B2").Offset(1, 0).Value = "" Then
Range("B2").Offset(1, 0).Select
Else
Range("B2").End(xlDown).Offset(1, 0).Select
End If
ActiveSheet.Paste '<< The trouble maker
Application.CutCopyMode = False
Worksheets("Record").Protect Password:="4321", UserInterfaceOnly:=True
End Sub
That I want to know:
What I've missed?
The solution of this dilemma.
I think all this Select process is not necessary and you can avoid that. I can edit my answer if you will add your intentions "what you are trying to do".
You want to copy some range from Input and paste it to Record always to the next empty row?
If I understood you correct, maybe something like this?
Sub adddata()
On Error GoTo ErrorHandler
Application.ScreenUpdating = False
ThisWorkbook.Worksheets("Record").Unprotect Password:="4321"
Dim NextFreeCell As Range
Set NextFreeCell = ThisWorkbook.Worksheets("Record").Cells(Rows.Count, "B").End(xlUp).Offset(RowOffset:=1)
With ThisWorkbook.Worksheets("Input")
If .Range("B2").Value = 1 Then
.Range("C15", .Range("C15").End(xlToRight)).Copy
Else
.Range("C15", .Range("C15").End(xlDown).End(xlToRight)).Copy
End If
End With
NextFreeCell.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ThisWorkbook.Worksheets("Record").Protect Password:="4321", UserInterfaceOnly:=True
ThisWorkbook.Worksheets("Input").Activate
Application.ScreenUpdating = True
Application.CutCopyMode = False
Exit Sub
ErrorHandler:
Application.CutCopyMode = False
ThisWorkbook.Worksheets("Input").Activate
Application.ScreenUpdating = True
ThisWorkbook.Worksheets("Record").Protect Password:="4321", UserInterfaceOnly:=True
End Sub
Here is how it works:
This was not included in your original question. So you have to create a new question with additional information to your original question. However this time I will answer here but not next time.
Here is the code for table:
Sub adddata()
On Error GoTo ErrorHandler
Application.ScreenUpdating = False
ThisWorkbook.Worksheets("Record").Unprotect Password:="4321"
With ThisWorkbook.Worksheets("Record").ListObjects("Table1").ListRows.Add
With ThisWorkbook.Worksheets("Input")
If .Range("B2").Value = 1 Then
.Range("C15", .Range("C15").End(xlToRight)).Copy
Else
.Range("C15", .Range("C15").End(xlDown).End(xlToRight)).Copy
End If
End With
.Range.Cells(1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With
ThisWorkbook.Worksheets("Record").Protect Password:="4321", UserInterfaceOnly:=True
ThisWorkbook.Worksheets("Input").Activate
Application.ScreenUpdating = True
Application.CutCopyMode = False
Exit Sub
ErrorHandler:
Application.CutCopyMode = False
ThisWorkbook.Worksheets("Input").Activate
Application.ScreenUpdating = True
ThisWorkbook.Worksheets("Record").Protect Password:="4321", UserInterfaceOnly:=True
End Sub
Remove all empty cells in table below your last data. This code will add a new line to table. Also table name should correspond to your table name. Can be found in Excel under Format Table
Try this.
Sub adddata()
Worksheets("Record").Unprotect Password:="4321" ' Unlock the target sheet before copying.
Sheets("Input").Select
Range("C15").Offset(1, 0).Select
If Range("M27") = 1 Then
Range(Selection, Selection.End(xlToRight)).Select
Else
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
End If
Selection.Copy
Sheets("Record").Select
If Range("B2").Offset(1, 0).Value = "" Then
Range("B2").Offset(1, 0).Select
Else
Range("B2").End(xlDown).Offset(1, 0).Select
End If
Activecell.PasteSpecial xlAll
Application.CutCopyMode = False
Worksheets("Record").Protect Password:="4321", UserInterfaceOnly:=True
End Sub
I have an If statement that runs on the change of a cell. This part works fine. However, when it runs the macro, for some reason it adds is about 40 extra lines. I'v used the breakpoint and discovered that the lines are added in after the the paste special. Can anyone tell me why?
Thanks in advance.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$AG$4" Then
Call CapEx_Copy_Paste_Delete
End If
End Sub
Sub CapEx_Copy_Paste_Delete()
'
' CapEx_Copy_Paste_Delete Macro
'
'
Rows("11:11").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("B4:AG4").Select
Selection.Copy
Range("B11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("AG4").Select
Selection.ClearContents
Range("B4:E4").Select
Selection.ClearContents
Range("H4:I4").Select
Selection.ClearContents
Range("L4:M4").Select
Selection.ClearContents
Range("P4:Q4").Select
Selection.ClearContents
Range("T4:U4").Select
Selection.ClearContents
Range("X4:Y4").Select
Selection.ClearContents
Range("Z4").Select
Selection.ClearContents
Range("AA4").Select
Selection.ClearContents
Range("AC4").Select
Selection.ClearContents
Range("AD4").Select
Selection.ClearContents
Range("B4").Select
End Sub
Does this work any better??:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$AG$4" Then
Application.EnableEvents = False
Call CapEx_Copy_Paste_Delete
Application.EnableEvents = True
End If
End Sub
Here is the neater version of your code. It's likely all that use of Select isn't helping your problems:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$AG$4" Then
Application.EnableEvents = False
Call CapEx_Copy_Paste_Delete
Application.EnableEvents = True
End If
End Sub
Sub CapEx_Copy_Paste_Delete()
Dim ws As Worksheet
Dim arrRanges As Variant, v As Variant
'set this as the worksheet you want to update
Set ws = ThisWorkbook.Worksheets("Sheet1")
'set this as the ranges you want to clear
arrRanges = Array("AG4", "B4:E4", "H4:I4", "L4:M4", "P4:Q4", "T4:U4", "X4:Y4", "Z4")
With ws
.Rows("11:11").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
.Range("B4:AG4").Copy
.Range("B11").PasteSpecial Paste:=xlPasteValues
For Each v In arrRanges
.Range(v).ClearContents
Next v
End With
End Sub
Updated to include Gary's Student's suggestion - all credit goes to him for suggesting you disable events in your first sub
So, I have a simple macro that just clears out specific cells. It worked fine, and then I added a userform that takes input via textbox and I added the last line TextBox1.Value= "" to clear it. Now, clicking the button that runs the macro works, it even clears the box, but then it gives me the 424 error. I'm sure its something basic I'm overlooking, syntax or some closing argument. Any help would be appreciated.
Sub Clear()
'
' Clear Macro
'
'
Range("A1").Select
Selection.ClearContents
Range("H17").Select
Selection.ClearContents
Range("H11").Select
Selection.ClearContents
Range("B1").Select
Selection.ClearContents
Range("I4").Select
Selection.ClearContents
Range("K4").Select
Selection.ClearContents
Range("M4").Select
Selection.ClearContents
Range("H10").Select
Selection.ClearContents
Range("H16").Select
Selection.ClearContents
TextBox1.Value = ""
End Sub
Sub notebutton1_Click()
Range("K8").Select
Selection.Copy
End Sub
Sub notebutton2_Click()
Range("K13").Select
Selection.Copy
End Sub
Sub notebutton3_Click()
Range("K18").Select
Selection.Copy
End Sub
TextBox1.Value = "" should be accompanied with the form it is living in. So for example Userform1.TextBox1.Value = ""
I have an issue with the following code.
I want each cell with the value "long" in the column "U" to be copied in a new sheet.
But the code I developed only retrieves the first result. It does stop after "U6".
Can you please help me?
Sub reportcrea ()
Worksheets("TLM").Select
ActiveSheet.Range("U3").Select
Do
If ActiveCell.Value = "long" Then
ActiveCell.EntireRow.Select
Selection.Copy
Sheets("report").Select
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Else
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell.Offset(1, 0))
End sub ()
I found a bug in your code in this line:
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
Offset takes two parameters, so it should be something like this:
Range("A" & Rows.Count).End(xlUp).Offset(1,0).Select
Also, you should cancel CutCopy mode right after you paste what is in the clipboard:
ActiveSheet.Paste 'Paste is done here
Application.CutCopyMode = False 'This is a good practice
See if that helps. Also, a screenshot of the TLM sheet would help us analyze the problem more accurately.
First up, End Sub shouldn't have trailing brackets. When I copied it into a module it highlighted an error straight away.
Your loop is using ActiveCell.Offset(1, 0).Select twice:
If ActiveCell.Value = "long" Then
ActiveCell.EntireRow.Select
Selection.Copy
ActiveCell.Offset(1, 0).Select 'first Offset
Sheets("report").Select
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Sheets("TLM").Select
ActiveCell.Offset(1, 0).Select 'second Offset
Else
so you're only looking at every second row after each "long" is found.
I tested your code on 10 contoguous "long" cells and got 5 back in the report sheet. I couldn't reproduce your U6 stop.
Looking for help on a macro to take chunks of data on further rows, and place them into columns instead.
I've attached a picture to depict this. All of the chunks of data will split determined by the first column, 1 or 2 in the picture. I simply want to move chunk two up and next to 1. The only problem I've run into is that for each chunk, the number of columns is variable.
Edit: Image link incase the embedded isn't showing up: enter link description here
Would this be relatively close?
Sub macro()
Dim wav_name As String
Range("A1").Select
Do While ActiveCell.Value <> ""
ActiveCell.Offset(0, 2).Select
wav_name = ActiveCell.Value
ActiveCell.Offset(1, 0).Select
Do
If ActiveCell.Value = wav_name Then
ActiveCell.Offset(1, 0).Select
Else
Exit Do
End If
Loop
Range(ActiveCell.Offset(0, -2), ActiveCell.End(xlDown)).Select
Selection.Cut
ActiveCell.End(xlUp).Offset(0, 3).Select
ActiveSheet.Paste
Loop
Range("A1").Select
End Sub
What you have there is pretty workable with a one key exception.
Your cut selection is only grabbing the first row of data. You will need to change it to
Range(ActiveCell).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
To handle the variable number of columns, you can capture the last column in section one by adding a varabile (i.e. LastCol) and putting the following code in your Do Loop
LastCol = Activecell.End(xlToRight).Column
Then replace the 3 in your last offset statement with your variable
Note that you can refactor the code to remove many of the select statements (includeing the ones I have mentioned above) if you need to improve the preformance of your code, but what you have written will work for you.
EDIT: Here is what your end code would look like
Sub macro()
Dim wav_name As String
Dim LastCol as Long
Range("A1").Select
Do While ActiveCell.Value <> ""
ActiveCell.Offset(0, 2).Select
wav_name = ActiveCell.Value
ActiveCell.Offset(1, 0).Select
LastCol = Activecell.End(xlToRight).Column
Do
If ActiveCell.Value = wav_name Then
ActiveCell.Offset(1, 0).Select
Else
Exit Do
End If
Loop
Range(ActiveCell.Offset(0, -2), ActiveCell.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Cut
ActiveCell.End(xlUp).Offset(0, LastCol +1).Select
ActiveSheet.Paste
Loop
Range("A1").Select
End Sub
I haven't tested this, so you may have to do some debugging... but it is now logically correct.