change TEXT to display in excel via vba - excel

Is there a way to change the text to display of all the hyperlinks in a sheet?
All I want to do it is run a macro to trim the the existing TEXT to Display to 5 chrs only?
At the mo, my text to display is really big text string something like "21253.bla bla bla.wla wla wla. dah dah dah.jpg" and I only want to display 21253
Is this even possible?
Regards
Shei

Loop through the links on the sheets and use the left function to trim the text to display. Example below
Dim hl As Hyperlink
For Each hl In ActiveSheet.Hyperlinks
hl.TextToDisplay = Left(hl.TextToDisplay, 5)
Next

Related

How to programmatically print a long text string in 3 columns over multiple lines in excel vba

I have a long string of text. I want to print this in 3 columns over 2 pages in excel, using vba. e.g.:
A|B|C
-----
D|E|F
This is easy to do in ms word, I just split the page into 3 columns and print the text and it automatically goes onto the next column/page once the previous on is full. But I need to do similar in excel.
Ideas I've explored:
Having a textbox that stretches over 2 pages - splits into 3 columns, but the arrangement is:
a|c|e
b|d|f
I can't seem to find a way to put a page break within the text box.
Have 6 separate text boxes and split the text up - can't find a way of determining when one box is full so that the other can be started. Can't even determine how many lines the text takes up as characters have varying widths.
Have 6 separate large cells and split the text up - same issues as above.
Does anyone know of a way this can be overcome? I just want to replicate the behaviour of ms word.
Edit: here's the code for a textbox with columns that I can't get to page break:
Dim tb_1
Set tb_1 = jumbledwords_sheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 10, 7470, 488, 1300)
tb_1.TextFrame2.Column.Spacing = 10
tb_1.TextFrame2.Column.Number = 3
tb_1.TextFrame2.TextRange.Font.Size = 9
tb_1.TextFrame2.TextRange.Characters.Text = long_string
The stucture of long_string isn't important, and I can break it etc. to fit the solution.

How to Start Cursor After 3rd character of text box in UserForm

