netrixuser
Board Regular
- Joined
- Jan 21, 2019
- Messages
- 77
- Office Version
- 365
- Platform
- Windows
Hello, a relative newbie here - trying hard to learn....
I have worked out error trapping for a message box, but for some reason am struggling with an input box.
Main code below calling smaller sub routines
DeleteTabs does just that, checks if any tabs were saved in the workbook that shouldn't be there and deletes them.
OpenFiles is below - I am fairly happy with this code but would like the GetOpenFilename to repeat a couple of times before exiting the sub - this isn't a show stopper though
RegionSelect1 is the issue - a simple input box that assigns the value to the global variable "region" which is used later to call other subs depending on the value.
Regarding the Error Trapping ....
I want the sub to throw a message box if the user types anything other than AMER, EMEA or EH telling them to try again, or if they type a number to tell them that and to try again.
Also, if they press enter (or ok) which will input the default of "For Example EMEA", or press cancel, or press the X - to all throw a message box.
If they "mess up" more than twice to exit the sub.
At this point though, the three workbooks have already been opened in the OpenFiles sub above so before exiting the sub I would like the DeleteTabs sub to run again.
I realise I do not need the displayalerts and screenupdating in each sub - to be tidied up later.
Hope that all made sense ! as I say, I'm pretty happy with the code but want to do something if users of the macro try and break things by entering rogue stuff !!
Any help gratefully received !!
I have worked out error trapping for a message box, but for some reason am struggling with an input box.
Main code below calling smaller sub routines
Rich (BB code):
Dim region As String
Dim YesErr As Boolean
Sub Get_Reports()
Call DeleteTabs
Call OpenFiles
If YesErr = True Then Exit Sub
Call RegionSelect1
Call RenameTabs
Call AccsysColumnsZandAA
Call AccsysAddColumns
Call AccsysOutlineAllCells
Call AccsysFormatRow1
Call AccsysInsertUsenameCol
Call AccsysFlipUsername
Call AccsysHideColumns
Call AccsysCapitalLetters
Call AccsysCheckColumn
Call RegionSelect2
Call HighlightOlderThan30
Call VoxSmartTidyDates
Call CogniaTidyDates
Call AccsysTidySheet1
Call VoxSmartTidySheet
Call VoxSmartOutlineAllCells
Call CogniaOutlineAllCells
'Call SaveAs
MsgBox "File saved as: 'MMM-YYYY Monthly MRL Reconciliation [Region]' in the Documents folder" & vbNewLine _
& "Note, Macros have been removed from this document", , "Reconciliation Spreadsheet ready for use "
End Sub
DeleteTabs does just that, checks if any tabs were saved in the workbook that shouldn't be there and deletes them.
VBA Code:
Sub DeleteTabs()
Dim xWs As Worksheet
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each xWs In Application.ActiveWorkbook.Worksheets
If LCase(xWs.Name) <> "index" Then xWs.Delete
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
VBA Code:
Sub OpenFiles()
Dim controlFile As Variant
Dim TargetFiles As Variant
Dim X As Integer
Dim wb As Variant
YesErr = False
Application.ScreenUpdating = False
Application.DisplayAlerts = False
If MsgBox("Have you downloaded the latest reports for this month?", vbYesNo _
+ vbQuestion + vbDefaultButton1, "Before you begin") = vbNo Then
YesErr = True
MsgBox "Please Download The Appropriate Accsys report and 3rd Party Vendor Reports", vbExclamation, "Whoops!!"
Exit Sub
End If
controlFile = ActiveWorkbook.Name
ChDir "C:\Users\" & Environ$("UserName") & "\Downloads"
TargetFiles = Application.GetOpenFilename _
(Title:="Please choose the files you wish to open", _
FileFilter:="Excel and CSV Files (*.xls*;*.csv),*.xls*;*.csv", MultiSelect:=True)
If Not IsArray(TargetFiles) Then
MsgBox "Please Select The Appropriate Files", vbExclamation, "Whoops!"
YesErr = True
Exit Sub
Else
For X = 1 To UBound(TargetFiles)
Set wb = Workbooks.Open(TargetFiles(X))
Windows(wb.Name).Activate
Sheets(1).Copy After:=Workbooks(controlFile).Sheets(1)
Windows(wb.Name).Close (False)
Next X
End If
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Regarding the Error Trapping ....
I want the sub to throw a message box if the user types anything other than AMER, EMEA or EH telling them to try again, or if they type a number to tell them that and to try again.
Also, if they press enter (or ok) which will input the default of "For Example EMEA", or press cancel, or press the X - to all throw a message box.
If they "mess up" more than twice to exit the sub.
At this point though, the three workbooks have already been opened in the OpenFiles sub above so before exiting the sub I would like the DeleteTabs sub to run again.
I realise I do not need the displayalerts and screenupdating in each sub - to be tidied up later.
VBA Code:
Sub RegionSelect1()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
region = UCase(InputBox("Which Regional Report Are You Creating ? AMER, EMEA or EH", "Enter Region", "For Example EMEA"))
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Hope that all made sense ! as I say, I'm pretty happy with the code but want to do something if users of the macro try and break things by entering rogue stuff !!
Any help gratefully received !!
Last edited by a moderator: