change text direction in excel using VB code - excel

I want to change text direction in a cell from left-to-right to right-to-left using vb code.
Can any one guide me to do that?
thanks.

It is easy, you just need to create a Function in Excel with the following code:
Function Reversestr(str As String) As String
Reversestr = StrReverse(Trim(str))
End Function
Then just use the function inside a cell like
=Reversestr("Text you want to reverse")

Related

Using excel function in created VBA function

I am trying to write a function in VBA, that I can then use in excel...
This is not the exact function that I want to create, but I'm having a lot of trouble so I simplified it down a lot...
Suppose I have the following matrix in my excel spreadsheet.
I know there is a built-in function in excel to find the determinate. Now, I want to use that in my function I define in VBA. Again there will be more stuff in my function, but I'm just trying to understand what I'm doing wrong.
So suppose this is my function I've defined in VBA...
Option Explicit
Public Function determinate(mtx As Range)
determinate = Application.MDeterm(mtx)
End Function
However, when trying to use determinate() in excel it doesn't calculate. Why is this/How can I fix this?

Excel Formula needed in separate column

I can't figure this out, Maybe you guys can help me.
I need to separate just the image name for example
xxqti8eli5h2f4abpiz2.jpg
lvfdpujvgkf75ve8ikob.jpg
In a separate column, I've a list of 8000 image name which I need to separate out any help from you guys is much appreciated.
http://images.XXX.com/image/upload/s--B3cI5sks--/c_limit,cs_srgb,h_600,w_600/xxqti8eli5h2f4abpiz2.jpg
http://images.XXX.com/image/upload/s--_3R1kbWq--/c_limit,cs_srgb,h_600,w_600/lvfdpujvgkf75ve8ikob.jpg
formula version:
=MID(A1,FIND("}}}",SUBSTITUTE(A1,"/","}}}",LEN(A1)-LEN(SUBSTITUTE(A1,"/",""))))+1,LEN(A1))
Since the image is always the last chunk of your string when you separate/split the string by a forward slash, you can use a really tiny UDF to get this functionality.
In a new VBE Module (Alt+F11, then right click your workbook and Insert>>Module), paste the following:
Function get_image(url As String) As String
'extract the last token from the url when it is split by forward slash
get_image = Split(url, "/")(UBound(Split(url, "/")))
End Function
Now you can use this new function in your worksheet:

Writing a string instead of true/false for a checkbox in VBA?

I have been trying to output a string when a checkbox is clicked in excel. The checkboxes are currently linked to a separate page in the work book. The code I am writing so far is:
Private Sub Rollout()
If Rollout.Value = True Then
Range(D11, [D11]).Value = "Roll-Out"
Else
Range(D11, [D11]).Value = "No Roll-Out Service"
End If
End Sub
Rollout() is the Macro assigned to the checkbox. The cell on the page that I want the string to output is D11.
I'm pretty new to VBA so I'm not sure if I'm using the correct command window or if I need to write a function and then call it in the sub?
Any help would be great, Thanks!
Something like this:
Worksheets("AppropriateWorksheetName").Range("D11").Value = "No Roll-Out Service"
I'm pretty new to VBA so I'm not sure if I'm using the correct command window or if I need to write a function and then call it in the sub?
The command window writes a function; they both do the same thing, it's just that the command window is easier to use but has more limited functionality. If you are just learning, the command window is fine

Creating a custom hyperlink function in excel

