somethingz
New Member
- Joined
- Oct 28, 2021
- Messages
- 5
- Office Version
- 365
- 2021
- 2019
- 2016
- Platform
- Windows
Hi!
I've searched everywhere (not just this forum) and I can't figure this problem out and therefore I hope someone here can assist
What I want:
I want to be able to create a folder whenever a new entry is made in i.e. column A with the folder name being whatever is entered in the row. I.e. someone types "321" in A1 and a folder is created called A1
What I have:
I have a module which creates the folder (it's on sharepoint which made it even more complicated) but the module needs a foldername, right now it just says newFolderName = "Test".
I want to pass a variable to this from a change in my sheet. I created a worksheet change code which tracks changes. This is where I'm lost. Do I call this worksheet (I've read that it's not possible to call a worksheet change) or can I store the value elsewhere and then call it from my module?
Here is the module that creates the folders:
I'm grateful of any help even just references on what to read up on to fix this!
I've searched everywhere (not just this forum) and I can't figure this problem out and therefore I hope someone here can assist
What I want:
I want to be able to create a folder whenever a new entry is made in i.e. column A with the folder name being whatever is entered in the row. I.e. someone types "321" in A1 and a folder is created called A1
What I have:
I have a module which creates the folder (it's on sharepoint which made it even more complicated) but the module needs a foldername, right now it just says newFolderName = "Test".
I want to pass a variable to this from a change in my sheet. I created a worksheet change code which tracks changes. This is where I'm lost. Do I call this worksheet (I've read that it's not possible to call a worksheet change) or can I store the value elsewhere and then call it from my module?
VBA Code:
Sub Worksheet_Change(ByVal Target As Range)
Dim rangeToChange As Range
Set rangeToChange = Range("A1:A50") ' This is just for testing, I want it to be something like from A1 to infinity (or maxRows or however its done)
Dim newFolderName As String
If Not Intersect(Target, rangeToChange) Is Nothing Then
newFolderName = Target.Value
End If
End Sub
Here is the module that creates the folders:
VBA Code:
Public Sub SharepointAddFolder()
Dim filePath As String
filePath = "https://COMPANYNAME.sharepoint.com/X/X/X/Where folders are created" ' This part works fine
Dim newFolderName As String
Call Worksheet_Change ' What i want is basically newFolderName = current target value from worksheet change which updates and runs every time worksheet change finds a change
' This is what the code already had but obviously this doesn't update:
' newFolderName = "Test"
Dim driveLetter As String
driveLetter = "Z:"
Dim ntwk As Object
Set ntwk = CreateObject("WScript.Network")
On Error GoTo ErrHandler
ntwk.MapNetworkDrive driveLetter, filePath, False ', "username", "password"
If Len(Dir(driveLetter & "/" & newFolderName, vbDirectory)) = 0 Then
MkDir driveLetter & "/" & newFolderName
Else
MsgBox "Folder " & newFolderName & "already exists."
End If
End Sub
I'm grateful of any help even just references on what to read up on to fix this!