0

I have a macro that is on a command button. It takes the users input from a text box on the same user form and performs a set of tasks.

At the end I want it to select/activate the first sheet cell a1. When I run it currently, whether select or activate, the cursor just stays on sheet 2 and the sub finishes like the code isn't there. Here is the code for the specific task:

 Sheets("Main Committee Database").Range("$A$1").Select

Here is the sub it is in:

Private Sub CommandButton1_Click()
Dim tblm As Range
Dim c As Control
Dim k As String
Dim fndrng As Range


'Hides Userform after "Add" button is selected
 UserForm1.Hide


' Sets variables for use in coding
Set tblm = Worksheets("Main Committee Database").ListObjects("CredDB").ListColumns(3).DataBodyRange
UserForm1.TextBox1.SetFocus
Set c = UserForm1.Controls("TextBox1")
Set fndrng = tblm.Find(c.Value, LookIn:=xlValues, lookat:=xlWhole)



'Checks conditions for any error handling
If UserForm1.TextBox1.Value = "" Then
  ' message if no value is entered
  MsgBox "No Value Entered. Please Try Again"
Exit Sub
Else

If Not fndrng Is Nothing Then
' message if value already exists in list
MsgBox "This Vendor Already Exists"
Exit Sub
Else
Application.ScreenUpdating = False

' UnHide Helper sheet
Worksheets("Add Insurance help").Visible = True

' Copies table in main sheet
Sheets("Main Committee Database").Select
On Error Resume Next
Sheets("Main Committee Database").ShowAllData
Range("CredDB[[#Headers],[Name]]").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

' pastes the data into helper sheet
Sheets("Add Insurance Help").Select
Sheets("Add Insurance Help").Range("A1").PasteSpecial _
Paste:=xlPasteValuesAndNumberFormats

' removes duplicates to make unique list of providers
ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1").CurrentRegion, , xlYes).Name = "HelpIns"
Application.CutCopyMode = False
ActiveSheet.Range("HelpIns[#All]").RemoveDuplicates Columns:=1, Header:= _
    xlYes
   
' clears unneeded data from column C-F
Range("C2:F2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents

' Places the New Insurance Name into column C and autofills it down to the last provider in the list
Worksheets("Add Insurance Help").Range("C2") = TextBox1.Value
Range("C2").Select
Selection.AutoFill Destination:=Range("HelpIns[Payor]")
Range("HelpIns[Payor]").Select

' copies new data
Range("A:M").Select
Range(Selection, Selection.End(xlUp)).Select
Range("HelpIns").Select
Selection.Copy

'pastes new data at bottom of table
Sheets("Main Committee Database").Activate
Range("CredDB[[#Headers],[Name]]").Select
Range("CredDB[[#Headers],[Name]]").End(xlDown).Offset(1, 0).PasteSpecial _
Paste:=xlPasteValuesAndNumberFormats

' cleans up helper sheet for next use
Sheets("Add Insurance Help").Activate
Columns("A:M").Select
Selection.Delete Shift:=xlToLeft
Application.CutCopyMode = False

' hides helper sheet
Sheets("Add Insurance help").Visible = False


 ' Resets workbook to main page and sorts column Alphabetically
Worksheets("Main Committee Database").Range("A15").Activate


Worksheets("Main Committee Database").ListObjects("CredDB").Sort _
    .SortFields.Clear
Worksheets("Main Committee Database").ListObjects("CredDB").Sort _
    .SortFields.Add2 Key:=Range("CredDB[[#All],[Payor]]"), SortOn:= _
    xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Main Committee Database").ListObjects("CredDB") _
    .Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .Apply
End With



k = UserForm1.TextBox1.Value
Worksheets("Main Committee Database").ListObjects("CredDB").Range.AutoFilter Field:=3, Criteria1:= _
    k
    
' clears textbox for next use
TextBox1.Value = ""
Application.ScreenUpdating = True

Sheets("Main Committee Database").Range("$A$1").Select
End If
End If

End Sub
PMNIServ1
  • 49
  • 1
  • 2
  • 8
  • 1
    Remove the line `On Error Resume Next` and see what happens. – Darrell H Jan 26 '21 at 22:28
  • 2
    `On Error Resume Next` = "if there's any problem with this code then don't tell me about it - just ignore the error and carry on" There are times when this might be useful for a couple of lines, but you should cancel it ASAP with (e.g.) `On Error Goto 0` – Tim Williams Jan 26 '21 at 22:30
  • 1
    You have the same request as [THIS QUESTION](https://stackoverflow.com/questions/65886973/excel-vba-selecting-a-cell-getting-error-1004-select-range-class-failed) and I will give the same suggestion to you as well :) – Siddharth Rout Jan 26 '21 at 22:41
  • Okay, I was definitely having another issue and could not see it due to my On Error code. I have fixed that by adding On error go to 0 where needed. I get a runtime error when trying to select or activate the 1st sheet in the workbook at the end of the code. says the range method failed. – PMNIServ1 Jan 26 '21 at 22:44
  • 1
    Thank you @SiddharthRout. I appreciate the new approach. Much Cleaner. – PMNIServ1 Jan 26 '21 at 22:50

0 Answers0