I have searched far and wide, but can't find an answer to this simple question. I want to make a custom function in excel which will create a hyperlink.
Excel has a built in hyperlink function that works like this:
=Hyperlink(link_location, display_text)
I want to create a function called CustomHyperlink which takes one parameter, and returns a hyperlink to a google query with that parameter. Just for the sake of the question, lets assume that the passed parameter is a alphanumeric string, with no spaces.
Essentially, calling
=CustomHyperlink("excel")
should be the same as calling
=Hyperlink("http://www.google.com/search?q=excel", "excel")
This seems like such a simple task, but I absolutely cannot find a way to make this function.
Can anyone offer some quick help?
I can offer a partial solution, one that will update an existing hyperlink. This only makes sence if you are using it like, say
CustomHyperlink(A1)
were A1 contains the required serch term
To use,
enter your UDF formula in a cell, eg =CustomHyperlink(A1)
create a hyperlink on the cell (right click, Hyperlink...) . This can be any hyperlink, valid or invalid
put the required search term in the referenced cell, eg in A1 put excel
When the UDF runs it will update the hyperlink to Google the entered search term
Function CustomHyperlink(Term As String) As String
Dim rng As Range
Set rng = Application.Caller
CustomHyperlink = Term
If rng.Hyperlinks.Count > 0 Then
rng.Hyperlinks(1).Address = "http://www.google.com/search?q=" & Term
End If
End Function
In VBA editor you can use
ThisWorkbook.FollowHyperlink Address:=(strWebsite), NewWindow:=True
Which will take you to that specific website, and just build a function around that to navigate you to the site you need.
Nice idea although this isn't possible.
You seem to want to have the formula of the cell as one thing (your custom function call) and yet have the value as another (the hyperlink / URL) which simply isn't possible.
The correct way through VBA to add a hyperlink is to use the Hyperlinks property but it is not possible to call this property, through a Worksheet UDF (because of the reason above).
What is wrong with just using the the built-in =Hyperlink() worksheet function? You could effectively parameterise your URL as follows (where cell A1 = Excel):
=HYPERLINK("http://www.google.com/search?q="&A1)
You can't do this directly for the reasons creamyegg suggests, but there is a way to achieve the functionality albeit with a bit of a performance consideration.
You could use the Worksheet_Change event to track for the presence of your UDF then process the hyperlink addition there.
You would need to set up an empty function to allow this to happen, otherwise Excel will throw an error whenever you entered =CustomHyperlink... in a cell.
The below should work, not really had time to test.
Private Sub worksheet_change(ByVal target As Range)
Dim SearchValue As String
If LCase(Left(target.Formula, 16)) = "=customhyperlink" Then
SearchValue = Mid(target.Formula, 19, Len(target.Formula) - 20)
target.Value = SearchValue
target.Hyperlinks.Add target, "http://www.google.com/search?q=" & SearchValue, , "Search Google for " & SearchValue, SearchValue
End If
End Sub
The performance consideration is of course the volatile Worksheet_Change event as this can really kill large, complex workbooks.

how do I use a excel function inside the vba editor

I want to write a Excel function like this. It is an extension of the Dec2Bin function
Public Function Dec2BinEx(x As Long)
Dec2BinEx = dec2bin(x) + 10
End Function
But I am getting an error when trying to use it. How do I call a excel function inside the visual basic editor
In general, you call Excel functions with Application.WorksheetFunction.SomeFunctionName. However, Dec2Bin is special, as it is an Add-In function, and not a pure Excel function. Hence, Application.WorksheetFunction does not work here. Instead, you have to make the functions of the add-in available to your code. To do so, follow these steps
In Excel, menu Tools/Add-Ins, make
sure that the add-in Analysis
ToolPak - VBA is imported.
Then, set a reference to this add-in
in your code: in the VBA editor, menu
Tools/References, add a reference to
atpvbaen.xls.
Then, your original code, as posted in your quesiton should work just fine.
You will first of all have to create a module eg from menu select Insert->Module. Then inside this module create a function named main. This function is run by default when code is run. Now inside this function call your own function like this:
Sub main()
Call Dec2BinEx(your_value_here)
End Sub
Public Function Dec2BinEx(x As Long)
Dec2BinEx = dec2bin(x) + 10
End Function
Having done that, make sure that you have the reference to dec2bin function or if you create that too. Thanks

Resources