How to Fix Run-time Error 424 "Object Required" in Excel VBA - excel

I'm working on an Excel project where I am trying to produce certain rows from "Sheet 1" that contains a word called "external" in column C and then copy and paste that row into "Sheet 3"
I understand that there is a thing called "filter" but that is not an option.
This project is for my team at work that wants to be able to extract rows and columns that are shown as "external" and then be able to paste them and other information to another sheet that contains that information.
Private Sub CommandButton1_Click()
a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("Sheet1").Cells(i, 3).Value = "External" Then
Worksheets("Sheet1").Rows(i).Copy
Worksheets("Sheet3").Activate
b = Worksheets("Sheet3").Cells(Row.Count, 1).End(xlUp).Row
Worksheets("Sheet3").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Sheet1").Activate
End If
Next
Application.CutCopyMode = False
ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select
End Sub
The expected result was to display all rows that contained the word "External" in Sheet 1 Column C into a new sheet and have all its information displayed in Sheet 3.
Excel Worksheet for Reference:

First, declare all your variables. Next, you can try changing If Worksheets("Sheet1").Cells(i, 3).Value = "External" Then to If Worksheets("Sheet1").Range("C" & i).Text = "External" Then. See here:
Private Sub CommandButton1_Click()
Dim a As Long
Dim i As Long
Dim b As Long
a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("Sheet1").Range("C" & i).Text = "external" Then
Worksheets("Sheet1").Rows(i).Copy
Worksheets("Sheet3").Activate
b = Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet3").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Sheet1").Activate
End If
Next
Application.CutCopyMode = False
ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select
End Sub

Related

Unable to get my code to execute paste special

I have a small VBA code to copy a row from one sheet and paste to another, it works fine for paste but not for paste special, as I am trying to paste values only and not just paste.
this is my code, very basic. Noted that the pastespecial is changed to paste the code works fine.
thanks for you help
Private Sub CommandButton1_Click()
a = Worksheets("Inventory List Costing Review").Cells(Rows.Count, 1).End(xlUp).Row
For i = 10 To a
If Worksheets("Inventory List Costing Review").Cells(i, 19).Value = "Completed" Then
Worksheets("Inventory List Costing Review").Rows(i).Copy
Worksheets("Completed by Sales").Activate
b = Worksheets("Completed by Sales").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Completed by Sales").Cells(b + 1, 1).Select
ActiveSheet.PasteSpecial Paste:=xlPasteValues, operation:=xlNone
Worksheets("Inventory List Costing Review").Activate
End If
Next
Application.CutCopyMode = False
ThisWorkbook.Worksheets("Inventory List Costing Review").Cells(1, 1).Select
End Sub
PasteSpecial xlPasteValues vs Assigning Values
A Quick Fix
If you insist on using PasteSpecial, in the IF clause you can use:
Worksheets("Inventory List Costing Review").Rows(i).Copy
b = Worksheets("Completed by Sales").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Completed by Sales").Cells(b + 1, 1).PasteSpecial Paste:=xlPasteValues
But a better (more efficient) way is:
b = Worksheets("Completed by Sales").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Completed by Sales").Rows(b + 1).Value = _
Worksheets("Inventory List Costing Review").Rows(i).Value
when Application.CutCopyMode = False and ... Cells(1, 1).Select are not needed anymore.
Improvements
If you use Option Explicit, it will 'force' you to qualify all variables (a, b).
If you additionally qualify the workbook and worksheets, the code becomes quite readable.
Since the code can be run from a command button on any sheet, you can give it a suitable name and put it into a standard module. Then you can easily call it in the click event code of a command button (located in a sheet module).
Standard Module e.g. Module1
Option Explicit
Sub updateSales()
Dim wb As Workbook
Set wb = ThisWorkbook ' The workbook containing this code.
Dim src As Worksheet
Set src = wb.Worksheets("Inventory List Costing Review")
Dim tgt As Worksheet
Set tgt = wb.Worksheets("Completed by Sales")
Dim a As Long
Dim b As Long
a = src.Cells(src.Rows.Count, 1).End(xlUp).Row
For i = 10 To a
If src.Cells(i, 19).Value = "Completed" Then
b = tgt.Cells(tgt.Rows.Count, 1).End(xlUp).Row
tgt.Rows(b + 1).Value = src.Rows(i).Value
End If
Next
End Sub
Sheet Module e.g. Inventory List Costing Review and/or Completed by Sales
Option Explicit
Private Sub CommandButton1_Click()
updateSales
End Sub
The following should work (cleaned up a bit of clutter along the way).
Although if you're just copying data from cells it would be faster to assign the values directly to the destination cells instead of copy-pasting.
Private Sub CommandButton1_Click()
With Worksheets("Inventory List Costing Review")
a = .Cells(Rows.Count, 1).End(xlUp).Row
For i = 10 To a
If .Cells(i, 19).Value = "Completed" Then
.Rows(i).Copy
b = Worksheets("Completed by Sales").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Completed by Sales").Cells(b + 1, 1).PasteSpecial Paste:=xlPasteValues, operation:=xlNone
End If
Next
Application.CutCopyMode = False
.Cells(1, 1).Select
End With
End Sub

Copy a row from Sheet1 and paste it into Sheet 2 if color of a cell is green

I made this code to copy data from Sheet1 to Sheet2 if the color of the cell is green (after conditional formatting it turns green). But it is giving me error in the color condition. Any suggestions ?
Private Sub CommandButton1_Click()
a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("Sheet1").Interior.ColorIndex = 14 Then
Worksheets("Sheet1").Rows(i).Copy
Worksheets("Sheet2").Activate
b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet2").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Sheet1").Activate
End If
Next
Application.CutCopyMode = False
ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select
End Sub
Few things to consider, the For Loop will iterate through column A of Sheet1 and copy the full row to Sheet2 in the next available row, if it meets the criteria:
Private Sub CommandButton1_Click()
a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
If Worksheets("Sheet1").Cells(i, "A").Interior.ColorIndex = 14 Then
Worksheets("Sheet1").Rows(i).Copy
Worksheets("Sheet2").Range("A" & b).PasteSpecial xlPasteAll
Application.CutCopyMode = False
End If
Next i
End Sub
You set
Application.CutCopyMode = False
So there is nothing in the buffer to paste. Move that line to after the PasteSpecial
You'd be better off not copying and pasting. When you copy/paste you muck with the user's copy/paste buffer. It's generally better to assign values and other aspects directly:
myTargetRange.Value = mySourceRange.Value
myTargetRange.Formula = mySourceRange.Formula
myTargetRange.RowHeight = mySourceRange.RowHeight
etc.

VBA - Copy only visible cells from sheet to another worksheet

I have worksheet ("Formatted Data") and worksheet("Client_1 Data")
I run Macro which do following steps:
select worksheet("Fromatted Data")
autoFilter data in Column "C" with value "client_1"
copy selected columns from worksheet ("Formatted Data") and Paste data to worksheet("Client_1 Data")
What is my issue:
macro copy not only Data i filtered but all of them, veen if they are not visible.
My Macro Code:
Sub PRINT_AVIVA_ISA()
Sheets("Formatted Data").Select
ActiveSheet.Range("$A$1:$R$73").autofilter Field:=3, Criteria1:=Array( _
"client_1"), Operator:=xlFilterValues
Dim LastRow As Long, erow As Long
LastRow = Worksheets("Formatted Data").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To LastRow
Worksheets("Formatted Data").Cells(i, 2).Copy
erow = Worksheets("Client_1 Data").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Formatted Data").Paste Destination:=Worksheets("Client_1 Data").Cells(erow + 1, 1) ' --- account number
Worksheets("Formatted Data").Cells(i, 3).Copy
Worksheets("Formatted Data").Paste Destination:=Worksheets("Client_1 Data").Cells(erow + 1, 2) ' --- designation
Worksheets("Formatted Data").Cells(i, 4).Copy
Worksheets("Formatted Data").Paste Destination:=Worksheets("Client_1 Data").Cells(erow + 1, 3) ' --- fund name
Worksheets("Formatted Data").Cells(i, 5).Copy
Worksheets("Formatted Data").Paste Destination:=Worksheets("Client_1 Data").Cells(erow + 1, 4) ' --- fund code
Worksheets("Formatted Data").Cells(i, 7).Copy
Next i
End Sub
What i need:
put into my existing code something to copy only filtered data?
Thanks,
Peter.
The problem that you're running into is that you're looping through all of the cells in your 'formatted data' worksheet. The VBA code doesn't check to see if the cells have been filtered or not.
I'm attaching some code below that should do what you're attempting to do. I've made a few changes to clean it up a bit, such as storing sheets into their own variable so that you don't have to recurringly reference them directly.
Also, I opted to use direct value assignment as opposed to copy/paste. Assigning the value directly is usually quicker and has cleaner, more self-descriptive code. The tradeoff is that it doesn't copy over formatting. If you really need formatting, you can add it in once (either at the start or end of the routine, for the entire column).
See if you can adapt the below code and let us know if you need more help.
Sub PRINT_AVIVA_ISA()
Dim sData As Worksheet
Dim sClient As Worksheet
'Prevents the application from rendering graphical elements during processing
Application.ScreenUpdating = False
Set sData = Worksheets("Formatted Data")
Set sClient = Worksheets("Client_1 Data")
sData.Range("$A$1:$R$73").AutoFilter Field:=3, Criteria1:=Array( _
"client_1"), Operator:=xlFilterValues
LastRow = sData.Cells(Rows.Count, 1).End(xlUp).Row
Dim i As Long
For i = 2 To LastRow
If sData.Rows(i).Hidden = False Then
' Rather than add 1 to erow 4 times later, just calculate it here
erow = sClient.Cells(Rows.Count, 1).End(xlUp).Row + 1
sClient.Cells(erow, 1).Value = sData.Cells(i, 2).Value
sClient.Cells(erow, 2).Value = sData.Cells(i, 3).Value
sClient.Cells(erow, 3).Value = sData.Cells(i, 1).Value
End If
Next i
Application.ScreenUpdating = True
End Sub

