Copy link's address - vba

How I can copy hyperlink's address into clipboard?
Using the "record macro" option, I get this:
Sub CopyHyperlink()
Selection.Range.Hyperlinks(1).Range.Fields(1).Result.Select
Selection.Copy
End Sub
But, this doesn't give me the desired result. See the picture to compare actual and desired results:
As you see, my code is actually copying link's text, instead of it's address.
Probably, there should be something like
Selection.Range.Hyperlinks(1).Address
but it doesn't work at all.
How make it work properly?

Assuming that you have used Selection because you recorded it with macro recorder, you should change it according to your workbook.
Insert the following library:
Tools -> References -> Microsoft Forms 2.0 Object Library
and use the following code:
Sub CopyHyperlink()
Dim clipboard As MSForms.DataObject
Set clipboard = New MSForms.DataObject
clipboard.SetText Selection.Hyperlinks(1).Address
clipboard.PutInClipboard
End Sub

Related

checkbox rename in excel macro

I would like to create an excel macro, in which I create some chechboxes. When I record the macro, I choose the properties of the chechbox in edit mode, then on the left side i give new position values, size values, name and caption to the chechbox. But the macro itself don't remember, just the size and position values.
It will be in the visual basic code:
ActiveSheet.OLEObjects.Add(ClassType:="Forms.CheckBox.1", Link:=False, _
DisplayAsIcon:=False, Left:=128.25, Top:=84.75, Width:=108, Height:= _
21).Select
When I try to write in the code itself, that Caption:="xyz", that will be error.
How can I handle this?
If you want to do this via your VBA macro. Then the code in this question (not answers) can provide the syntax needed.
Otherwise, the following should work, it's not the cleanest but does the job (hat tip to Google for finding this):
Sub test()
Dim obj As OLEObject
Set obj = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CheckBox.1")
obj.Name = "checkboxx"
obj.Object.Caption = "CAPTION"
End Sub
Note that you will not be able to run this using the Step Into functionality of VBA (F8).
Please take a look at the attached image 1

Extract hyperlink target from a cell in different workbook

I have a workbook with a custom right click function that extracts cell values from another workbook depending on what the user chooses. It works very well, I just take in the cell's value from the other workbook. Some cells contain hyperlinks though, and I'd like to import the functional hyperlink, not the value of what's shown in the cell. For example, the following image contains a hyperlink in cell (Y216) of sheet BOS of the input workbook.:
This is an image of the cell I want to copy. It is indeed a hyperlink.
?application.Workbooks(2).Sheets("BOS").Range("Y216").value
returns MKB 70-203 Wicket Shear Pin Detection System, which is indeed correct.
But how do I take the hyperlink's destination? I tried several things including
?application.Workbooks(2).Sheets("BOS").Range("Y216").Hyperlinks.count
returns 0 even though you can see in the image that the hyperlink does have an address. In the same fashion the following sub doesn't enter the For Each because it counts 0 hyperlinks.
Sub HLtester()
Dim HL As Hyperlink
For Each HL In Application.Workbooks(2).Sheets("BOS").Range("Y216").Hyperlinks
Debug.Print HL.Address
Next
End Sub
Expected output would be the link's target J:\SOUM\3191.... as shown in image.
EDIT
If it's important the cell's formula is
=LIEN_HYPERTEXTE("J:\SOUM\3191 M - Old Hickory Dam\11_BOS_FT\02_FT_MECT\21-200 Headcover";"21-200 Headcover")
That's the =HYPERLINK function of French Excel, by the way. I guess in last resort I can take the formula and cut off the function parts to retrieve the link part?
Your command works for me, I don't know why you set the range if you want to loop through all the hyperlinks in the sheet -neither why you set as application. workbooks-, anyways, this worked fine for me:
Sub HLtester()
Dim HL As Hyperlink
For Each HL In Sheets("Sheet1").UsedRange.Hyperlinks
Debug.Print HL.Address
Next
End Sub
You may get it as well within range methods with the following
ActiveCell.Hyperlinks(1).Address
You may get more info here
Edit:
Probably the count is wrong because of the "application.workbook", try to declare it as a variable instead of using it all over the code
Sub HLtester()
Dim HL As Hyperlink
Dim WBAnalyzed As Workbook: Set WBAnalyzed = Workbooks("MyWB.xlsm")
For Each HL In WBAnalyzed.Sheets("Sheet1").UsedRange.Hyperlinks
Debug.Print HL.Address
Next
End Sub
Edit 2:
This is the approach suggested when the hyperlink it's given by its formula
Sub test()
On Error Resume Next 'means no formula
x = Evaluate(Range("A1").Formula)
x1 = Sheets("Sheet1").UsedRange.Hyperlinks.Count
Debug.Print x
Debug.Print x1
End Sub
PS: I saved my variable declaration -just cause-, but, you should always have a neat control for them and use option explicit at the beginning of the module.

copy selection from chrome to excel

