VBA code with Excel on OneDrive

Solved
xavier62000 Posted messages 106 Registration date   Status Membre Last intervention   -  
xavier62000 Posted messages 106 Registration date   Status Membre Last intervention   -
Hello everyone,

I have 26 identical files that I call A to Z and another one called 99. The A to Z files are shared with different users (26 total) on OneDrive and contain functioning VBA code.

The procedure for the A to Z files is that the user selects the file to open on OneDrive
Right-click
Open with their Excel
Modifies the data and validates

I tested it and it works

For the file 99, its purpose is to transfer a worksheet from the A to Z files to file 99. The A to Z files are all closed and located in the same directory as file 99, which is OneDrive/directory_name

The existing VBA code in file 99 works locally. Opening it with their Excel on OneDrive works, but the VBA crashes

Below is the code to transfer my data, with the crash point in bold/italic/underlined

Thank you for your help


Sub IMPORTATION()

' Procedure for consolidating multiple workbooks
'
Dim wbk As Workbook
Dim wshScr As Worksheet
Dim wshDst As Worksheet
Dim celDst As Range
Dim rng As Range
Dim chemin As String
Dim nomClasseur As String
Dim derLigne As Long

'Sheets("TRANS_CONSO").Select
Sheets("TRANS_CONSO").Activate
Range("B3:AK10000").ClearContents

' Stop screen refreshing
Application.ScreenUpdating = False
' Set the destination sheet
Set wshDst = ThisWorkbook.Worksheets("TRANS_CONSO")

' Reset the summary file for classifications
With wshDst

' Define the destination cell for the data
Set celDst = .Range("B3")
End With

' Step 2: Loop through all files in the current folder
chemin = ThisWorkbook.Path & "\"

' Name of the first workbook in the folder
nomClasseur = Dir(chemin & "*.xlsm")

' Loop to open the workbooks in the folder
Do While Len(nomClasseur) > 0
If nomClasseur <> ThisWorkbook.Name Then ' except this consolidation workbook

' Open the workbook
Set wbk = Workbooks.Open(chemin & nomClasseur, False)
' Define the JOUEURS worksheet
On Error Resume Next
Set wshScr = wbk.Worksheets("TRANSFERT")
On Error GoTo 0
' Test existence of the sheet
If Not wshScr Is Nothing Then
With wshScr
' Number of the last line of data
derLigne = .Cells(.Rows.Count, "B").End(xlUp).Row
' Replace formulas with their values (to break links)
.UsedRange.Value = .UsedRange.Value
' Copy all data
.Range("B3:AK" & derLigne).Copy celDst
End With
' Define the next destination cell for the data
Set celDst = celDst.Offset(derLigne - 2)

End If
' Close the data workbook without saving changes
wbk.Close False
End If
' Name the next workbook
nomClasseur = Dir

Loop
' Sort by club + name surname

ActiveWorkbook.Worksheets("TRANS_CONSO").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("TRANS_CONSO").Sort.SortFields.Add Key:=Range( _
"D3:D10000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("TRANS_CONSO").Sort.SortFields.Add Key:=Range( _
"F3:F10000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("TRANS_CONSO").Sort
.SetRange Range("B3:AK10000")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' Message
Call TRANSFERT

MsgBox " Import and transfer without duplicates completed "

End Sub

5 réponses

eriiic Posted messages 24581 Registration date   Status Contributeur Last intervention   7 281
 
Hello,

OneDrive on the cloud is not a local or network drive.
What does ThisWorkbook.Path give you out of curiosity?

Make sure to have files A-Z in the OneDrive directory on your PC and work in that directory.
By ensuring that synchronization is working well, it’s often a mess with MS cloud.
Eric

--
By continually trying, we eventually succeed.
So the more it fails, the more chances we have that it works. (the Shadoks)
In addition to the thank you (yes, yes, it happens!!!), remember to mark it as resolved. Thank you
1
xavier62000 Posted messages 106 Registration date   Status Membre Last intervention   3
 
Hi,

Yes, that's what I did, the A-Z files are in the OneDrive directory of my PC. I have no problem with that.

But I also want certain users to be able to use file 99. Is there another solution to meet my needs?

For your curiosity:

From my PC: ThisWorkbook.Path gives you c:\users\my name\onedrive\my directory

But from OneDrive, indeed, it's an access path that doesn't make any sense

Best regards
0
eriiic Posted messages 24581 Registration date   Status Contributeur Last intervention   7 281
 
The path is correct, yes.
I don't understand your error on Dir in this condition.
I tested it on my OneDrive directory just to be sure, but I didn't get any error.
What's the message?
And what does it give if you hardcode everything? :
nomClasseur = Dir("c:\users\my name\onedrive\my directory\*.xlsm")
0
xavier62000 Posted messages 106 Registration date   Status Membre Last intervention   3
 
The error does not occur when we are in the OneDrive directory;

The error occurs when we have launched OneDrive,
selected file 99, right-clicked, opened with HIS Excel
then started the procedure

Execution error code 52
Incorrect file name or number
0
eriiic Posted messages 24581 Registration date   Status Contributeur Last intervention   7 281
 
But you’re opening it from the cloud, if I understand correctly.
You can’t use the online version; it won’t work like that. Your path will be something like https://onedrive.net/xxxx and Excel doesn’t have permission to access it like that.
Especially since doing a Dir is a DOS command.
Everything needs to be opened from c:\users\my name\onedrive\my folder

Well, that’s how it was. Now with Office 365 and 2016, which have improved collaborative work, there may be changes and new possibilities. But in this case, it’s not good for you.
0
xavier62000 Posted messages 106 Registration date   Status Membre Last intervention   3
 
OK. So to conclude, it is impossible for other users to use file 99 by importing data from OneDrive.

The only solution is to open it from my users!!

By any chance, do you know another solution (I’m open to anything)

I’m not picky anymore!!

Thanks Eric
0
eriiic Posted messages 24581 Registration date   Status Contributeur Last intervention   7 281
 
or they put all the files on their PC in a synchronized OneDrive folder and work with the files from the PC.
Just like you.
If they are not permanently connected (for example, when traveling), for safety you should make regular timestamped backups of your folder.
I suppose MS has done things well but still... In case of a problem, you'll be happy to find a not too old copy with minimal updates to make.
0
xavier62000 Posted messages 106 Registration date   Status Membre Last intervention   3
 
Thank you for the information.
0