IF statement and inputbox check validate user input - excel

I have a code that asks the user to select a sheet by writing its name in an inputbox, and then I need to check if the selected name is correct.
How can I write the "if" statement so to return back to the inputbox?
I'm using MS Word in Windows 7. This is the code:
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function
Sub OpenExcelFile()
Dim oExcel As Excel.Application
Dim oWB As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim oneRange As Excel.Range
Dim aCell As Excel.Range
Dim intChoice As Integer
Dim strPath As String
Dim uiSheet As String
Set oExcel = New Excel.Application
'Select the start folder
Application.FileDialog(msoFileDialogOpen _
).InitialFileName = ActiveDocument.path
'Remove all other filters
Call Application.FileDialog(msoFileDialogOpen).Filters.Clear
'Add a custom filter
Call Application.FileDialog(msoFileDialogOpen).Filters.Add( _
"Only Excel File Allowed", "*.xl*")
'only allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
'make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made
If intChoice <> 0 Then
'get the file path selected by the user
strPath = Application.FileDialog( _
msoFileDialogOpen).SelectedItems(1)
End If
'open excel file and select sheet
Set oWB = oExcel.Workbooks.Open(strPath)
Dim strBuild As String
'set Array for user input control
Dim myArray() As Variant
ReDim myArray(1 To oWB.Sheets.Count)
'populate input box and array
For Each xlSheet In oWB.Worksheets
strBuild = strBuild & xlSheet.Name & vbCrLf
For i = 1 To oWB.Sheets.Count
myArray(i) = oWB.Sheets(i).Name
Next i
Next xlSheet
'show inputbox with list of sheets
strBuild = Left$(strBuild, Len(strBuild) - 2)
uiSheet = InputBox("Provide a sheet name." & vbNewLine & strBuild)
'check if User input match with sheet name
If IsInArray(uiSheet, myArray) Then
'show excel window
oExcel.Visible = True
'sort selected sheet by first column range
oExcel.Worksheets(uiSheet).Activate
Set oneRange = oExcel.Range("A1:A150")
Set aCell = oExcel.Range("A1")
oneRange.Sort Key1:=aCell, Order1:=xlAscending, Header:=xlYes
Else
MsgBox "Please enter a valid name!", vbCritical
End If
End Sub

