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.
Related
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
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 currently run a sub in another (open) workbook using this line:
Application.Run ("'NDS Analytics.xlsm'!Process")
The names of the referenced workbooks are changing so I would like to change the reference so that instead of looking for file NDS Analytics.xlsm it looks for an open file that contains the words "NDS Analytics".
There will only ever be one file with this name open at a time and it could be called either NDS Analytics.xlsm, wNDS Analytics.xlsm or 40ghNDS Analytics.xlsm.
Application Run with Variable Workbook
Option Explicit
Sub runProcess()
Dim wb As Workbook
For Each wb In Workbooks
If UCase(wb.Name) Like UCase("*NDS Analytics*.xlsm") Then
Application.Run "'" & wb.Name & "'!Process"
Exit For
End If
Next wb
End Sub
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 am trying to write a macro that will move a worksheet (order_line_list) from its workbook to a position after the last sheet of another workbook (Accrual Planning by Month 2020).
The Accrual Planning by Month workbook file name isn't constant, so I have set it up as a variable in the code. The last sheet of that workbook will also change every month as a new sheet is added.
I can get the macro to work if I use the full file name and specify a sheet position (after sheet 2). However, if I try to do it with the file name and sheets count variables, I receive a the following error:
Run-time error 1004: Move Method of Worksheet Class Failed
I tried to use the Copy method instead, but I just receive the same Method Failed error.
I have spent multiple hours researching and trying to figure this out, but have not been able to. I'm new to VBA so there is likely a much simpler way to do what I am trying to do. Below is my code. I have files but can't seem to figure out how to attach them. Your help would be greatly appreciated.
Function wbname(MatchName As String) As String
Dim wb As Workbook
For Each wb In Workbooks
If wb.Name Like MatchName Then
wbname = wb.Name
Exit Function
End If
Next wb
wbname = ""
End Function
Sub Macro302()
' Move Sheet to Accrual Planning by Month file
Accrual = wbname("Accrual*")
Windows("order_line_list.csv").Activate
Sheets("order_line_list").Move After:=Workbooks(Accrual).Sheets.Count
'Sheets("order_line_list").Copy After:=Workbooks(Accrual).Sheets.Count
'Windows("order_line_list.csv").Close SaveChanges:=False
End Sub