How to call VBA function from excel - excel

I have en excel file where i have to put validation rule. I have one cell let says "customer Time" where user can enter time but it is customize time. User can enter time like that
23:45
98:20
100:30
User cannot enter string and no special character except colon. I have made one macro and it works perfectly accoriding to customer demand. Here is macro
Public Function isValidTime(myText) As String
Dim regEx
Set regEx = New RegExp 'Regular expression object
regEx.Pattern = "^[0-9]+([:]+[0-9]+)*$" ' Set pattern.
If regEx.test(myText) Then
isValidTime = myText
Else
isValidTime = "Null"
End If
End Function
Note: To test this macro you have to go to VBA ide in Tool then reference and then select microsoft visual basic vbascript 5.5
Now i want to call this at excel. I can call like =IsValidTime("23:43") and getting result but customer is not interested to call this. Customer need a excel where he/she enter the value and value will validate according to above criteria and get the exact value or Null.
I dont know how to fix this task. I have Validated date and time as well from Data and then data validation and set the rule and it works perfect, i need the same way for my this rule as well. Any help will be highly appreciated...
Thanks
Kazmi

You can use the Worksheet_Change event inside the sheet. Inside the VBE, select the sheet and choose Workhseet from the left drop-down and Change from the right.
Enter the following code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then 'assumes user input cell is A1
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
On Error GoTo ErrTrap
Target.Value = isValidTime(Target.Value)
End If
KeepMoving:
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Exit Sub
ErrTrap:
MsgBox Err.Number & Err.Description
Resume KeepMoving
End Sub
Public Function isValidTime(myText) As String
Dim regEx
Set regEx = New RegExp 'Regular expression object
regEx.Pattern = "^[0-9]+([:]+[0-9]+)*$" ' Set pattern.
If regEx.test(myText) Then
isValidTime = myText
Else
isValidTime = "Null"
End If
End Function

Related

Get clipboard value for data validation