If you replace the code starting with uiSheet = InputBox(..... with the code below, it should work.
'check if User input match with sheet name
Dim bSheetPresent As Boolean
bSheetPresent = False
Do Until bSheetPresent
uiSheet = InputBox("Provide a sheet name." & vbNewLine & strBuild)
If uiSheet = "" Then Exit Do
If IsInArray(uiSheet, myArray) Then
bSheetPresent = True
Else
MsgBox "Please enter a valid name!", vbCritical
End If
Loop
If bSheetPresent Then
'show excel window
oExcel.visible = True
'sort selected sheet by first column range
oExcel.Worksheets(uiSheet).Activate
Set oneRange = oExcel.Range("A1:A150")
Set aCell = oExcel.Range("A1")
oneRange.Sort Key1:=aCell, Order1:=xlAscending, Header:=xlYes
End If
If the user presses cancel on the inputbox, the it will exit the loop.
You may also consider to build a form with a pre-filled combobox. That way the user cannot make a mistake.

To create create your own inputbox with a list of sheets, you can do this:
Create a userform in the VBE
Name it frmSelectSheet
Put a combobox on it named cbSheets
Add the following code to the userform's code pane:
Private Sub UserForm_Initialize()
Dim oSheet As Worksheet
For Each oSheet In ThisWorkbook.Sheets
Me.cbSheets.AddItem oSheet.Name
Next oSheet
End Sub
Private Sub cbSheets_Change()
Me.Hide
End Sub
Add a module and add the following code to it:
Public Function SheetInputBox() As String
Dim ofrmSheetInput As New frmSelectSheet
ofrmSheetInput.Show
SheetInputBox = ofrmSheetInput.cbSheets
Unload ofrmSheetInput
End Function
Call the function like this
? SheetInputBox or uiSheet = SheetInputBox

Related

Use ComboBox input to find itself in the workbook?

I am new to VBA and UserForms.
I have a ComboBox where the user will enter a unique Sales Order # (SalesOrder). I want my form to take this input and find it in the workbook and then update the status with the user's inputs in later ComboBoxes (CommentBox & OrderStatus). The issue I am facing is the actual code to find the Sales Order # in the workbook. I've tried what is seen below in several different variations.
If I replace all the ComboBox inputs with the actual inputs as a string, the code runs fine in a module.
Ideally, the code will loop through the sheet array finding all the lines with the Sales Order # and apply the inputs to the row.
Sub AddDataToList()
Dim shtarray As Variant, shtname As Variant
Dim Data As Worksheet, ws As Worksheet
Dim wbk As Workbook
Dim Strg As String
shtarray = Array("EMAUX", "Irene", "Cassandra", "Patricia", "EMREL", "Maria", "Jason", "Peedie", "MICRO", "PARTS", "NAVY", "DELTA")
Set wbk = ThisWorkbook
For Each shtname In shtarray
Set ws = Nothing
On Error Resume Next
Set ws = wbk.Worksheets(shtname)
On Error GoTo 0
If Not (ws Is Nothing) Then
ActiveSheet.Cells.Find(StatusUpdateForm.SalesOrder.Text).Offset(0, 17).Select
ActiveCell.Value = CommentBox.Text
ActiveCell.Offset(0, 2).Value = OrderStatus.Text
End If
Next
MsgBox SalesOrder.Value & "was updated."
End Sub
Thank you for the assistance!
More Information ***
Below is the code for the Update command button. This is a standard two button system, one updates the records and the other cancels the form.
Private Sub UpdateButton_Click()
If Not EverythingFilledIn Then Exit Sub
Me.Hide
AddDataToList
Unload Me
End Sub
And code for the EverthingFilledIn
Private Function EverythingFilledIn() As Boolean
Dim ctl As MSForms.Control
Dim AnythingMissing As Boolean
EverthingFilledIn = True
AnythingMissing = False
For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.TextBox Or TypeOf ctl Is MSForms.ComboBox Then
If ctl.Value = "" Then
ctl.BackColor = rgbPink
Controls(ctl.Name & "Label").ForeColor = rgbRed
If Not AnythingMissing Then ctl.SetFocus
AnythingMissing = True
EverythingFilledIn = False
End If
End If
Next ctl
End Function
Try this (my first comment notwithstanding):
Sub AddDataToList()
Dim shtarray As Variant, shtname As Variant
Dim Data As Worksheet, ws As Worksheet
Dim wbk As Workbook
Dim Strg As String
Dim r As Range
shtarray = Array("EMAUX", "Irene", "Cassandra", "Patricia", "EMREL", "Maria", "Jason", "Peedie", "MICRO", "PARTS", "NAVY", "DELTA")
Set wbk = ThisWorkbook
For Each shtname In shtarray
Set ws = wbk.Worksheets(shtname)
Set r = ws.Cells.Find(StatusUpdateForm.SalesOrder.Text) 'better to specify all parameters
If Not r Is Nothing Then
r.Offset(0, 17).Value = CommentBox.Text
r.Offset(0, 2).Value = OrderStatus.Text
End If
Next
MsgBox SalesOrder.Value & "was updated."
End Sub
There is no need to select things.

Find and Filter by String Value

I am pulling a work organization report and want to find and filter by a unique ID value. The unique ID to be filtered is specified by a public property (this number is used on another occasion so that is why it is public) entered through a text box within a userform.
User enters unique ID of manager they want to filter under
Use unique ID to find which manager level column has unique ID
Move onto the next column if ID is not found
Once ID is found, Filter column
There are 9 different manager levels I am filtering through, columns AU, AW, AY, BA ,BC, BE, BG, BI, & BK, and they all rest on row 3. Therefore I have columns 'A3:BK3' but am only filtering between 'AU3:BK3' to pull data in the earlier columns.
++++++Open File Dialog:
```
Private Sub SelectButton_Click()
Dim SelectedFile As String
SelectedFile = Application.GetOpenFilename()
SelectedFiletxtbox = SelectedFile
End Sub
```
++++++Public Property Code:
```
Public Property Get OpenFileTxt() As String
OpenFileTxt = SelectedFiletxtbox.Value
End Property
```
++++++Execution Piece:
```
Private Sub EmailButton_Click()
'Workbooks.Open OpenFile
Application.ScreenUpdating = False
If Len(Trim(Me.EnterWWIDtxtbox.Text)) = 0 Then
Me.EnterWWIDtxtbox.SetFocus
MsgBox "Must provide a Unique ID"
Exit Sub
End If
'WWID = Trim(Me.EnterWWIDtxtbox.Text)
'Path to be pulled from open file dialog as reports are dynamic
Dim ws As Worksheet
Dim wb As Workbook
Set wb = Workbooks.Open(Filename:=OpenFileTxt)
Set ws = wb.Worksheets("Sheet1")
Dim aColumns() As String
aColumns = Split("AU,AW,AY,BA,BC,BE,BG,BI,BK", ",")
Dim bFound As Boolean
bFound = False
Dim rFound As Range
Dim vColumn As Variant
For Each vColumn In aColumns
Set rFound = ws.Columns(vColumn).Find(WWID, , xlValues, xlPart)
If Not rFound Is Nothing Then
bFound = True
MsgBox "Found [" & WWID & "] in column " & vColumn
With ws.Columns(vColumn)
.AutoFilter 1, rFound.Value
MsgBox "filtered"
'Do stuff with the filtered data here
End With
Exit For
End If
Next vColumn
If bFound = False Then MsgBox "Unique ID [" & WWID & "] not found"
'Filter by Region Selected
'Range("AG3").AutoFilter Field:=33, Criteria1:=Region
Unload DistrUserForm
'Open E-Mail
Application.ScreenUpdating = True
End Sub
```
I created a test workbook for this and filled it with junk data. I then created a basic userform that only had a textbox (named txtUniqueID) and a button (named CommandButton1). Entered the ID I was looking for in the textbox and clicked the button to run the search and filter if found. Verified it worked as intended. You should be able to adapt this to your needs. Here's the full userform code. Note the Dim WWID As Variant at the top outside of the sub since you said that was a public variable (this could also have been in a standard module and instead of Dim it would be Public, I just did this for ease of testing).
Dim WWID As Variant
Private Sub CommandButton1_Click()
If Len(Trim(Me.txtUniqueID.Text)) = 0 Then
Me.txtUniqueID.SetFocus
MsgBox "Must provide a Unique ID"
Exit Sub
End If
WWID = Trim(Me.txtUniqueID.Text)
'Explicitly define your workbook and worksheet where the data is
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim aColumns() As String
aColumns = Split("AU,AW,AY,BA,BC,BE,BG,BI,BK", ",")
Dim bFound As Boolean
bFound = False
Dim rFound As Range
Dim vColumn As Variant
For Each vColumn In aColumns
Set rFound = ws.Columns(vColumn).Find(WWID, , xlValues, xlPart)
If Not rFound Is Nothing Then
bFound = True
MsgBox "Found [" & WWID & "] in column " & vColumn
With ws.Columns(vColumn)
.AutoFilter 1, rFound.Value
MsgBox "filtered"
'Do stuff with the filtered data here
.AutoFilter 'Remove filter afterwards
End With
Exit For
End If
Next vColumn
If bFound = False Then MsgBox "Unique ID [" & WWID & "] not found"
End Sub

Object Required in Excel VBA

I am trying to get the user to enter sheet name and based on the input I want selected cell value to be copied from one sheet to another sheet to a new row.
This is for a basic excel functioning system
Set nextCellInColumn = Worksheets("Summary").Cells(Rows.Count, 4).End(xlUp).Offset(1, 0)
strName = Application.InputBox("Please enter")
nextCellInColumn.Value = Worksheets.Application.InputBox("Please enter").Range("I5").Value
Worksheets.Application.InputBox("Please enter").Range("I5").Copy Worksheets("Summary").Range("D6")
You need to test if the worksheet name exists that the user entered otherwise the copy will fail. Also if the user presses the Cancel button the InputBox will return a boolean False. You need to check for that and eg exit, or your code fails too.
Option Explicit
Public Sub Test()
Dim wsSummary As Worksheet
Set wsSummary = ThisWorkbook.Worksheets("Summary")
Dim NextCellInColumn As Range
Set NextCellInColumn = wsSummary.Cells(wsSummary.Rows.Count, 4).End(xlUp).Offset(1, 0)
Dim strName As Variant 'if user presses cancel it will return a boolean false
strName = Application.InputBox("Please enter")
If VarType(strName) = vbBoolean And strName = False Then Exit Sub 'user pressed cancel so exit
If WorksheetExists(strName) Then
NextCellInColumn.Value = ThisWorkbook.Worksheets(strName).Range("I5").Value
ThisWorkbook.Worksheets(strName).Range("I5").Copy wsSummary.Range("D6")
Else
MsgBox "Worksheet '" & strName & "' not found.", vbCritical
End If
End Sub
'check if a worksheet exists
Public Function WorksheetExists(ByVal WorksheetName As String, Optional ByVal wb As Workbook) As Boolean
If wb Is Nothing Then Set wb = ThisWorkbook 'default to thisworkbook
Dim ws As Worksheet
On Error Resume Next
Set ws = wb.Worksheets(WorksheetName)
On Error GoTo 0
WorksheetExists = Not ws Is Nothing
End Function

How to check if "afile.xlsm:2" is already open VBA

I am have built a search bar in an Excel Worksheet where the user clicks [search]; it will sort the designated table column based off some FormControl buttons, then open a NewWindow off the current workbook, select Sheet2 and search the designated column on Sheet2 as well. I am having trouble preventing this from happening again the next time someone searches w/o closing the second window manually. I Currently only need two active windows open at once. So I don't want Excel to open a third ActiveWindow and so on.
I am not too familiar with utilizing Functions. I have pasted my macro Sub and Function. I've tried different configurations/variations thinking I am missing something simple (Hopefully).
It appears to me my AlreadyOpen function is not right. I can't seem to get my first IF...True statement to work when afile.xlsm:2 is already open.
Function AlreadyOpen(sFname As String) As Boolean
Dim wkb As Workbook
'Dim sFname As String
sFname = "afile.xlsm:2"
On Error Resume Next
Set wkb = Workbooks(sFname)
AlreadyOpen = Not wkb Is Nothing
Set wkb = Nothing
End Function
Private Sub Search_cmd1_Click()
'PURPOSE: Filter Data on User-Determined Column & Text/Numerical value
Dim myButton As OptionButton
Dim SearchString As String
Dim ButtonName As String
Dim sht As Worksheet
Dim myField As Long
Dim DataRange As Range
Dim mySearch As Variant
'Load Sheet into A Variable
Set sht = ActiveSheet
'Unfilter Data (if necessary)
On Error Resume Next
sht.ShowAllData
On Error GoTo 0
'Filtered Data Range (include column heading cells)
Set DataRange = sht.ListObjects("Table1").Range 'Table
'Retrieve User's Search Input
mySearch = sht.OLEObjects("SearchBox1").Object.Text & "*" 'ActiveX Control ''must include "*" for partials
'Determine if user is searching for number or text
If IsNumeric(mySearch) = True Then
SearchString = "=" & mySearch
Else
SearchString = "=*" & mySearch & "*"
End If
'Loop Through Option Buttons
For Each myButton In sht.OptionButtons
If myButton.Value = 1 Then
ButtonName = myButton.Text
Exit For
End If
Next myButton
'Determine Filter Field
On Error GoTo HeadingNotFound
myField = Application.WorksheetFunction.Match(ButtonName, DataRange.Rows(1), 0)
On Error GoTo 0
'Filter Data
DataRange.AutoFilter _
Field:=myField, _
Criteria1:=SearchString, _
Operator:=xlAnd
Dim sFilename As String
sFilename = "afile.xlsm:2"
If AlreadyOpen(sFilename) Then
Sheets("Sheet2").ListObjects("Table24").Range.AutoFilter Field:=5, Criteria1:=SearchString
Else
If myButton.Text = "SITE" Then
Sheets("Sheet1").Select
ActiveWindow.NewWindow
Windows("afile.xlsm:1").Activate
Windows("afile.xlsm:2").Activate
Windows.Arrange ArrangeStyle:=xlVertical
Sheets("Sheet2").Select
ActiveWindow.Zoom = 55
ActiveSheet.ListObjects("Table24").Range.AutoFilter Field:=5, Criteria1:=SearchString
End If
End If
Exit Sub
End Sub
I want Excel to open a NewWindow ("afile.xlsm:2"), Select Sheet2 and Sort Table1. But, If the second window is already open then just Sort Table24.
A Workbook is not the same as a Window, which is definitely where your If statement is failing. You would need to modify your function to reflect that.
Function AlreadyOpen(sFname As String) As Boolean
Dim wkb As Window
On Error Resume Next
Set wkb = Windows(sFname)
wkb.Activate
AlreadyOpen = Not wkb Is Nothing
Set wkb = Nothing
End Function

Applying condition to processRange function below and perform loop

[table - worksheet "output - flat"][1]
I have code below that checks to see if column "NamedRange" in the table attached appears as a named range in the (dstRng) template and if it does exist it returns the value to the right ("report balance"). How can I add a condition where when the user chooses a template it will only return values based on the Ted ID - in the table attached. I have 2 templates and it loops through the two templates however I want the first template to only return values for Ted ID 10004 and template 2 it will only return values for Ted ID 11372 and etc. etc. Hope that makes sense... let me know if u have any questions
Option Explicit
Sub Button4_Click()
Dim Desktop As Variant
Dim Files As Object
Dim Folder As Variant
Dim oShell As Object
Dim Tmplts As Variant ' Templates folder
Dim wsLocal As Worksheet
Dim wsGroup As Worksheet
Dim wb As Object
' Check Box 2 "Select All" must be checked to run the macro.
If ActiveSheet.Shapes("Check Box 2").ControlFormat.Value = xlOff Then Exit Sub
Application.ScreenUpdating = False
Application.EnableEvents = False
' Prompt user to locate the Templates folder.
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = True Then
Tmplts = .SelectedItems(1)
Else
Exit Sub
End If
End With
Set oShell = CreateObject("Shell.Application")
Set Desktop = oShell.Namespace(0)
' Create the Output folder on the User's Desktop if it does not exist.
Set Folder = Desktop.ParseName("Output")
If Folder Is Nothing Then
Desktop.NewFolder "Output"
Set Folder = Desktop.ParseName("Output")
End If
Set Files = oShell.Namespace(Tmplts).Items
Files.Filter 64, "*.xlsm"
For Each wb In Files
Set wb = Workbooks.Open(Filename:=wb.Path, UpdateLinks:=False)
Call BreakLinks(wb)
On Error Resume Next
Set wsLocal = wb.Worksheets("RVP Local GAAP")
Set wsGroup = wb.Worksheets("RVP Group GAAP")
'unprotect workbook
wsLocal.Unprotect Password:="KqtgH5rn9v"
wsGroup.Unprotect Password:="KqtgH5rn9v"
On Error GoTo 0
' Check that both worksheets exist before updating.
If Not wsLocal Is Nothing And Not wsGroup Is Nothing Then
Call ProcessNamedRanges(wb)
'lock the workbook
wsLocal.Protect Password:="KqtgH5rn9v"
wsGroup.Protect Password:="KqtgH5rn9v"
''MsgBox "Ranges have been updated sucessfully."
' Save the workbook to the folder and close.
On Error Resume Next
wb.SaveAs Filename:=Folder.Path & "\" & wb.Name
ActiveWorkbook.Close True
On Error GoTo 0
End If
Next wb
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
Sub ProcessNamedRanges(ByRef wb As Workbook)
Dim dstRng As Range
Dim rng As Range
Dim rngName As Range
Dim rngNames As Range
Dim wks As Worksheet
Set wks = ThisWorkbook.Sheets("Output - Flat")
' Exit if there are no named ranges listed.
If wks.Range("D4") = "" Then Exit Sub
Set rngNames = wks.Range("D4").CurrentRegion
Set rngNames = Intersect(rngNames.Offset(1, 0), rngNames.Columns(2))
'Loop through all the values in NamedRange
For Each rngName In rngNames
' Verify the Named Range exists.
On Error Resume Next
Set dstRng = wb.Names(rngName.Text).RefersToRange
If Err = 0 Then
'Copy the report balance to the Template worksheet in column "G".
dstRng.Value = rngName.Offset(0, 1).Value
Else
'answer = MsgBox("The Named Range """ & rngName.Value & """ Does Not Exist" & vbLf & vbLf & "Continue?", vbYesNo + vbExclamation)
'If answer = vbNo Then Exit Sub
End If
On Error GoTo 0
Next rngName
End Sub
Sub BreakLinks(ByRef wb As Workbook)
Dim i As Long
Dim wbLinks As Variant
wbLinks = wb.LinkSources(xlExcelLinks)
If Not IsEmpty(wbLinks) Then
For i = 1 To UBound(wbLinks)
ActiveWorkbook.BreakLink wbLinks(i), xlLinkTypeExcelLinks
Next i
End If
End Sub

Resources