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.
 

Forum statistics

Threads
1,082,151
Messages
5,363,434
Members
400,737
Latest member
vipamuk

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top