I have an Excel file that are used by end users, where, in a specific range of cells, if a change is made, a change event macro is triggered.
What I do with this macro is to check if the last action is any type of pasting.
What I need is to, somehow, get in a variable the content the user has copied (clipboard?) and then execute a function or procedure which checks the validy of the data. If it's correct, it will paste the values mantaining the conditional format, and if wrong it will undo the operation and disable the events.
So far, I think I am close to have everything but I am missing the knowledge to get in VBA the clipboard content in a variable.
I would appreciate general feedback as well.
PD: I have stated range(B:B) to keep it simple, in reality I will have a function for each column because the validation changes based (but that's on me,I just need to have 1 correct in order to replicate the logic).
Private Sub Worksheet_Change(ByVal Target As Range)
lastAction = Application.CommandBars("Standard").FindControl(ID:=128).List(1)
If Left(lastAction, 5) = "Paste" Then
If Not Application.Intersect(Target, Range("B:B")) Is Nothing Then
validation (Application.Intersect(Target, Range("B:B")))
End If
Else
End If
End Sub
Function validation(cell) As Boolean
Dim check As Boolean
check = Application.WorksheetFunction.VLookup(cell, MDM.Range("AK2:AK86"), 1, False)
If check = True Then
ActiveSheets.PasteSpecial Paste:=xlPasteAllMergingConditionalFormats
Application.CutCopyMode = False
Else
With Application
.EnableEvents = False
.Undo
End With
Application.EnableEvents = True
End If
End Function
I need to do this validation because if the user paste the value from another excel, it will remove both the conditional formatting and the data validation for that column.
I've used the clsClipboard class described at the following link
http://www.la-solutions.co.uk/content/CONNECT/MVBA/MVBA-Clipboard.htm
Copy the VBA class module code to a file named clsClipboard.cls, then import new Class module into your project.
Usage:
Sub test()
Dim CB As New clsClipboard
Dim myVar As String
CB.SetText "this is a test"
myVar = CB.GetText()
Debug.Print myVar
Set CB = Nothing
End Sub

Multiple Non-contiguous rows in excel Based on cell value

My goal is to be able to have a drop down list that hides certain non-contiguous rows in excel based off the name of the individual in the list I create. I have this code which I found off Youtube and was wondering what was wrong with it as it was not working. I am relatively new to VBA
Private Sub Worksheet_Calculate()
Dim Andrew, Robert, Michael As Range
Set Andrew = Range("K30")
Set Robert = Range("K30")
Set Michael= Range ("K30")
Select Case Andrew
Case Is = "Andrew": Rows("8:10").EntireRow.Hidden = True
Rows("11:12").EntireRow.Hidden = False
Rows("13:13").EntireRow.Hidden = True
Rows("14:25").EntireRow.Hidden = False
End Select
Select Case Robert
Case Is = "Robert"
Rows("6:20").EntireRow.Hidden = True
Rows("21:25").EntireRow.Hidden = False
End Select
Select Case Michael
Case Is = "Michael"
Rows("1:5").EntireRow.Hidden = True
Rows("6:25").EntireRow.Hidden = False
End Select
End Sub
I created a dummy test Excel worksheet and inserted your VBA code into a new module. It worked fine for me, albeit is a bit clunky.
Some suggestions to help:
Always set Option Explicit at the top of your module, because this means any undeclared variables and other little things like that get picked up immediately. It's good practice to get into that habit early when starting out with VBA.
Always qualify your .Range statement with the prefix for the specific workbook/worksheet your code needs to work on. This may be why it isn't working for you, but ran fine for me. As it stands, your code will only run on whatever worksheet happens to be active at the time.
You have made this a Private Sub (private subroutine). If you have done the proverbial copy & paste then this subroutine will not show up in your list of macros, which could be another reason you cannot run it. I highly recommend you have a read of this ExcelOffTheGrid article, which breaks down the different types nicely. If you have inserted this into the Worksheet object of your VBA Project, then it may need to be moved into its own Module.
You have assigned three different names (Andrew, Robert, Michael) to the same .Range reference. This shouldn't really be allowed (although weirdly didn't flag an error when I copied your code) because what it is saying is that those text strings - and they could be anything, not just those names - all refer to the same specific cell on your worksheet. This hasn't affected your code, because you don't actually refer to them later on. In your Select Case logical tests you have used double quotes " " around each name, telling VBA it is a string of characters not a variable you have defined.
I would suggest something like this:
COPY & PASTE INTO A NEW MODULE
Option Explicit
'
'
Sub HideRows()
' This macro will hide specific non-contiguous rows based upon criteria in my drop down combo box.
'
Dim wkMyBook As Workbook
Dim wsMainSheet As Worksheet
Dim rName As Range
'
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
Set wkMyBook = ActiveWorkbook
Set wsMainSheet = wkMyBook.Sheets("ENTER SHEET NAME HERE")
Set rName = wsMainSheet.Range("K30")
Select Case rName
Case Is = "Andrew"
wsMainSheet.Rows("8:10").EntireRow.Hidden = True
wsMainSheet.Rows("11:12").EntireRow.Hidden = False
wsMainSheet.Rows("13:13").EntireRow.Hidden = True
wsMainSheet.Rows("14:25").EntireRow.Hidden = False
Case Is = "Robert"
wsMainSheet.Rows("6:20").EntireRow.Hidden = True
wsMainSheet.Rows("21:25").EntireRow.Hidden = False
Case Is = "Michael"
wsMainSheet.Rows("1:5").EntireRow.Hidden = True
wsMainSheet.Rows("6:25").EntireRow.Hidden = False
End Select
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
End Sub
COPY & PASTE INTO THE WORKBOOK OBJECT
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
' This code will run whenever a change is made to your worksheet
If Target.Address = "$K$30" Then
Select Case Target.Value
Case Is = "Andrew", "Robert", "Michael"
Call HideRows
End Select
End If
End Sub
This has bought your ticket, given you the lift, dropped you off right at the door. Now you gotta do that final step to put this together to make it work. Read the article I linked, learn about bit about how VBA is constructed and then next time you should be a bit further along the path before you need a pick up. Good luck!
Drop Down Worksheet Change
When you change a value in a cell via 'drop down', the Worksheet.Change event is triggered.
Copy the first code into the appropriate sheet module e.g. Sheet1.
Copy the second code into a standard module, e.g. Module1.
You do not run anything, it is automatically showing or hiding rows.
Sheet1
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Const RangeAddress As String = "K30"
If Not Intersect(Target, Me.Range(RangeAddress)) Is Nothing Then
manipulateRows Me, Target.Value
End If
End Sub
Module1
Option Explicit
Sub manipulateRows(Sheet As Worksheet, checkString As String)
With Sheet
Select Case checkString
Case "Andrew"
.Rows("8:10").Hidden = True
.Rows(13).Hidden = True
.Rows("11:12").Hidden = False
.Rows("14:25").Hidden = False
Case "Michael"
.Rows("1:5").Hidden = True
.Rows("6:25").Hidden = False
Case "Robert"
.Rows("6:20").Hidden = True
.Rows("21:25").Hidden = False
Case Else ' When DEL is pressed (Empty Cell), shows all rows.
.Rows("1:25").Hidden = False
End Select
End With
End Sub