I'd like to create a quick way of copying and pasting in between Chrome and Excel. The program would need to perform a task of pasting the selection in a given cell. If we access Excel it's fairly easy, we just need to use Microsoft Forms Library in order to get what we copied to clipboard and paste it in the highlighted field. I'd like to extend this process. Now it looks as follows:
I copy something in Chrome
I need to switch to Excel and access the macro in order to paste what's selected
What I have so far:
Option Explicit
Public Clipboard As New MSForms.DataObject
Sub pasteTable1()
'Tools -> References -> Microsoft Forms 2.0 Object Library
'of you will get a "Compile error: user-defined type not defined"
Dim DataObj As New MSForms.DataObject
Dim S As String
DataObj.GetFromClipboard
S = DataObj.GetText
Debug.Print S 'print code in the Intermediate box in the Macro editor
ActiveSheet.Paste Destination:=Worksheets("Arkusz1").Range("A1")
End Sub
Would it be possible to skip the intermediate step and make Excel paste what's in the clipboard without having to open it? In other words I'd like to have a way of pasting the contents of clipboard automatically once the selection is made.

How to find location of link in Excel workbook?

I have an Excel book that, when opened, gives the warning:
This workbook contains links to other data sources.
I want to remove all of these links so that the warning will not be triggered. Thinking that any external link will be of the form '[workbook path]'!address I used this code:
Sub ListLinks()
Dim wb As Workbook
Dim link As Variant
Set wb = ThisWorkbook
For Each link In wb.LinkSources(xlExcelLinks)
Debug.Print link
Next link
End Sub
This returned a file path:
\\somePath\xyz\aWorkbook.xlsm
I searched all formulas in the workbook for this string using Ctrl+F, but no results were returned. How do I find and remove this link?
Your workbook could be linking via a named range pointing to another workbook. A search of formulas for the linked workbook may find nothing because the link is hidden in the name.
Check your named ranges for links to other workbooks.
Breaking the links is not enough to suppress the warning. On the Edit Links window, I clicked Startup Prompt and set the radio button to "Don't display the alert and don't update automatic links". This successfully prevented the warning from appearing.
The following loop should work.
Dim intCounter As Integer
Dim varLink As Variant
'Define variable as an Excel link type.
varLink = ActiveWorkbook.LinkSources(Type:=xlLinkTypeExcelLinks)
'Are there any links?
If IsArray(varLink) = False Then
Exit Sub
End If
'Break the links in the active workbook.
For intCounter = 1 To UBound(varLink)
ActiveWorkbook.BreakLink _
Name:=varLink(intCounter), _
Type:=xlLinkTypeExcelLinks
Next intCounter
I had this problem after removing all the links as described above, I then found that some of my validation data was refering to other workbooks. After correcting this the problem disappeared.
Could be in one of these?:
HYPERLINKS (formula and normal)
NAME MANAGER
VALIDATION
CONDITIONAL FORMATTING
BUTTON WITH MACRO (still pointing to originating macro)

Spell check an Excel sheet in VBA

I have scoured the Internet and I have found a handful of possible solutions to this issue, but I wanted to ask here as well.
The goal is to click a button, and spell check an entire sheet.
Here's some code
Sub spellCheck()
Sheet1.Cells.CheckSpelling
End Sub
Also, I found this:
Sub SpellCheck()
Dim Checkword As String, Result As Boolean
Checkword = Selection.Value
Result = Application.CheckSpelling(Checkword)
Selection.Offset(0, 1) = Result
End Sub
Any ideas? Neither is working for me. Thanks!
You can check the whole workbook by doing something like:
Sub SpellCheck()
For Each sh In Worksheets
Sheets(sh.Name).Cells.CheckSpelling
Next
End Sub
this will cycle through each sheet in the entire book and run a spellcheck on each one. What I can't figure out yet is how to make the spell checker actually move to the position of the spelling error. So, with the above you just get a list of spelling errors with no context with which to asses them.
I noticed I just had a typo in my code.
Below works:
Sub spellCheck()
Sheet1.Cells.CheckSpelling
End Sub
But, if anyone knows how to do the entire workbook, I'd be interested in that. Thanks.
This code will work on selected cells .This will highlight if any spell mistakes in a cell
Dim Myrange As Range
Selection.SpecialCells(xlVisible).Select
For Each Myrange In Selection
If Application.CheckSpelling(word:=Myrange.Value) = False Then
Myrange.Font.Color = vbRed
End If
Next
OK, so you can use the following command to invoke the toolbar's spellchecker which does move you to the position of the spelling error as long as you have screen updating enabled at the time.
Application.CommandBars("Tools").Controls("Spelling...").Execute
You can use this command embedded in the loop above to loop through the sheets in the workbook and invoke this command on each new sheet.
Cap
This uses a code snippet from a previous answer to restrict the area used for spellchecking to a specific region. Something I needed to do in a small project. This give the full functionallity of the spellchecker with errors shown in context. The rowNumber is calculated elsewhere.The selection range can be fully controlled elsewhere in your code to suit your particular need. Thought this might help others searching this posting.
With Sheets("Sheet1")
slic = CStr(rowNumber)
.Range("AL3:AN" & slic).Select
Application.CommandBars("Tools").Controls("Spelling...").Execute
End With
Thanks to previous posters this solved a problem form me. I am most grateful.