오피스

[엑셀]매크로 파일명 변경하기

자료나눔 2013. 11. 28. 13:37

A컬럼에 원본파일 있고 L컬럼에 변경할 파일명 있을 경우 처리

 

Option Explicit

Sub FP_RENAME(sFrom As String, sTo As String)
    Dim sPath As String
   
    on Error Resume Next
    sPath = "G:\MP3\단일곡"
    Name sPath & "\" & sFrom As sPath & "\" & sTo
   
    If Err.no <> 0 Then
        '파일 있을 경우
        Kill sPath & "\" & sFrom
    End If
    on Error GoTo 0
End Sub

Sub FP_WORK()
    Dim lMaxRow As Long
    Dim sCell As String
    Dim sFrom As String
    Dim sTo As String
    Dim lRow As Long
   
    Sheets("Sheet1").Select
   
    Range("A2").Select
    Selection.End(xlDown).Select
    '마지막 자료의 행
    lMaxRow = ActiveCell.Row - 1
   
    For lRow = 2 To lMaxRow + 1
        sCell = "L" & lRow
        Range(sCell).Select
        sTo = Selection.Text
       
        If sTo <> "" Then
            sCell = "A" & lRow
            Range(sCell).Select
            sFrom = Selection.Text
           
            Call FP_RENAME(sFrom, sTo)
        End If
    Next
    Call MsgBox("작업완료")
End Sub