• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Files move to particular folder

Abhijeet

Active Member
Hi

I Have macro that macro pull Column A Hyperlink path pull all file from that rename those files then paste in destination folder but now i need changes in this macro. I want only .xls, .xlsx, .xlsm files pull & paste in destination folder.
In this macro not use error handling method i want if any wrong Hyperlink Or Not any .xls ,.xlsx , .xlsm files then those details i want in next sheet.Please help me in this
 

Attachments

  • Hyper Link Move files to Particular Folder.xlsm
    21.5 KB · Views: 12
Try inserting below code into Private Sub MoveFilesAndFolders and comment out Else portion of the code to move .xls, .xlsx, .xlsm files only.

I'm not sure what you mean by 2nd portion of your request.

Code:
 ElseIf fName = "*.xls" Or fName = "*.xlsx" Or fName = "*.xlsm" Then
    FileCopy sourcePath, destPath & c.Offset(, 1).Value & " " & fName
 
Can u please give me file

2nd part is if Hyper link no any Excel file then next sheet give info That Column A hyperlink path in next sheet & If any hyper link unable to open then my current macro will stop their so i do not want to stop macro i want macro goes in next cell & Which cell hyperlink unbale to open that info i want in next sheet
 
Hi
I tried this code but files not move to folder can any one please help me in this

Code:
Sub MainMacro()
'This is the macro to run for all things
Application.ScreenUpdating = False

'CHANGE THIS LINE
'===============
'What Range has hyperlinks originally?
Call MoveFilesAndFolders(Range("A1:A5"))
'===============

MsgBox "Operation complete!"
Application.ScreenUpdating = True
End Sub


Private Sub MoveFilesAndFolders(myRange As Range)
Dim c As Range
Dim destPath As String
Dim sourcePath As String
Dim fName As String

'CHANGE THIS LINE
'===============
'Where are we moving everything to?
destPath = "C:\Users\swami\Desktop\Dest Files Move"
'===============

'Error checking
If Right(destPath, 1) <> "\" Then
    destPath = destPath & "\"
End If

For Each c In myRange
    sourcePath = ""
    On Error Resume Next
    sourcePath = c.Hyperlinks(1).Address
    On Error GoTo 0
    If sourcePath = "" Then
        MsgBox c.Address(False, False) & " does not have a hyperlink." & vbNewLine & "Moving to next cell..."
        GoTo nextCell
    End If
    'Check if it's a file or folder
fName = Dir(sourcePath)
    If fName = "" Then
        'It's a folder
    Call MakeLinks(sourcePath, c.Offset(0, 1).Value)
    'Else
        'It's a file
    'FileCopy sourcePath, destPath & c.Offset(, 1).Value & " " & fName
    ElseIf fName = "*.xls" Or fName = "*.xlsx" Or fName = "*.xlsm" Then
    FileCopy sourcePath, destPath & c.Offset(, 1).Value & " " & fName
    End If
nextCell:
Next c

End Sub

Private Sub MakeLinks(fPath As String, newNum As String)
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
'Pick some column out of the way to put links.
'Currently using col Z
Const myCol As Long = 26

'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder(fPath)
i = 1

'loops through each file in the directory
For Each objFile In objFolder.Files
      'create hyperlink in selected cell
  ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, myCol), Address:= _
        objFile.Path, TextToDisplay:=objFile.Name
    Cells(i, myCol + 1).Value = newNum
    i = i + 1
Next objFile

'Move over the newly copied files
Call MoveFilesAndFolders(Range(Cells(1, myCol), Cells(i - 1, myCol)))

'Clear links
Range(Cells(1, myCol), Cells(1, myCol + 1)).EntireColumn.Clear
End Sub
 
Does it give any error message when you run it? Or simply nothing happens?

Give me few days to look at it.
 
Try attached. I haven't had the chance to look through and test 2nd portion yet.
 

Attachments

  • Hyper Link Move files to Particular Folder2.xlsm
    20.6 KB · Views: 10
Hi Chihiro

Thanks It's work perfect please help me in If any cell Hyperlink is not work then this current macro stop their but i want go to next cell give info in next sheet which hyperlink is not unable to open that info i want in next sheet.Please help me in this part also
 
See attached. Added checks in Sub MakeLinks.

- ErrHandler when Hyperlink does not have valid link/path and copy fPath to another sheet
- Checks if Dir(objFolder & "\*.x*") is empty/blank, then copy path to another sheet.

Make sure that string in Column A Sheet1 does not end with "\".
 

Attachments

  • Hyper Link Move files to Particular Folder2 (1).xlsm
    23.3 KB · Views: 11
Thanks Chihiro

Its work but problem is in this part of code because If Hyper link only contain excel file then also this gives info in sheet 2 can u please tell me what changes should i do
Code:
ErrHandler:
lRow = ThisWorkbook.Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
ThisWorkbook.Sheets("Sheet2").Range("A" & lRow + 1) = fPath
Exit Sub
 
I was bit lazy and didn't check error #.

Change ErrHandler portion to below:
Code:
ErrHandler:
If Err.Number = 76 Then
    lRow = ThisWorkbook.Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
    ThisWorkbook.Sheets("Sheet2").Range("A" & lRow + 1) = fPath
Else
End If
Exit Sub
 
Hi

In this attach file Only hyperlink contain only excel file but in sheet2 show in names i tried to do this but unable to find solution so can u please help me
 

Attachments

  • Hyper Link Move files to Particular Folder2 (1).xlsm
    23.9 KB · Views: 10
Ah, I see where the issue is. The code working as intended.

The issue is with your Hyperlink.

Hyperlink has "..\..\Input Files\100\M03" as address. The code has issue interpreting this when setting "objFolder" and spews error when it's looping through file name check.

Change Hyperlink address to "C:\Users\swami\Desktop\Input Files\100\M03" and you won't have issue.
 
Hi Chihiro

Hyperlink has "..\..\Input Files\100\M03" as address. ok that is not problem. This Hyperlink contain only excel file then why this address is show in sheet2 that is my problem if i do not get correct info which hyperlink is not valid or Excel file in that folder then how can i identify which i want to check which is not correct

That is my problem so please help me in this
 
Let me clarify...

If address is "..\..\Input Files\100\M03" code will go through, but will have issue while stepping through each file and will generate Err.Number 76 at one point.

If you change it to "C:\Users\swami\Desktop\Input Files\100\M03". The code will not generate error and thus will not copy the info into Sheet2.

Have you tried changing address and ran the code? I tested on my end and had no issues.

Personally, I wouldn't have used Hyperlink, but then it isn't my code ;)

P.S. Make sure your destination path/folder exists. Since this code does not check or create folder. If there isn't one, it's going to cause issues.

P.S.2 Did more testing. "..\..\" doesn't matter it looks like. Only the destination folder. Make sure it exists before you run the code.
 
Last edited:
Hi Chihiro

Its work but why this type of address is generate for making hyperlink i am using this code.Thanks again for your support all my queries u resolved.
Code:
Public Sub Convert_To_Hyperlinks()
    Dim Cell As Range
    For Each Cell In Intersect(Selection, ActiveSheet.UsedRange)
        If Cell <> "" Then
            ActiveSheet.Hyperlinks.Add Cell, Cell.Value
        End If
    Next
End Sub
 
Back
Top