trying to find code to force users not to leave cells unpopulated

Thanks Guys. I used the following code to get an error message to pop up if I attempt to save the sheet with one or more of the specified cells left blank. However, when I then go back and populate all the specified blank cells, and then save, I still get the error message. where am I going wrong?
Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim Cell As Range
Dim flag As Boolean
flag = False
If Cells(1, 1) = "" Then flag = True
For Each Cell In Range("C9:C14")
If Cell = "" Then
flag = True
Exit For
End If
Next Cell
For Each Cell In Range("C18:C25")
If Cell = "" Then
flag = True
Exit For
End If
Next Cell
For Each Cell In Range("C30:E35")
If Cell = "" Then
flag = True
Exit For
End If
Next Cell
Cancel = flag
If flag Then MsgBox "error message"
End Sub
Here are 2 small functions checking if "a range" contains any empty cells and if sum of "one range" is equal to sum of "another range". I say this a bit abstract because your ranges may be larger tomorrow or in a different position and you may not want to recode your app time & again.
Function IsFilled(TestRange As Range) As Boolean
Dim C As Range
IsFilled = True
For Each C In TestRange.Cells
If C = "" Then
IsFilled = False
Exit For
End If
Next C
End Function
Function IsSum(TestRange As Range, TestSum As Range) As Boolean
IsSum = (Application.WorksheetFunction.Sum(TestRange) = Application.WorksheetFunction.Sum(TestSum))
End Function
In your context, both functions must evaluate to True for user entry to be valid.
Now you can
enter these functions into two cells of your worksheet (e.g. =isfilled(A2:A7), =issum(A3:A7;A2)) and tell users they have to work hard to make both True
evaluate the two results from above in any appropriate event Sub(), e.g. in a Worksheet_Deactivate(), Workbook_BeforeSave() (I prefer BeforeSave over BeforeClose as the user may want to escape w/o saving in which case I wouldn't need to validate)
directly call and evaluate these two functions from an appropriate event without using them in the worksheet
in any case the validating Sub() must terminate its default action and display a MsgBox() in case validation fails.

Excel keeps running my macro

I have a workbook where several people will make an entry during the week.
Every entry is on its own row. Now i would like to have excel automatic insert the "Windows log-in name" of the user who made the entry, lets say on column K in that speciffic row.
I have found and tried to use the following script.
Function GetName(Optional NameType As String) As String
'Function purpose: To return the following names:
'Defaults to MS Office username if no parameter entered
'
'Formula should be entered as =GetName([param])
'
'For Name of Type Enter Text OR Enter #
'MS Office User Name "Office" 1 (or leave blank)
'Windows User Name "Windows" 2
'Computer Name "Computer" 3
'Force application to recalculate when necessary. If this
'function is only called from other VBA procedures, this
'section can be eliminated. (Req'd for cell use)
Application.Volatile
'Set value to Office if no parameter entered
If Len(NameType) = 0 Then NameType = "OFFICE"
'Identify parameter, assign result to GetName, and return
'error if invalid
Select Case UCase(NameType)
Case Is = "OFFICE", "1"
GetName = Application.UserName
Exit Function
Case Is = "WINDOWS", "2"
GetName = Environ("UserName")
Exit Function
Case Is = "COMPUTER", "3"
GetName = Environ("ComputerName")
Exit Function
Case Else
GetName = CVErr(xlErrValue)
End Select
End Function
I would then call GetName(2) from the relevant cell, but when a new user enter a new entry, all the previous user names are set to the new user.
Any help on this problem, are welcome
Thx
Taz
UPDATE:
Thx for the answers, they helped me get a bit further in solving my problem.
I have now come up with this code, but theres some strange things going on sometimes.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim User As String
User = Environ("UserName")
If Not Intersect(Target, Range("a7:a30")) Is Nothing Then
ActiveSheet.Unprotect
Application.EnableEvents = False
ActiveCell.Offset(0, 10).Value = User
Application.EnableEvents = True
ActiveSheet.Protect
End If
End Sub
This is pretty much working like it should, however it is possible to kinda fool the offset, so it will sometimes write the username only 9 offsets away.
Is it possible to change the code so i can write to a cell in a fixed column, on that row that is active ?
/Taz
With the help of this forum, i was able to make excel do what i wanted, i post the code here.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim row, col, user, ColCell As String
user = Environ("UserName")
col = "G" 'Set the Column ?
If Not Intersect(Target, Range("B7:B30")) Is Nothing Then
ActiveSheet.Unprotect
Application.EnableEvents = False
row = Split(Selection.Address, "$")(2) 'Get row number
ColCell = col & row
Range(ColCell).Value = user
'MsgBox "ColCell is : " & ColCell
Application.EnableEvents = True
ActiveSheet.Protect
End If
End Sub
But i have one question still, i have alot of sheets in my workbook, do i need to put this code in all the sheets, or is there a way that i can avoid this, and only have the code run from one place ?

