This code works, but could it all be done in VBA?

Razzy

Board Regular
Joined
Jul 24, 2020
Messages
106
Office Version
  1. 365
Platform
  1. Windows
VBA Code:
Sub rabbit()
Dim myFile As String
Dim FilePicker As FileDialog

Dim B       As String
Dim B1()    As String

Set FilePicker = Application.FileDialog(msoFileDialogFilePicker)
    With FilePicker
      .Title = "Choose file"
      .AllowMultiSelect = False
        If .Show <> -1 Then GoTo Velg
        myFile = .SelectedItems(1)
    End With
    
Workbooks.Open Filename:=myFile
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "=CELL(""filename"")"
    B = Range("A1").Value
    Selection.ClearContents
    B = "='" & B & "'" & "!$A$1"
    B1 = Split(B, "]")
    B = B1(0) & "]"

***Any better idea how to get the workbook reference? Can I avvoid writing inside the workbook?

The code will turn a filepath into a workbook reference.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
is this what you need, it uses the FSO object,
VBA Code:
Sub rabbit()
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")

Dim myFile As String
Dim FilePicker As FileDialog

Dim B       As String
Dim B1()    As String

Set FilePicker = Application.FileDialog(msoFileDialogFilePicker)
    With FilePicker
      .Title = "Choose file"
      .AllowMultiSelect = False
        If .Show <> -1 Then Exit Sub
        myFile = .SelectedItems(1)
        fname = FSO.GetFileName(myFile) 'The file name
       MsgBox (fname)
    End With
End Sub
 
Upvote 0
This outputs "File 1.xlsx"

But I need: C:\Users\Sylvester\Desktop\[File 1.xlsx]. To further turn it into a workbook reference.

But it is okay. Thx for your help! I can use what I have.

Right now I am struggeling with replacing the workbook reference Ive created, with a text I have in my template "rabbit.xlsm":

VBA Code:
Workbooks("rabbit.xlsm").Activate
Sheets("Oversikt").Select
    Cells.Select
    Selection.Replace What:="FILE1", Replacement:=B, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2

Is it not possible to search and replace a line of text in a template, with a variable B?
 
Upvote 0
Variabe B is: ='C:\Users\Sylvester\Desktop\[File 1.xlsx], and in my template I write: FILE1Sheet4'!$A$4, wich should output:

='C:\Users\Sylvester\Desktop\[File 1.xlsx]!$A$4

Right now it wont replace. It works with text.
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,549
Members
449,089
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top