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.
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.