1004 error on excel 2013 textbox VBA

I want to run a simple script which I would further enhance. I had something similar working for autofilters yesterday but I misplaced it and am having a hard time to find a solution. I want to use an active x textbox to filter a pivot table. I have tried changing the location of the module as well as using acitveworkbook and activesheet and me but all seem to return a 1004 error. I can confirm the textbox is on the page as well as the pivot table. This is office 13.
Private Sub TextBox1_Change()
ActiveSheet.PivotTables("PivotTable2").PivotFields("Agency").ClearAllFilters
ActiveSheet.PivotTables("PivotTable2").PivotFields("Agency").CurrentPage = ActiveSheet.TextBox1.Text
End Sub
I believe the solution I had yesterday had something to do with activating the textbox. But cant quite rememeber.
Any help would be much appreciated.
Your subroutine must be placed in the Worksheet's code module. I believe the error is happening because the _Change event fires with every keystroke, so it is very likely that the first keystroke creates a TextBox1.Text value which does not correspond with any of the PivotItems in that PivotField.
You could do something like this. Use the _GotFocus event to provide an input prompt. That subroutine will then implicitly raise the TextBox1_Change event by assigning the input value to the TextBox1.Text.
Private Sub TextBox1_GotFocus()
Dim str$
str = InputBox("Please enter a value", "Pivot Field filter")
TextBox1.Text = str
End Sub
I temporarily disable error handling (On Error Resume Next) to assign the filter. Then I check to see if an error happened, and you can (optionally) inform the user that they supplied an invalid criteria.
Sub TextBox1_Change()
Dim pt As PivotTable
Dim fld As PivotField
Set pt = PivotTables("PivotTable2") 'Modify as needed
Set fld = pt.PivotFields("Agency")
With fld
.ClearAllFilters
On Error Resume Next
.CurrentPage = TextBox1.Text
.Orientation = xlPageField
.Position = 1
If Err.Number <> 0 Then
'you may want to include a prompt here, i.e.:
MsgBox "Invalid Filter!", vbInformation
Exit Sub
End If
On Error GoTo 0
End With
End Sub

Resources