Open vba in current sheet

Excelexcel86

Board Regular
Joined
Feb 28, 2023
Messages
99
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Hi I have the following code below on multiple sheets in excel where only 1 piece of code changes which is the path as my data is in different folders what I want to know is is there a piece of code I can put into all my sheets so when run it opens in the current sheet because at the moment it opens up another workbook for each one .

Option explicit

Sun openlatestfile

Dim mypath as string
Dim myfile as string
Dim latestfile as string
Dim latestdate as date

The below path is an example
Mypath = “s:\jobs\area\people\”

If right (mypath, 1) <> “\” then mypath = mypath & “\”

Myfile = dir (mypath & “*.csv”,vbnormal)
If len (myfile) = 0 then
Exit sub
End if

Do while len (myfile) > 0

lmd= filedatetime (mypath & myfile)
If lmd > latestdate then
latestfile = myfile
latestdate = lmd
End if

myfile = dir

Loop

workbooks.open mypath & latestfile

End sub

That’s my code the only bit that changes for every sheet is mypath =
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Is it the same question ???

 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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