Vba code not fully working if in other sheet or adjacent sheet

muhammad susanto

Well-known Member
Joined
Jan 8, 2013
Messages
2,089
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
hi all..
i have code that working properly only in one sheet (left sheet).
if i run the code in the other sheet (adjacent sheet) the macro not fully working
this code function can to get file name and date modified from a folder
in left sheet or first sheet "file name & date modified from a folder" can be extracted but in adjacent sheet only "file name" can be extracted
how to fit it? the code can running well in different sheet.
here this code :
VBA Code:
Sub GetFileNames()
Dim xRow As Long
Dim xDirect$, xFname$, InitialFoldr$
Dim cell As Range, fso As Object

InitialFoldr$ = "x:\t\" '<<< Startup folder to begin searching from
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select a folder to list Files from"
.InitialFileName = InitialFoldr$
.Show
If .SelectedItems.Count <> 0 Then
xDirect$ = .SelectedItems(1) & "\"
xFname$ = Dir(xDirect$, 1)
Do While xFname$ <> ""
ActiveCell.Offset(xRow) = xFname$
xRow = xRow + 1
xFname$ = Dir
Loop
End If
End With

Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
For Each cell In Range(ActiveCell, ActiveCell.Offset(xRow))
cell.Offset(0, 1).Value = fso.getfile(xDirect$ & cell.Value2).DateLastModified
Next cell
Set fso = Nothing
On Error GoTo 0
End Sub
i hope someone would help out this problem..
any help, greatly appreciated.
.sst
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I am sorry I am unable to reproduce your problem. It works fine for me.
It consistently gives me 2 columns Filename & Date modified.
Even when I selected group sheets it ran fine putting the data on the sheet Excel considered to be the active one out of the group.
 
Upvote 0
thank Alex...but that not work for me
like this : example i have 3 sheets, in sheet1 the code work fine (can extract "file name and date"- my expected] otherwise in sheet2 or sheet3 can extract only "file name"
 
Upvote 0
I can't get it to fail. I assume you don't have a hidden column next to your activecell on sheets 2 and 3.
 
Upvote 0
What happens if you remove this line
VBA Code:
On Error Resume Next
 
Upvote 0
after delete On Error Resume Next
then run the code the result show number 400 and still date not show
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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