EXCEL TOOLS
Excel Add-ins ที่พัฒนาโดยคุณสันติพงศ์ ณสุย (MVP Excel 2010-2020) ด้วยภาษา C# เพื่อแก้ไขปัญหาไฟล์ใหญ่ คำนวณนาน ทำงานช้า จัดการข้อมูลต่าง ๆ ที่ทำงานประจำวันได้อย่างสะดวกรวดเร็ว สนใจคลิกไปดูได้ที่นี่ครับ => Excel Tools
Excel Add-ins ที่พัฒนาโดยคุณสันติพงศ์ ณสุย (MVP Excel 2010-2020) ด้วยภาษา C# เพื่อแก้ไขปัญหาไฟล์ใหญ่ คำนวณนาน ทำงานช้า จัดการข้อมูลต่าง ๆ ที่ทำงานประจำวันได้อย่างสะดวกรวดเร็ว สนใจคลิกไปดูได้ที่นี่ครับ => Excel Tools
[code]
และปิดด้วย [/code]
ตัวอย่างเช่น [code]dim r as range[/code]
เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)Code: Select all
Code: Select all
Code: Select all
Code: Select all
Code: Select all
Sub RenameFile()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.Worksheets(“List”)
Dim NowPath As String, OldName As String, NewName As String
Dim Row As Long, cnt As String
NowPath = wb.Path
ws.Select
cnt = .Cells(.rows.Count, "A:A").End(xlDown).Row
For Row = 2 To cnt + 1
OldName = NowPath & “ \ ” & Cells(Row, 2)
If Cells(Row, 2) <> “” Then
If Cells(Row, 2) <> wb.Name Then
NewName = NowPath & “ \ ” & Cells(Row, 3)
Name OldName As NewName
End If
End If
Next
End Sub
Code: Select all
Sub DoSomething()
Dim rAll As Range
Dim r As Range
With Sheets("List")
Set rAll = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
End With
For Each r In rAll
MyPath = r.Value
MyFile = r.Offset(0, 1).Value
NewName = r.Offset(0, 2).Value
If Dir(MyPath & MyFile) <> "" Then
Name MyPath & MyFile As MyPath & NewName
End If
Next r
End Sub
อาจารย์ครับ ขอสอบถามหน่อยครับว่าพอจะประยุกต์ใช้ Code VBA เพื่อที่ย้าย File ที่เราต้องการย้ายไปยัง Folder ต่างๆ ได้ไหมครับsnasui wrote:ลองแนบไฟล์ แนบ Code ที่ใช้ มาดูกันครับ
ลองนำไปปรับใช้แล้วไม่ทำงานครับ ผิดพลาดตรงไหนหรือเปล่าครับsnasui wrote:ลองดูตัวอย่าง Code ตามด้านล่างครับ
Code: Select all
Sub DoSomething() Dim rAll As Range Dim r As Range With Sheets("List") Set rAll = .Range("A2", .Range("A" & Rows.Count).End(xlUp)) End With For Each r In rAll MyPath = r.Value MyFile = r.Offset(0, 1).Value NewName = r.Offset(0, 2).Value If Dir(MyPath & MyFile) <> "" Then Name MyPath & MyFile As MyPath & NewName End If Next r End Sub