2012-02-02 151 views
0

我真的希望有人可以幫助我,因爲我需要做的,如何將文件從英語重命名爲其他語言?

我有一個excel文件,有一些列和一列有英文文件名和其他列有其他語言的文件名。 現在我需要做的是重命名其他語言的文件,是否可以重命名。

我嘗試這個代碼

Sub pdfrenamefile() 
Dim oldfile As String 
Dim nwfile As String 
Dim rng As Range 
Dim fname As Range 
Set rng = Range("Y7", Range("Y" & Rows.Count).End(xlUp)) 
For Each fname In rng 
    If IsEmpty(fname) Or fname = "" Then 
    'do nothing 
    Else 
     If FileFolderExists(Cells(1, 1) & fname) Then 
      nwfile = fname.Offset(, 1) & ".PDF" 
      Name Cells(1, 1) & fname As Cells(1, 1) & nwfile 
      fname.Offset(0, 2) = nwfile 
      fname.Offset(0, 3) = "Success" 
     Else 
      Range("AB" & fname.Row) = "File Not Found" 
     End If 
    End If 
Next fname 
End Sub 

實施例:

示例數據ID OldFileName NewFileName

1 Sales1.PDF తెలుగు1.PDF 
2 Sales02.PDF తెలుగు02.PDF 
3 Sales567.PDF తెలుగు567.PDF 
4 dest67.PDF తెలుగు67.PDF 

我嘗試,但它僅轉換成英語,但不接受對其他。

在此先感謝您的幫助。

+0

您沒有顯示您嘗試過的任何代碼。分享你正在做的事情......以及數據的樣本。 – 2012-02-02 12:34:51

+0

謝謝麥斯歐德快速響應 該守則 子pdfrenamefile() 昏暗的oldfile作爲字符串 昏暗nwfile作爲字符串 昏暗的RNG作爲範圍 昏暗FNAME作爲範圍 設置RNG =範圍(「Y7」,範圍(「Y 「&Rows.Count).END(xlUp)) 對於每個FNAME在RNG 如果爲IsEmpty(FNAME)或者FNAME = 」「 然後 否則 如果FileFolderExists(將細胞(1,1)&FNAME)然後 nwfile = FNAME。 (1,1)&「。PDF」 fname.Offset(0,2)= nwfile fname.Offset(0,3)=「fname.Offset(0,2)&nwfile 」成功「 其他 範圍(「AB」及fname.Row)=「找不到文件」 結束如果 結束如果 接下來FNAME 末次 – user1049518 2012-02-02 12:41:35

+0

請將此代碼添加到這個問題,以便它實際上可讀。此代碼ASIS不可用的任何地方:( – 2012-02-02 12:42:28

回答

4

我已對您的代碼進行了最低限度的更改以使其正常工作。不過,我發現你的代碼很混亂,所以我還推薦了一些進一步的修改。

我不得不將您的示例數據放在範圍X6:Z10中,因此第一個舊文件名在單元格Y7中。

我必須將包含文件的文件夾的名稱放在單元格A1中。

我希望我的變化的原因很明確。問他們是不是。

Sub pdfrenamefile() 

    Dim oldfile As String 
    Dim nwfile As String 
    Dim rng As Range 
    Dim fname As Range 

    ' I find your names confusing. For example, You should rename fname to make 
    ' clear that it is a range. You have declared oldname but do now use it. 

    ' You are using methods that require a file system object 
    Dim fso As Object 
    Set fso = CreateObject("Scripting.FileSystemObject") 

    Set rng = Range("Y7", Range("Y" & Rows.Count).End(xlUp)) 

    ' You use too many different methods of located cells. You use 
    ' rng.Offset(0, c), Cells(r, c) and "AB" & r. Will you understand this 
    ' code in six months? What if you decide to change the position of the 
    ' table of names? 

    For Each fname In rng 

    ' fname is a range. fname.Value is its value. 
    If IsEmpty(fname.Value) Or fname.Value = "" Then 
    Else 
     ' I have replaced "FileFolderExist" by "fso.FileExists". 
     ' "Cells(1, 1)" is acceptable but I prefer "Cells(1, 1).Value" 
     ' which makes absolutely clear you want the value. 
     If fso.FileExists(Cells(1, 1).Value & fname.Value) Then 
     ' You already have the extension in the worksheet so 
     ' do not need to add ".PDF". 
     nwfile = fname.Offset(0, 1).Value 
     ' You check the old file exists but not that the new file 
     ' does not exist. I have added another If-Then-Else-End If. 
     If fso.FileExists(Cells(1, 1) & nwfile) Then 
      Range("AB" & fname.Row) = nwfile & " already exists" 
     Else 
      ' The Name statement will not accept non-English letters. 
      ' I have used MoveFile which will accept non-English letters. 
      ' "fname.Value" not "fname" because "fname" is a range. 
      fso.movefile Cells(1, 1).Value & fname.Value, _ 
                Cells(1, 1).Value & nwfile 
      ' You have nwfile in Offset(0,1). Why duplicate it? 
      fname.Offset(0, 2) = nwfile 
      fname.Offset(0, 3) = "Success" 
     End If 
     Else 
     ' I have added the old name to the message to make clear 
     ' what has not been found. 
     Range("AB" & fname.Row) = fname.Value & " not found" 
     End If 
    End If 
    Next fname 

End Sub 
+0

不值得改善這個代碼+1(並試圖理解問題:)) – JMax 2012-02-02 19:39:57

+0

非常感謝Tony Dallimore。它的工作正常。其實我需要做的是用非英文字母,即其他語言重命名文件。您修改的代碼運行良好。我有一個疑問,非英文文件名將會以原樣顯示(即以非英文顯示)或以符號顯示。按照原樣顯示(非英文不符號)我需要做什麼。感謝您的幫助... – user1049518 2012-02-03 03:46:37

+0

我使用您的名字將虛擬文件重命名,其外觀正確,形狀與您的示例中相同。我相信Windows將允許任何字母表中的任何字母用於文件名中。唯一的問題是他們字體很小,所以很難閱讀。我確信有一種讓他們變大的方法,但我不知道它是什麼。 – 2012-02-03 09:06:22

相關問題