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