VBA InputBox need user to enter details if not loop back on itself

universaloneill

New Member
Joined
May 19, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
So I'm new to doing this !!! I have used the record Macro function and then expanded when necessary. what I need help with is having my input box require a text entry. if its blank have and error message and for it to return to the message box, if cancelled the new sheet be removed and go back to the beginning. here's what I'm working with.

VBA Code:
Sub SAVE_TEMP_MAR()
'
' SAVE_TEMP_MAR Macro
'

'
    Sheets("JP PRN.").Select
    Sheets("JP PRN.").Copy Before:=Sheets(4)
    Sheets("JP PRN. (2)").Select
    Sheets("JP PRN. (2)").Name = ("TEMPORARY MAR")
    ActiveWindow.SmallScroll Down:=-21
    Range("A1:AF18").Select
    Selection.ClearContents
    Range("AG1").Select
    Sheets("Blank MAR").Select
    Range("A1:AF18").Select
    Selection.Copy
    Sheets("TEMPORARY MAR").Select
    Range("A1").Select
    ActiveSheet.Paste
    Sheets("Blank MAR").Select
    Range("AG1").Select
    Sheets("TEMPORARY MAR").Select
    Sheets("TEMPORARY MAR").Name = InputBox("Enter new name for the MAR." & vbNewLine & vbNewLine & "Please use Resident initials and name of Medication ")
    Range("AG1").Select

Thank you in advance
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
I think this will do what you want in a more logical manner:

VBA Code:
Sub SAVE_TEMP_MAR()
'
    Dim NameForAddedSheet   As String
'
Start:
    NameForAddedSheet = Application.InputBox("Enter new name for the MAR." & vbNewLine & _
            vbNewLine & "Please use Resident initials and name of Medication ", Type:=2)    ' Ask the user to enter a name for the new sheet to create
'
    If NameForAddedSheet = "False" Then GoTo Start                                          ' 'Cancel' or 'X' was clicked so start over
    If Trim(NameForAddedSheet) = "" Then                                                    ' If nothing was entered and the 'OK' was clicked then ...
        MsgBox "Please enter a name for the sheet to be added."                             '   Display message to user
        GoTo Start                                                                          '   Start over
    End If
'
    Sheets("JP PRN.").Copy Before:=Sheets(4)                                                ' Copy sheet 'JP PRN.' to workbook
    Sheets("JP PRN. (2)").Name = NameForAddedSheet                                          ' Rename the copied sheet
    Range("AG1").Select                                                                     ' Select AG1 on the copied sheet
'
    Sheets("Blank MAR").Range("A1:AF18").Copy Sheets(NameForAddedSheet).Range("A1")         ' Copy range from 'Blank MAR' to the copied sheet
'
    Application.Goto Worksheets("Blank MAR").Range("AG1")                                   ' Select AG1 on sheet 'Blank MAR'
End Sub
 
Last edited:
Upvote 0
This is a simple way to make a copy of a sheet.
Not sure what you're trying to do as far as copying Ranges
This script does not consider if sheet name already exists.
VBA Code:
Sub Copy_Sheet()
'Modified  5/19/2022  9:14:05 PM  EDT
Application.ScreenUpdating = False
Dim ans As String
ans = InputBox("Enter New sheet name")

Sheets("JP PRN.").Copy Before:=Sheets(4)
ActiveSheet.Name = ans
Application.ScreenUpdating = True
End Sub
 
Upvote 0
So I'm new to doing this !!! I have used the record Macro function and then expanded when necessary. what I need help with is having my input box require a text entry. if its blank have and error message and for it to return to the message box, if cancelled the new sheet be removed and go back to the beginning. here's what I'm working with.

VBA Code:
Sub SAVE_TEMP_MAR()
'
' SAVE_TEMP_MAR Macro
'

'
    Sheets("JP PRN.").Select
    Sheets("JP PRN.").Copy Before:=Sheets(4)
    Sheets("JP PRN. (2)").Select
    Sheets("JP PRN. (2)").Name = ("TEMPORARY MAR")
    ActiveWindow.SmallScroll Down:=-21
    Range("A1:AF18").Select
    Selection.ClearContents
    Range("AG1").Select
    Sheets("Blank MAR").Select
    Range("A1:AF18").Select
    Selection.Copy
    Sheets("TEMPORARY MAR").Select
    Range("A1").Select
    ActiveSheet.Paste
    Sheets("Blank MAR").Select
    Range("AG1").Select
    Sheets("TEMPORARY MAR").Select
    Sheets("TEMPORARY MAR").Name = InputBox("Enter new name for the MAR." & vbNewLine & vbNewLine & "Please use Resident initials and name of Medication ")
    Range("AG1").Select

Thank you in advance
This is a simple way to make a copy of a sheet.
Not sure what you're trying to do as far as copying Ranges
This script does not consider if sheet name already exists.
VBA Code:
Sub Copy_Sheet()
'Modified  5/19/2022  9:14:05 PM  EDT
Application.ScreenUpdating = False
Dim ans As String
ans = InputBox("Enter New sheet name")

Sheets("JP PRN.").Copy Before:=Sheets(4)
ActiveSheet.Name = ans
Application.ScreenUpdating = True
End Sub
Thank you very much I know it's not optimised very well as I said I am new to this and was using the recording function as a basis so it did exactly what I wanted it to do to start with and then went into it and changed what I thought was necessary.
 
Upvote 0
Thank you very much I know it's not optimised very well as I said I am new to this and was using the recording function as a basis so it did exactly what I wanted it to do to start with and then went into it and changed what I thought was necessary.
You did not say if my script worked for you.
 
Upvote 0

Forum statistics

Threads
1,214,645
Messages
6,120,711
Members
448,984
Latest member
foxpro

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