I am fairly new to VBA so any help would be greatly appreciated.
I have a UserForm that contains 3 labels and 3 text boxes. 2 of the text boxes (including the first one that is activated upon the form's activation) are empty. The third has 3 characters pre-filled into it. When the userform starts, I would like to fill in the first text box as normal then when I tab into the second text box, I would like the cursor to automatically go to the end of that 3 letters instead of highlighting the entire text box. I have looked around but cannot seem to find anything that can do this easily. Could you please let me know where exactly (which sub) to enter this code into as well?
My UserForm is called 'GetBIInfo2' and my text box is called 'NBIDText'
Thanks!
Select the textbox.
Go to property (F4).
Scroll till you find EnterFieldBehavior property.
Change it to 1 - FmEnterFieldBehaviorRecallSelection.
The direct approach is via the textbox'es .SelStart property; possibly you might call it via a sub procedure like this:
Sub fillAnyTextBox(myTextBox As MSForms.TextBox, ByVal firstCharacters As String)
With myTextBox
.Value = firstCharacters
.SetFocus
.SelStart = Len(firstCharacters)
.SelLength = Len(.Text) - Len(firstCharacters)
End With
End Sub
A simple alternative would be to make another text box to hold the first three characters and have the user enter the following characters into a separate textbox. Then you can write code that will take the contents of both and append them into a single string like this:
Dim finalString As String
finalString = TextBox1.Value & TextBox2.Value

Finding a character count in Word VBA

I'm trying to dynamically generate a word document from an excel spreadsheet. The way that I am going about doing it now is basically manually finding character count, and then will eventually insert a given item at that specific character count. The given item is stored in excel. So it would be like:
Spreadsheet
| 1 | 2 | 3
Char | 50 | 125 | 250
Item | Hello | Darkness | Friend
And then in the word VBA, use
Dim myCheck As ContentControl
Dim myRange As Range
Dim rng As Long
rng = ''whatever char is for that column
Set myRange = ActiveDocument.Range(start:=rng, End:=rng)
Set myCheck = ActiveDocument.ContentControls.Add(wdContentControlCheckBox, myRange)
'what I am doing now is adding a check box dynamically at a given position on the range. This can be either checkboxes that are added or text or whatever.
The issue is: how do I find locate a specific point on the a word document? The form that I am trying to insert these into is static, so I can safely assume that if I want to insert it at character 100, each time I make a new form it will insert it in the same place.
I tried using 'word count' character button, but that didn't seem to be accurate. It was consistently 40 or so characters in front of where I wanted it to be. Didn't see a pattern.
Is there an easy way to place my cursor at a location on a document and know how many characters came before it, as counted by the range object? Or is there a more efficient way of locating things on a word document, provided that I cannot alter the word document itself in appearance (can't add grids, grid layouts, tables, or anything like that).
I believe the bookmark functionality is what you're looking for.
You can create bookmarks in a static Word Document beforehand and use them as your insertion target... or you can create the bookmarks programmatically with VBA and then use the insert text option.
Examples for using a bookmark to insert text.
Sub InsertAtBookmarkI()
ActiveDocument.Bookmarks("bmAtBookmark").Range.InsertAfter "Some text here"
End Sub
MSDN Document for creating bookmarks programmatically.
Sub Mark()
ActiveDocument.Bookmarks.Add Name:="mark"
End Sub
Sub ThirdPara()
Dim myDoc As Document
' To best illustrate this example,
' Letter.doc must be opened, not active,
' and contain more than 3 paragraphs.
Set myDoc = Documents("Letter.doc")
myDoc.Bookmarks.Add Name:="third_para", _
Range:=myDoc.Paragraphs(3).Range
myDoc.ActiveWindow.View.ShowBookmarks = True
End Sub

VBA ListBox and ComboBox display wrong characters

Excel VBA ListBox and ComboBox display wrong characters while TextBox provides the correct ones, using the same UserForm.
ListBox And ComboBox provides some Ansi substitute instead of Baltic characters, that is wrong.
I've changed:
charset of form and listBox from 133 into 163 by using ListBox.Font.Charset property
use of font Arial or Times New Roman with Baltic encoding
read through tons of pages on the internet and still no luck..
Thanks for your attention
I used code as follows:
Private Sub UserForm_Initialize()
UserForm2.Font.Charset = 186
ListBox1.List = Array("ĄČęėį", "Žųūįšų", 222) -> does not provide correct text :(
With ListBox1
.AddItem "Vilnius"
.AddItem "Kaunas"
.AddItem "Klaipėda" -> Klaipëda
.AddItem "Šiauliai" -> Ðiauliai
.AddItem Chr(222)
End With
Finally looks like VBA editor has trouble with international characters - if use button caption through Object Properties window - it goes wrong.
But entering labels and button text directly on the form provides correct outcome, hence there is a work around here - use RowSource option, taking data from excel table, which gives right encoding:
ListBox1.RowSource = "=Sheet2!A1:A5"
This solution is convenient to my needs and the question is closed by now.
Thank you for your response.
To populate ListBox and ComboBox with correct international characters I used Object Property window providing RowSource and taking data from Excel table.
In VBA editor it looks like this: ListBox1.RowSource = "=Sheet2!A1:A5"
Such an approach works well with Baltic and Russian languages, I did not try it with other languages.

Add a space after colored text

I'm Using Microsoft Excel 2013.
I have a lot of data that I need to separate in Excel that is in a single cell. The "Text to Columns" feature works great except for one snag.
In a single cell, I have First Name, Last Name & Email address. The last name and email addresses do not have a space between them, but the color of the names are different than the email.
Example (all caps represent colored names RGB (1, 91, 167), lowercase is the email which is just standard black text):
JOHN DOEjohndoe#acmerockets.com
So I need to put a space after DOE so that it reads:
JOHN DOE johndoe#acmerockets.com
I have about 20k rows to go through so any tips would be appreciated. I just need to get a space or something in between that last name and email so I can use the "Text to Columns" feature and split those up.
Not a complete answer, but I would do it way:
Step 1 to get rid of the formatting:
Copy all text that you have to the notepad
Then copy-paste text from Notepad to excel as text
I think this should remove all the formatting issues
Step 2 is to use VBA to grab emails. I assume that you have all your emails as lowercase. Therefore something like this should do the trick (link link2):
([a-z0-9\-_+]*#([a-z0-9\-_+].)?[a-z0-9\-_+].[a-z0-9]{2,6})
Step 3 is to exclude emails that you extracted from Step2 from your main text. Something like this via simple Excel function:
=TRIM(SUBSTITUTE(FULLTEXT,EMAIL,""))
Since you removed all the formatting in Step1, you can apply it back when you done
You can knock this out pretty quickly taking advantage of a how Font returns the Color for a set of characters that do not have the same color: it returns Null! Knowing this, you can iterate through the characters 2 at a time and find the first spot where it throws Null. You now know that the color shift is there and can spit out the pieces using Mid.
Code makes use of this behavior and IsNull to iterate through a fixed Range. Define the Range however you want to get the cells. By default it spits them out in the neighboring two columns with Offset.
Sub FindChangeInColor()
Dim rng_cell As Range
Dim i As Integer
For Each rng_cell In Range("B2:B4")
For i = 1 To Len(rng_cell.Text) - 1
If IsNull(rng_cell.Characters(i, 2).Font.Color) Then
rng_cell.Offset(0, 1) = Mid(rng_cell, 1, i)
rng_cell.Offset(0, 2) = Mid(rng_cell, i + 1)
End If
Next
Next
End Sub
Picture of ranges and results
The nice thing about this approach is that the actual colors involved don't matter. You also don't have to manually search for a switch, although that would have been the next step.
Also your neighboring cells will be blank if no color change was found, so it's decently robust against bad inputs.
Edit adds ability to change original string if you want that instead:
Sub FindChangeInColorAndAddChar()
Dim rng_cell As Range
Dim i As Integer
For Each rng_cell In Range("B2:B4")
For i = 1 To Len(rng_cell.Text) - 1
If IsNull(rng_cell.Characters(i, 2).Font.Color) Then
rng_cell = Mid(rng_cell, 1, i) & "|" & Mid(rng_cell, i + 1)
End If
Next
Next
End Sub
Picture of results again use same input as above.

Resources