Folder open prompt Issue VBA

roynaz11

New Member
Joined
Jun 23, 2021
Messages
7
Office Version
  1. 365
Platform
  1. Windows
I am facing an issue while opening a folder via a dialog box, but with manual path, insertion codes working fine. need support pls.

VBA Code:
Public Sub MoveFiles()
' Move any FolderA files (columnA) to dirs in ColumnB
 Dim fldr As FileDialog
Dim sItem As String
Const colA = 1
Const colB = 2
Const colC = 3

Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
    .Title = "Select a Folder"
    .AllowMultiSelect = False
    .InitialFileName = strPath
    If .Show <> -1 Then GoTo NextCode
    sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing


'FolderA = GetFolder()

'Const FolderA = "C:\Users\Muhammad Muzammal\Desktop\New folder (2)\Folder\"    ' source folder

Const srcSheet = "Source"
 
Dim xlS As Excel.Worksheet
Dim xlW As Excel.Workbook
Dim RN As Long              ' row number
Dim fName As String
Dim fPath As String
 
  ' get ready
  
  Set xlW = ActiveWorkbook
  Set xlS = xlW.Sheets(srcSheet)
  
  RN = 2
  fName = Trim(xlS.Cells(RN, colA).Text)
  
  ' run thru ColA until hit a blank
  
  On Error Resume Next  ' expect problems if no target Dir
        
  While fName <> ""
  
    ' if it hasn't aready been moved
    
    If Trim(xlS.Cells(RN, colC).Text) = "" Then
    
      ' got one.
      ' Get the path.  Ensure trailing backslash
      
      fPath = Trim(xlS.Cells(RN, colB).Text)
      
      If Right(fPath, 1) <> "\" Then fPath = fPath & "\"
      
      ' if the target already exists, nuke it.
      
      If Dir(fPath & fName) <> "" Then Kill fPath & fName
      
      ' move it
      
      FileCopy FolderA & fName, fPath & fName
      DoEvents
      
      ' report it
      
      If Err.Number <> 0 Then
      
        xlS.Cells(RN, colC).Value = "Failed: Check target Dir"
        
        Err.Clear
        
      Else
      
        xlS.Cells(RN, colC).Value = Now()
        
      End If
    End If
    
    ' ready for next one
    
    RN = RN + 1
    fName = Trim(xlS.Cells(RN, colA).Text)
    
  Wend
  
  MsgBox "All files moved!!"
      
'End Function
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Unclear, can you please explain your issue?

A couple of things I see right off the bat, though...

1) strFile has not been declared, nor has it been assigned a value. Therefore, the folder initially displayed will be your current folder/last used path.

2) if the user cancels, your code skips to the label NextCode, where sItem gets assigned to GetFolder. The problem, though, sItem is empty at this point and so GetFolder remains empty.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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