Problem with VBA code

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,338
Office Version
  1. 365
Platform
  1. Windows
This filters and copies then errors where it should be pasting.

Sheets("CMOPUpload").Range("B" & lstrw).Paste


Code:
Private Sub CommandButton1_Click()

'Dim CMOP As Worksheet
Dim lstrw As Long

'Set CMOP = Worksheets("Sheet9")

Set lstwr = Sheets("CMOPUpload").Range("B" & Rows.Count).End(xlUp).Offset(1)

ActiveSheet.Range("A4").AutoFilter Field:=6, Criteria1:="Yes", Operator:=xlFilterValues
 
    Range("A3").Select
    ActiveCell.Offset(1, 0).Select
    
    Range(Selection, Selection.End(xlDown).Offset(0, 4)).Copy
    
    Sheets("CMOPUpload").Range("B" & lstrw).Paste
    

    Worksheets("ItemIDList").AutoFilterMode = False
    Application.CutCopyMode = False


End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Use PasteSpecial - the Range object doesn't have a Paste method.
 
Upvote 0
You have some errors:

Code:
Set lstwr = Sheets("CMOPUpload").Range("B" & Rows.Count).End(xlUp).Offset(1)

needs to be

Code:
lst[COLOR=#ff0000]rw[/COLOR] = Sheets("CMOPUpload").Range("B" & Rows.Count).End(xlUp).Offset(1)[COLOR=#ff0000].Row[/COLOR]

then you can replace this:

Code:
Range(Selection, Selection.End(xlDown).Offset(0, 4)).Copy
Sheets("CMOPUpload").Range("B" & lstrw).Paste

with either:

Code:
Range(Selection, Selection.End(xlDown).Offset(0, 4)).Copy Sheets("CMOPUpload").Range("B" & lstrw)

or

Code:
Range(Selection, Selection.End(xlDown).Offset(0, 4)).Copy
Sheets("CMOPUpload").Range("B" & lstrw).PasteSpecial xlPasteValues

See if that works.
 
Upvote 0
In your code, you haven't assigned a row number to lstrw

Also, "Set lstwr" ?
 
Upvote 0
Code:
Private Sub CommandButton1_Click()


'Dim CMOP As Worksheet
Dim lstrw As Range


'Set CMOP = Worksheets("Sheet9")


Set lstrw = Sheets("CMOPUpload").Range("B" & Rows.Count).End(xlUp).Offset(1)


ActiveSheet.Range("A4").AutoFilter Field:=6, Criteria1:="Yes", Operator:=xlFilterValues


Range([A4], [A4].End(xlDown).Offset(0, 4)).Copy lastrw


Worksheets("ItemIDList").AutoFilterMode = False


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,453
Members
448,898
Latest member
drewmorgan128

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top