I've got a list of ships in a spreadsheet, and one of the columns will contain the email address for each ship. However, entering an email address turns it into a hyperlink, while I'd like it all stored as plain text. Is there any way to either prevent that from happening or strip the hyperlink out afterwards?
My current attempt is this little sub:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("VesselEmails")) Is Nothing Then Exit Sub
Application.EnableEvents = False
If Target.Hyperlinks.Count > 0 Then Target.Hyperlinks.Delete
Application.EnableEvents = True
End Sub
It doesn't do the job, unfortunately. Is there any way I can do to make it work, or a format mask I can apply or something?
EDIT: To clarify, I don't want to switch off email address autoformatting altogether, just in that column.
You can remove hyperlinks in a column with Columns(1).Hyperlinks.Delete. Run this line in your on change sub and change the 1 in Columns(1) to the column number that has the email data - you can change it to a column letter as well, for example Columns("E").
Hope that will help.
You can try this as an alternative - it is working for me in Excel 2010:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 5 Then
Application.AutoFormatAsYouTypeReplaceHyperlinks = False
Else
Application.AutoFormatAsYouTypeReplaceHyperlinks = True
End If
End Sub
This makes the behaviour column-specific per your edit to the question. Obviously you can extend the If Target.Column = 5 code to include more columns depending on your form.
I realize you want to apply this to only one column and this answer does not achieve that. But for the record, here is how to prevent typed text turning into links or email addresses in all workbooks you open with Excel:
File > Options > Proofing > AutoCorrect options > Autoformat as you type > untick Internet and network paths with hyperlinks
Related
I know this question has been asked ad nauseum and I'm sorry to add to the mix, but I've been through many and varied posts to try to find the solution, and my code is just not doing anything. I have a drop down box in Worksheet tab cell 49 that has 3 options: Hybrid, Online, and Residential. I'd like when a person selects Hybrid or Online rows 37-63 in my Memo tab to be hidden. When a person selects Residential rows 37-63 are visible. I thought this would easy, but no matter what I do, nothing is happening. I have this in the specific "Memo" tab code, not within the module. Any help would be greatly appreciated. Below is the code I have written. Thanks!
Private Sub Worksheet_Change(ByVal Target As Range)
For a = 37 To 63
If Worksheets("Worksheet").Cell(C49).Value <> "Residential" Then
Worksheets("Memo").Rows(a).Hidden = True
ElseIf Worksheets("Worksheet").Cell(C49).Value = "Residential" Then
Worksheets("Memo").Rows(a).Hidden = False
End If
End Sub
Upshot of various comments is I think that your code can be reduced to
Private Sub Worksheet_Change(ByVal Target As Range)
If intersect(Target,range("C49")) is nothing Then Exit Sub
Worksheets("Memo").Rows("37:63").Hidden = (Range("C49").Value <> "Residential")
End Sub
Edit: updated as per #Pᴇʜ's comment.
I have looked at some examples for my question but couldn't find an answer that works.
Background:
I have a list of items (let's say apple, orange, banana) in Sheet1 (A2:A77, which is already a defined range with the name "Liste").
I then have on another sheet (Let's say Sheet2) with several cells where a userform (created with vba code) pops up where the user can choose an item and click OK.
However, due to the nature of the userform (and the list), you can have spelling errors etc and it will still be accepted. So I would like to create a check where it matches the input to the given list (to prevent users from putting anything else in). The userform/code is on purpose to keep it searchable (rather than just a simple data validation list).
Issue:
I tried to create this with vba code that checks the input, matches it to the Sheet1 list and if there is no match, shows a msgbox with a statement. This partially worked (for some letters but not others, very strange).
Here is the code I had:
Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Dim rSearchRng As Range
Dim vFindvar As Variant
If Not Intersect([B7:B26], Target) Is Nothing Then
Set rSearchRng = Sheet4.Range("Liste")
Set vFindvar = rSearchRng.Find(Target.Value)
If Not vFindvar Is Nothing Then
MsgBox "The Audit Project Name you have entered is not valid. Please try again!", vbExclamation, "Error!"
Selection.ClearContents
End If
End If
Application.EnableEvents = True
End Sub
So I was thinking of creating this error message instead with a simple data validation.
Data validation
I have tried the "list" option (and put it equal to the named range) but that did nothing (no error box showed up)
I have tried "Custom" with the following formula 'SUMPRODUCT(--(B12=Liste)>0)=TRUE (I found this on a post which worked for others when I tried it in the cell it gave me the expected "TRUE/FALSE" results) but still nothing shows up
UPDATE
Tigeravatars data validation recommendations work if you don't have a userform (see comments below).
For it to work with a UserForm, I changed the 'MatchEntry' to TRUE and also deleted any unwanted "change events" from my ComboBox code. The final code I use now is below:
Dim a()
Private Sub CommandButton2_Click()
End Sub
Private Sub UserForm_Initialize()
a = [Liste].Value
Me.ComboBox1.List = a
End Sub
Private Sub ComboBox1_Change()
Set d1 = CreateObject("Scripting.Dictionary")
tmp = UCase(Me.ComboBox1) & "*"
For Each c In a
If UCase(c) Like tmp Then d1(c) = ""
Next c
Me.ComboBox1.List = d1.keys
Me.ComboBox1.DropDown
End Sub
Private Sub CommandButton1_Click()
ActiveCell = Me.ComboBox1
Unload Me
End Sub
Private Sub cmdClose_Click()
Unload Me
End Sub
I thought I show it here in case anyone stumbles across my question.
Thank you!
Select your cells where you want to put the data validation
With the cells selected, go to Data Tab -> Validation
Set "Allow" to "List" and set the Source to =Liste as shown here:
Next go to the "Error Alert" tab and set "Style" to "Warning" and enter the desired text in "Title" and "Error Message" as shown here:
Test it out. You should now have a drop-down list of valid choices, and if you attempt to manually enter an invalid choice you'll get the error message you specified.
As a note, if you want the data validation to completely disallow/prevent any entry not in the list, you'll need to set the Error Allert -> Style to be "Stop" instead of "Warning".
EDIT:
Per comments, it can't be a drop-down list. I highly recommend using a drop-down list for this because it will be the most effective way to cut down on time entering data as well as reduce errors from typos. However, if it absolutely cannot be a drop-down list, then you can use a Custom Data Validation and set the formula to =ISNUMBER(MATCH(B7,Liste,0)) (we are using B7 here because it is the first cell in the range of cells that contains this data validation).
Try the following formula:
=NOT(ISERROR(FIND("-"&A1&"-",(TEXTJOIN(,"-",TRUE,Sheet1!A1:A77)))))
That combines all the texts and then see if what's in the cell occurs in the list. I put it between dashes to prevent it from accepting partials.
I have an Excel WorkBook with several Sheets. I would like to be able to select from a drop down list in the "Home" Sheet and after selection is made, automatically switch to the proper Sheet and select a specific Cell.
It appeared to be easy, but I have failed time and again to make it work.
Here is an example of what I would like to do:
The only code that I managed to have a little success with is the following:
Private Sub Worksheet_Activate()
With Sheets("Home")
If Cells(6, 3).Value = "A" Then
Sheets("A").Select
ActiveSheet.Range("B7").Select
End If
End With
End Sub
The problem with it is that it will not check for the value until the user moves to another sheet. Then when it comes back, it will check for it, and will take it to the correct one, but the user will be stuck in a loop without being able to go back to "Home". (I know that it will only work on cell C6, but I just wanted to try if it worked before changing the Range)
You need the worksheet change event, rather than activate. Try this, in the Home sheet module.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$6" Then
Application.Goto Sheets(Target.Text).Range("B7")
End If
End Sub
im not really sure how to word this but im going to try to describe the issue.
I am building a simple excel spreadsheet to keep track of my spending. each cost i input needs to be classified into a category. I have about 20 different categories and forget what they are.
Rather than scrolling up I would like to script a continuous suggestion prompt when i select a cell in a specific column of the spreadsheet. I want to just be able to read off the suggestion as to which category to input (manually).
See nothing too complex, i just know very little about vba syntax and how to phrase the issue. I think i need like a continuous running sub? again not sure what that is called.
Any help is appreciated.
Thank you
Ken
You could you a simple userform to select and parse data selection into the row you clicked.
First use an Event when you click in the column you want the reminder to pop up in. In this case I choose "D".
Private Sub Worksheet_Change(ByVal Target As Range)
Dim arr
If Not Intersect(Target, Range("D:D")) Is Nothing Then
Application.EnableEvents = 0
UserForm1.Show
Target = a
Application.EnableEvents = 1
End If
End Sub
Then load the user form with your reminder data and select the reminder data you need from a userform combobox.
Private Sub UserForm_Initialize()
Dim arr
arr = Array("Unit1", "Unit2", "Unit3", "Unit4", "Unit5", "Unit6", _
"Unit7", "Unit8", "Unit9", "Unit10", "Unit11", "Unit12", _
"Unit13", "Unit14", "Unit15", "Unit16", "Unit17", "Unit18", _
"Unit19", "Unit20")
Me.ComboBox1.List = arr
End Sub
Private Sub ComboBox1_Change()
a = ComboBox1.Value
Unload Me
End Sub
And you will need to put a public variable in a standard module to store the data you want pasted in your sheet.
Public a As String
Be sure to change any control names to suit you.
Hi all i will give an Excel Document for the user with some basic information as follows
Now if the user leaves a cell which is required i would like to prompt him a message saying that it is a required value with in the excel.
I have referred to some articles
But i am unable to achieve what I required so can any one help. Also I would like to know is it possible to apply Regular expression validators with in the Excel. Something like Date format should be mm/dd/yyyy and SSN should be 9 digited like that..
I tried some thing like but this didn't prompt me any error or Dialog
Private Sub Worksheet_BeforeSave(Cancel As Boolean)
If Sheet1.Range("A3:B3").Value = "" Then
Application.EnableEvents = True
MsgBox "Cannot print until required cells have been completed!"
Cancel = True
End If
End Sub
Part 1
That code has a few issues
You need a Workbook event - there is no WorkSheet_BeforeSave event
You cant test for two blank cells with Sheet1.Range("A3:B3").Value = ""
If the code is running then events are already enabled, so this line Application.EnableEvents = True is redundant
Something like this test for both A3 and B3 being non blank on the leftmost sheet
{code goes in the ThisWorkbook module}
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Application.WorksheetFunction.CountA(Sheets(1).Range("a3:b3")) <> 2 Then
MsgBox "Cannot print until required cells have been completed!"
Cancel = True
End If
End Sub
While you can use a Regex for part 2, plain data validation should work fine,ie
a) You can use an "allow" Date in Data Validation
b) You could use a Whole Number between 100,000,000 and 999,999,999 for a 9 digit number
Answer to Part 2 (Regular Expression thing)
You can write a user-defined function in VBA and use it in Excel. Here's an example that extracts parts of a string using RegEx, but with a bit of tweaking you can use it to do validation.