forked from farishadi/Excel_Macro_References
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRangeSelector
More file actions
50 lines (41 loc) · 2.29 KB
/
RangeSelector
File metadata and controls
50 lines (41 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Public Function rangeSelector(ByVal sKeyword As String, wbTarg As Workbook, Optional bMultiCol As Boolean) As String
'this function allows users to select a column range and checks it for suitability based on its parameters
'accepts compulsory keyword value to indicate column to select
'accepts compulsory workbook value if value is from external workbook that needs closing after selection
'accepts optional multicolumn switch to allow single column or multiple columns selection (false - default for single column only selection, true for multiple column selection)
'ask user to select column to take as material
'insert input box for acquisition of range
rangeSelect:
Set copyRange = Nothing
'on error to suppress errors from clicking cancel
On Error Resume Next
Set copyRange = Application.InputBox("Please select the " & sKeyword & " column(s) manually.", sKeyword & " Column(s) selection", , , , , , 8)
On Error GoTo 0
'target workbook needed, otherwise will switch workbooks, Excel2016 bug
wbTarg.Activate
If copyRange Is Nothing Then
cancelSelect = MsgBox("You clicked cancel during the range selection. Are you sure you want to cancel the macro? This will end the macro and return you to the macro workbook. If you clicked Cancel by mistake, click ""No"" to select the Material range again. Click ""Yes"" to cancel and end.", vbYesNo, sKeyword & " Column Cancellation Confirmation")
If cancelSelect = vbNo Then
GoTo rangeSelect
Else
If Not wbTarg Is Nothing Then
wbTarg.Close savechanges:=False
End If
ThisWorkbook.Sheets("Macro").Activate
MsgBox "User cancelled. Please re-run the macro to reselect the ranges."
End
End If
'user selected more than 1 columns
ElseIf copyRange.Columns.Count <> 1 And bMultiCol <> True Then
MsgBox "You have selected more than 1 column for the " & sKeyword & " Column. Please select ONLY ONE COLUMN."
GoTo rangeSelect
'user selected column, but still need to confirm
Else
confirmSelect = MsgBox("You have selected the column """ & copyRange.Address & """ as the " & sKeyword & " Column. Is this correct?", vbYesNo, sKeyword & " Column Confirmation")
If confirmSelect = vbNo Then
GoTo rangeSelect
Else
rangeSelector = copyRange.Address
End If
End If
End Function