VBA Copy/Paste Worksheet Loop Object Error

gciardi

New Member
Joined
Apr 29, 2015
Messages
1
A newbie to VB coding here. I have a small piece of code that has me stumped. I am using Excel 2012. The purchase of the code is to allow the user to enter one Ticker symbol that resides in two Excel sheets. Each sheet has the ticker symbol in column A, date in col B, formula in column C, and then price data in columns D through M.

After the user selects the ticker to be removed using an InputBox, the code then prompts for confirmation before proceeding. It then loops through two worksheets. The next process in the code is to delete the entire row where the selected ticker is located and then proceeds to copy the formula in column C down to the bottom of the column in the same worksheet. The code works to delete the tickers from both sheets, copies the formula in column C on the HiP tab, but then breaks when attempting to perform the same copy task on the LowP tab. This is where I can't figure out why the code doesn't work. Run-time error '1004' Method 'Range' of object '_Worksheet' failed.

Here's the code (command button located on HiP tab) activated when user clicks it:

Sub CommandButton2_Click()


Dim Response As String
Dim Ticker As String
Dim ws As Worksheet
Dim nrows As Integer ' row number where ticker is found
Dim rFound As Range



Range("A1").Select
Application.ScreenUpdating = False

Ticker = InputBox("Enter ticker symbol to delete.")

Set rFound = Cells.Find(What:=Ticker, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

If rFound Is Nothing Then
MsgBox ("Ticker not found. Try again!")
Exit Sub

End If

Response = MsgBox("We have found ticker " & UCase(Ticker) & " in row " & _
rFound.Row & " Proceed to delete it?", vbYesNo)

If Response = vbNo Then
Exit Sub


End If

nrows = rFound.Row

'Delete rows and copy/paste formulas in col. C across all worksheets
For Each ws In Sheets(Array("HiP", "LowP"))

With ws
.Activate
.Rows(nrows).EntireRow.Delete
.Range("C" & nrows - 1).Select
'***Code blows up here, but only on the second loop through LowP worksheet
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
.Paste

End With

Next ws

Application.ScreenUpdating = True

End Sub


I also would welcome and refinements to the code to make it easier or cleaner.

Thanks.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

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