Copy specific cell that match the condition and paste to another sheet

I'm trying to create a vba that copy the cell that match my condition and paste it to another sheet but my problem is it copy all rows with that matched with what I am looking for. I only need to copy the cell that matched. Here is my code
Sub format()
a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("Sheet1").Cells(i, 1).Value Like "*application_id*" Then
Worksheets("Sheet1").Cell(i).Copy
Worksheets("Sheet2").Activate
b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet2").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Sheet1").Activate
End If
Next
End Sub
For the beginning you should avoid all those activate stuff.
I gets a little confusing
I think your problem lies in: Worksheets("Sheet1").Rows(i).Copy
Sub format()
a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To a
If Worksheets("Sheet1").Rows(i, lColumn).Value Like "*application_id*" Then
Temp = Sheets("Sheet1").Cells(i,lColumn).value
b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
Sheets("Sheet2").Cells(b + 1, 1) = Temp
End If
Next i
End Sub

Excel VBA programming to copy specific rows from sheet1 to sheet2 specific cell

I want to write one vba code which will search data from a specific column of sheet1 and copy those rows and paste those to sheet2. I have written the code but found one problem there. here is the code below.
Sheets("Sheet1").Select
Range("D1").Select
Dim mycode As Worksheet
Set mycode = ThisWorkbook.Worksheets("Sheet2")
Dim i As Long
For i = 2 To Cells(Rows.Count, "D").End(xlUp).Row
If Cells(i, 4).Value = "high" Then
Range(Cells(i, 1), Cells(i, 8)).Copy Destination:=mycode.Range("A" & mycode.Cells(Rows.Count, "A").End(xlUp).Row + 1)
ElseIf Cells(i, 4).Value <> "high" Then
Sheets("Sheet2").Select
Range("A2").Value = "No Crtical Logs Found"
End If
Next i
End Sub
From sheet1 column D(number4) I am searching data matching "high" and copy paste those rows to sheet2. And if any of the "high" is not there in column D then "No action required" will be written in cell A2 on sheet2. But the problem is when "high" value is not there it is working fine but when "high" value is there in D column sheet1 then all time "no action required" value is coming on cell A2 on sheet2. Please help me to rectify this.
No Crtical Logs Found 4/11/2016 Critical high 192.168.1.1 This is the sample excel sheet Action Required
Regards,
Pinaki
You are moving off Sheet1 too soon:
Sub fhskdfh()
Sheets("Sheet1").Select
Range("D1").Select
Dim mycode As Worksheet
Set mycode = ThisWorkbook.Worksheets("Sheet2")
Dim i As Long
For i = 2 To Cells(Rows.Count, "D").End(xlUp).Row
MsgBox i & vbCrLf & Cells(i, 4).Value
If Cells(i, 4).Value = "high" Then
Range(Cells(i, 1), Cells(i, 8)).Copy Destination:=mycode.Range("A" & mycode.Cells(Rows.Count, "A").End(xlUp).Row + 1)
End If
Next i
End Sub
Change your destination of the "No Crtical Logs Found" message
From
Sheets("Sheet2").Select Range("A2").Value
To
Sheets("Sheet2").Range("A2").Value
You do not need to select the cell before assigning a value to it.

Resources