how do you extract the full path of a hyperlink in a cell, ive been given a workbook with a list of generated hyperlinks cells and im trying to extract the full path
when i select a cell and press CTRL+K it just shows the path as ../folder/filename also when i use a vba code below it just shows the same value
what i would like is the full path like "C:\Users\username\folder\filename"
Range("C4") = Cells(4, 1).Hyperlinks(1).Address
i changed the property hyperlink base to 'C:' and then i generated a sample hyperlink and got a '\Users\username\folder\filename' but the earlier cells with hyperlink has still the same result
there isn't any method of properties you can make use of to get the full path.
what you can consider doing is, if the hyperlink is an Excel workbook, you can open it up and then make use of the FullName property, then close the workbook.
Dim wb As Workbook, fullpath As String
For Each wb In Workbooks
If wb.Name <> ThisWorkbook.Name Then
fullpath = wb.FullName
wb.Close SaveChanges:=False
End If
Next wb
Related
I use this piece of code:
Application.Workbooks(V_WBNameOutPut).Activate
to activate a particular excel file, I notice that this method goes in error if the "File name extension" (in the View tab of the Folder Menu) is flagged.
In order to be independent of this, what modification should I do/include to the code or what alternative method should I use?
This answer is based on the comment
I interchange many times during the macro run between 2 workbooks, input and output
excel files, and I need to activate the V_WBNameOutPut, to paste and elaborate, and > this is done multiple times during the run. From the input file, I create the > V_WBNameOutPut file.
As #brax said - capture the workbook when it's opened and you don't have to worry about the extension after that.
Sub Test()
'Open the first workbook and store reference to it.
Dim wrkBk1 As Workbook
Set wrkBk1 = Workbooks.Open("H:\Darren Bartrup-Cook\Test 1.xlsx")
'Open the second workbook and store reference to it.
Dim wrkBk2 As Workbook
Set wrkBk2 = Workbooks.Open("H:\Darren Bartrup-Cook\Test 2.xlsx")
'Copy/paste from wrkbk1 to wrkbk2.
wrkBk1.Worksheets("Sheet1").Range("A1").Copy Destination:=wrkBk2.Worksheets("Sheet1").Range("A4")
'Create a new sheet in wrkbk2.
Dim NewWrkSht As Worksheet
Set NewWrkSht = wrkBk2.Worksheets.Add
NewWrkSht.Name = "My New Sheet"
'Paste copy/paste values from wrkbk1 to wrkbk2.
wrkBk1.Worksheets("Sheet1").Range("A2").Copy
NewWrkSht.Range("A5").PasteSpecial Paste:=xlPasteValues
'Make A3 in wrkbk2 equal the value in wrkbk1 A3.
wrkBk2.Worksheets("Sheet1").Range("A3") = wrkBk1.Worksheets("Sheet1").Range("A3")
'Close the two workbooks.
wrkBk2.Close SaveChanges:=True
wrkBk1.Close SaveChanges:=False
End Sub
I want to create a macro where it will copy some data from one workbook (whose name stays always same - "SameNameWorkbook") and pastes that data in another open workbook whose name is changing everyday (because its name is a date). For example today my workbook which I want to paste the data in is called "11.06.2021".
What I did is I created a =today() formula in the J2 cell in the active workbook (different from the other 2 and named "CurrentWorkbook") and created a variable in VBA for the workbook with changing name:
`Second_workbook = Range("J2").Value`
When I want to have always a reference to the second workbook I wrote this:
`Windows("Second_workbook.xlsx").Activate`
`Range.("A1").Select`
`ActiveSheet.Paste`
Since Second_workbook is a variable linked to the =today() formula which is 11.06.2021 I thought that will put the date before .xlsx. However, this shows an error so my logic is wrong. Since I am more fond of Excel formulas I thought that this logic will work like the indirect function but obviously it doesn't.
So the end result which I want to have is following:
`Windows("11.06.2021.xlsx").Activate`
Tomorrow, then I want to have the following:
`Windows(12.06.2021.xlsx").Activate`
... and so on without me manually changing the name workbook in the macro everyday while I keep all 3 workbooks open of course.
Could you please help me with this issue? I would really appreciate your help.
Date Formatting
You have to format your date:
Format(Date, "dd.mm.yyyy") & ".xlsx"
Format(Range("J2").Value, "dd.mm.yyyy") & ".xlsx"
Date is the equivalent of Excel's TODAY in VBA.
Here's a common scenario (adjust the worksheet names and the ranges):
Option Explicit
Sub CopyToToday()
Dim swb As Workbook: Set swb = ThisWorkbook ' workbook containing this code
' Attempt to create a reference to the Destination Workbook.
Dim dName As String: dName = Format(Date, "dd.mm.yyyy") & ".xlsx"
On Error Resume Next
Dim dwb As Workbook: Set dwb = Workbooks(dName)
On Error GoTo 0
If dwb Is Nothing Then
MsgBox "Could not create a reference to today's workbook.", _
vbCritical, "Workbook not open"
Exit Sub
End If
' Copy a range from Source Worksheet to Destination Worksheet.
swb.Worksheets("Sheet1").Range("A1:F10").Copy
dwb.Worksheets("Sheet1").Range("A1").PasteSpecial
Application.CutCopyMode = False
'dwb.Save
End Sub
"Second_workbook.xlsx" is a string and will be interpreted as a string, ignoring any variables with the same name.
Variables are written out without quotes, and strings of text have the quotes. Everything within quotes (green text) is taken as a string of text. To combine strings and variables we use the & operand like so:
"string" & variable & "string"
So what you are looking for should be:
Windows(Second_workbook & ".xlsx").Activate
You might want to save the workbook as a variable object instead, to refer to it easier:
Dim wb As Workbook
Set wb = Workbooks(Range("J2") & ".xlsx")
Or if you are using the Second_workbook variable anyway, you can set it like:
Set wb = Workbooks(Second_workbook & ".xlsx")
Remember that this range, just as in your example will be interpreted as ActiveWorkbook.ActiveSheet.Range("J").value unless you specify it. Make sure that this won't cuase problems.
To activate a cell in this workbook, you can use wb.Worksheets(1).Range("A1").Select, for example.
I am new to macros.
I'm trying to copy a sheet "Vzorec 1" from current excel file to another opened excel file, which has a name "Financial Overview_123142_test.xlsx". The text after "Financial Overview_" is always changing.
I tried to do it with asterisk but it does not work - see below:
Sheets("vzorec 1").Select
Sheets("vzorec 1").Copy After:=Workbooks("Financial Overview_*.xlsx").Sheets(1 _
)
Thanks in advance.
Br
You need to specify the correct name when accessing the Workbooks-collection (or any other collection in VBA), wildcards will not work. So you will have to loop over all open workbooks and look for a matching name:
Dim wb As Workbook
For Each wb In Workbooks
If wb.Name Like "Financial Overview*" Then
Sheets("vzorec 1").Copy After:=wb.sheets(1)
Exit For
End If
Next
I have a range in excel lets say A1:A50, and every cells contain hyperlink. I sort the range and get the only cells that needed..
Is there any ways using VBA, to save files in the hyperlink from the sorted cells to other folder?
The source link is from computer, not from internet URL.
thanks in advance.
Ciao
Below is the code
, I got this type of error when run it
Run time error '1004':
Sorry, we couldn't find (file name).pdf. Is it possible it was moved, renamed, or deleted?
Sub download ()
Dim linkfile As hyperlink
Dim wb as workbook
Dim savelocation As string
savelocation = "C:\(folder name)"
For Each linkfile In Thisworkbook.Sheets("sheet name").Hyperlinks
Set wb = Workbooks.Open(linkfile.address)
wb.Saveas savelocation & linkfile.Parent & ".pdf"
wb.Close True
Set wb = nothing
Next
End Sub
I have two Excel files open. I would like to create a formula that refers to the current workbook AND another open workbook. The referenced file name is not always the same and may have spaces, and will be used in a formula.
I can reference the current file name and use it in a formula with CELL("filename") but am not sure how reference another (not currently active) open file. How can this be done?
You will need something like this function in your workbook VBA.
Function OtherName()
Dim wb As Workbook, x As String
For Each wb In Workbooks
If wb.Name <> ThisWorkbook.Name Then
x = wb.Name
End If
Next wb
OtherName = x
End Function
Then you just have to call it with your code.