Insert a row and paste value from user input

jcrook

New Member
Joined
Jun 12, 2020
Messages
15
Office Version
  1. 2016
Platform
  1. Windows
Hello,
This is extremely simple but I can't find an example on this board. I'm sure it exists.
I want to get user input on a tool number in the form of alphanumeric. This works ok.
I then Uppercase the letters. This works.
I then check if a worksheet already exists with the name of the tool number. This works.
If there's NOT a sheet, then a new one is created by copying a template and renaming the sheet. This works.
I then want to check the list of tool numbers starting at B9 to the end and insert the tool number alphanumerically. This is NOT working.
Any suggestions would be greatly appreciated.

VBA Code:
Option Explicit

Private Sub CommandButton1_Click()

Dim toolName As Variant
Dim i As Integer
Dim lastRow As String
Dim lastOpenCell As Integer
Dim rngCell, testRange As Range



toolName = InputBox("Enter Tool Serial Number." & vbCrLf & _
                     "Upper or Lower Case letters.")

If toolName = "" Then
   MsgBox "Operation Cancelled"
   GoTo 999
Else
   toolName = StrConv(toolName, vbUpperCase)
End If
'Range("D11").Value = toolName - Passed 6/29/2020

Sheets("Start Sheet").Activate
lastRow = Range("B" & Rows.Count).End(xlUp).Row
'Range("D11").Value = lastRow 'Passed 6/29/2020
'testRange = Range("B9: & lastRow")
lastOpenCell = lastRow + 1

  'Check if worksheet already exists - Passed 6/29/2020
For i = 1 To Worksheets.Count
   If Worksheets(i).Name = toolName Then
     MsgBox "A worksheet " & toolName & " Exists!" & vbCrLf & "Operation Cancelled."
     Sheets(toolName).Select
     GoTo CheckList
   End If
Next i

CheckList:

  'Add toolName to list on Start Sheet - Not Passed 6/29/2020
Worksheets("start Sheet").Activate
  For Each rngCell In Range("B9: & lastRow")
      If Cells(rngCell).Value > toolName Then
          Cells(rngCell).Select
          toolName.Copy
          Range(rngCell).EntireRow.Insert
      End If
  Next rngCell
          
  'Create a worksheet "toolName" and copy template worksheet - Passed 6/29/2020
ThisWorkbook.Worksheets("Template").Copy After:=Worksheets(Sheets.Count)
Worksheets(toolName).Activate

        
999 End Sub
Thank you in Advance,
Jay.
 

Attachments

  • Broach List.PNG
    Broach List.PNG
    37 KB · Views: 8

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Can you please explain what you mean by this
I then want to check the list of tool numbers starting at B9 to the end and insert the tool number alphanumerically
 
Upvote 0
Never mind, how about
VBA Code:
Private Sub CommandButton1_Click()
   Dim ToolName As Variant
   Dim LastRow As Long
   Dim rngCell As Range
  
   ToolName = InputBox("Enter Tool Serial Number." & vbCrLf & _
                        "Upper or Lower Case letters.")
  
   If ToolName = "" Then
      MsgBox "Operation Cancelled"
      Exit Sub
   Else
      ToolName = UCase(ToolName)
   End If
  
   If Evaluate("isref('" & ToolName & "'!A1)") Then
      MsgBox "A worksheet " & ToolName & " Exists!" & vbCrLf & "Operation Cancelled."
      Exit Sub
   Else
      ThisWorkbook.Worksheets("Template").Copy After:=Worksheets(Sheets.Count)
      ActiveSheet.Name = ToolName
   End If
    
   Worksheets("start Sheet").Activate
   LastRow = Range("B" & Rows.Count).End(xlUp).Row
   If LastRow < 9 Then
      Range("B9").Value = ToolName
   Else
      For Each rngCell In Range("B9:B" & LastRow)
         If rngCell.Value > ToolName Then
            rngCell.EntireRow.Insert
            rngCell.Offset(-1).Value = ToolName
            Exit Sub
         End If
      Next rngCell
   End If
End Sub
 
Last edited:
Upvote 0
Hi Fluff,
Your code worked perfectly.

Thank you,
Jay.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Good Morning Fluff,
I've researched the "Evaluate" command a little. I don't understand the "isref" option you used.
I have the line written out below as if I hard coded the ToolName as SA2320.
IF Evaluate("isref('" & ToolName & "'!A1)") Then as
IF Evaluate(isref('SA2320'!A1)) Then - 1
-or-
IF Evaluate(isref("SA2320"!A1)) Then - 2

Am I interpreting the line correctly in option 1 or 2, or (not at all, haha). I think it's option 1.
Also, the "!A1", I thought that referenced a cell not a sheet but maybe it's the entire workbook since I require the user to start on "Start Sheet".
I understand the Evaluate command can eliminate writing out Worksheets or Sheets or Range identifiers.

Thanks in Advance for the education,
Jay.
 
Upvote 0
The Evaluate is simply calculating the Excel formula =ISREF('SA2320'!A1) which returns true or false depending on whether it's a valid cell reference.
So if the sheet exists it returns true.
 
Upvote 0
Hi Fluff,
I can't believe this is this difficult BUT, I'm trying to add a line in the code you gave me to write the value inside ToolName to cell D3
on the active sheet after it's created in the IF-END IF.
Range("D3").Value = ToolName
What in the world am I doing wrong?

If Evaluate("isref('" & ToolName & "'!A1)") Then
MsgBox "A worksheet " & ToolName & " Exists!" & vbCrLf & "Operation Cancelled."
Exit Sub
Else
ThisWorkbook.Worksheets("Template").Copy After:=Worksheets(Sheets.Count)
ActiveSheet.Name = ToolName
'Inserted Line
Range("D3").Value = ToolName
End If

Thanks,
Jay.
 
Upvote 0

Forum statistics

Threads
1,215,056
Messages
6,122,907
Members
449,096
Latest member
dbomb1414

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