ไปยังเนื้อหาหลัก

 วิธีตรวจสอบว่ามีโฟลเดอร์อยู่หรือไม่และไม่ได้สร้างขึ้นมา

คุณเคยพยายามตรวจสอบว่ามีโฟลเดอร์อยู่หรือไม่จากแผ่นงาน Excel หรือไม่? ในบทความนี้ฉันจะพูดถึงการตรวจสอบว่ามีโฟลเดอร์อยู่ในเส้นทางที่ระบุหรือไม่หากไม่มีโฟลเดอร์จะถูกสร้างขึ้นโดยอัตโนมัติภายใต้เส้นทาง

ตรวจสอบว่ามีโฟลเดอร์อยู่ในเส้นทางไฟล์ที่ระบุด้วยรหัส VBA หรือไม่

สร้างโฟลเดอร์หากไม่มีอยู่ในเส้นทางไฟล์เฉพาะด้วยรหัส VBA


ลูกศรสีฟ้าฟองขวา ตรวจสอบว่ามีโฟลเดอร์อยู่ในเส้นทางไฟล์ที่ระบุด้วยรหัส VBA หรือไม่

รหัส VBA ต่อไปนี้อาจช่วยให้คุณตรวจสอบว่ามีโฟลเดอร์อยู่ในเส้นทางของไฟล์หรือไม่โปรดดำเนินการดังนี้:

1. กด ALT + F11 คีย์เพื่อเปิด Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

2. คลิก สิ่งที่ใส่เข้าไป > โมดูลและวางรหัสต่อไปนี้ในไฟล์ โมดูล หน้าต่าง.

รหัส VBA: ตรวจสอบว่ามีโฟลเดอร์อยู่ในเส้นทางไฟล์เฉพาะหรือไม่:

Sub Test_Folder_Exist_With_Dir()
'Updateby Extendoffice
    Dim sFolderPath As String
    sFolderPath = "C:\Users\DT168\Desktop\Test folder"
    If Right(sFolderPath, 1) <> "\" Then
        sFolderPath = sFolderPath & "\"
    End If
    If Dir(sFolderPath, vbDirectory) <> vbNullString Then
        MsgBox "Folder exist", vbInformation, "Kutools for Excel"
    Else
        MsgBox "Folder doesn't exist", vbInformation, "Kutools for Excel"
    End If
End Sub

หมายเหตุ ในโค้ดด้านบนคุณควรเปลี่ยนเส้นทางและชื่อโฟลเดอร์ C: \ Users \ DT168 \ Desktop \ Test โฟลเดอร์ ตามที่คุณต้องการ

3. จากนั้นกด F5 กุญแจสำคัญในการเรียกใช้รหัสนี้คุณจะได้ผลลัพธ์ดังต่อไปนี้:

มีโฟลเดอร์ doc 1


ลูกศรสีฟ้าฟองขวา สร้างโฟลเดอร์หากไม่มีอยู่ในเส้นทางไฟล์เฉพาะด้วยรหัส VBA

ตรวจสอบว่ามีโฟลเดอร์อยู่ในเส้นทางของไฟล์หรือไม่หากไม่มีเพื่อสร้างขึ้นภายใต้เส้นทางของไฟล์นี้รหัส VBA ต่อไปนี้อาจช่วยคุณทำงานนี้

1. กด ALT + F11 คีย์เพื่อเปิด Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

2. คลิก สิ่งที่ใส่เข้าไป > โมดูลและวางรหัสต่อไปนี้ในไฟล์ โมดูล หน้าต่าง.

รหัส VBA: สร้างโฟลเดอร์หากไม่มีอยู่ในเส้นทางไฟล์:

Sub MakeMyFolder()
'Updateby Extendoffice
    Dim fdObj As Object
    Application.ScreenUpdating = False
    Set fdObj = CreateObject("Scripting.FileSystemObject")
    If fdObj.FolderExists("C:\Users\DT168\Desktop\Test folder") Then
        MsgBox "Found it.", vbInformation, "Kutools for Excel"
    Else
        fdObj.CreateFolder ("C:\Users\DT168\Desktop\Test folder")
        MsgBox "It has been created.", vbInformation, "Kutools for Excel"
    End If
    Application.ScreenUpdating = True
End Sub

หมายเหตุ: ในโค้ดด้านบนคุณควรเปลี่ยนเส้นทางและชื่อโฟลเดอร์ C: \ Users \ DT168 \ Desktop \ Test โฟลเดอร์ ตามที่คุณต้องการ

3. หลังจากวางรหัสแล้วกด F5 กุญแจสำคัญในการเรียกใช้:

(1. ) หากมีโฟลเดอร์อยู่กล่องพร้อมท์จะปรากฏขึ้นตามภาพหน้าจอต่อไปนี้:

มีโฟลเดอร์ doc 2

(2. ) หากไม่มีโฟลเดอร์อยู่โฟลเดอร์นั้นจะถูกสร้างขึ้นภายใต้เส้นทางที่ระบุพร้อมกันและกล่องพร้อมต์จะปรากฏขึ้นเพื่อเตือนคุณว่าได้สร้างโฟลเดอร์แล้วโปรดดูภาพหน้าจอ:

มีโฟลเดอร์ doc 3

สุดยอดเครื่องมือเพิ่มผลผลิตในสำนักงาน

🤖 Kutools AI ผู้ช่วย: ปฏิวัติการวิเคราะห์ข้อมูลโดยยึดตาม: การดำเนินการที่ชาญฉลาด   |  สร้างรหัส  |  สร้างสูตรที่กำหนดเอง  |  วิเคราะห์ข้อมูลและสร้างแผนภูมิ  |  เรียกใช้ฟังก์ชัน Kutools...
คุณสมบัติยอดนิยม: ค้นหา เน้น หรือระบุรายการที่ซ้ำกัน   |  ลบแถวว่าง   |  รวมคอลัมน์หรือเซลล์โดยไม่สูญเสียข้อมูล   |   รอบโดยไม่มีสูตร ...
การค้นหาขั้นสูง: VLookup หลายเกณฑ์    VLookup หลายค่า  |   VLookup ข้ามหลายแผ่น   |   การค้นหาที่ไม่ชัดเจน ....
รายการแบบเลื่อนลงขั้นสูง: สร้างรายการแบบหล่นลงอย่างรวดเร็ว   |  รายการแบบหล่นลงขึ้นอยู่กับ   |  เลือกหลายรายการแบบหล่นลง ....
ผู้จัดการคอลัมน์: เพิ่มจำนวนคอลัมน์เฉพาะ  |  ย้ายคอลัมน์  |  สลับสถานะการมองเห็นของคอลัมน์ที่ซ่อนอยู่  |  เปรียบเทียบช่วงและคอลัมน์ ...
คุณสมบัติเด่น: กริดโฟกัส   |  มุมมองการออกแบบ   |   บาร์สูตรใหญ่    สมุดงานและตัวจัดการชีต   |  ห้องสมุดทรัพยากร (ข้อความอัตโนมัติ)   |  เลือกวันที่   |  รวมแผ่นงาน   |  เข้ารหัส/ถอดรหัสเซลล์    ส่งอีเมลตามรายการ   |  ซุปเปอร์ฟิลเตอร์   |   ตัวกรองพิเศษ (กรองตัวหนา/ตัวเอียง/ขีดทับ...) ...
ชุดเครื่องมือ 15 อันดับแรก12 ข้อความ เครื่องมือ (เพิ่มข้อความ, ลบอักขระ, ... )   |   50 + แผนภูมิ ประเภท (แผนภูมิ Gantt, ... )   |   40+ ใช้งานได้จริง สูตร (คำนวณอายุตามวันเกิด, ... )   |   19 การแทรก เครื่องมือ (ใส่ QR Code, แทรกรูปภาพจากเส้นทาง, ... )   |   12 การแปลง เครื่องมือ (ตัวเลขเป็นคำ, การแปลงสกุลเงิน, ... )   |   7 ผสานและแยก เครื่องมือ (แถวรวมขั้นสูง, แยกเซลล์, ... )   |   ... และอื่น ๆ

เพิ่มพูนทักษะ Excel ของคุณด้วย Kutools สำหรับ Excel และสัมผัสประสิทธิภาพอย่างที่ไม่เคยมีมาก่อน Kutools สำหรับ Excel เสนอคุณสมบัติขั้นสูงมากกว่า 300 รายการเพื่อเพิ่มประสิทธิภาพและประหยัดเวลา  คลิกที่นี่เพื่อรับคุณสมบัติที่คุณต้องการมากที่สุด...

รายละเอียด


แท็บ Office นำอินเทอร์เฟซแบบแท็บมาที่ Office และทำให้งานของคุณง่ายขึ้นมาก

  • เปิดใช้งานการแก้ไขและอ่านแบบแท็บใน Word, Excel, PowerPoint, ผู้จัดพิมพ์, Access, Visio และโครงการ
  • เปิดและสร้างเอกสารหลายรายการในแท็บใหม่ของหน้าต่างเดียวกันแทนที่จะเป็นในหน้าต่างใหม่
  • เพิ่มประสิทธิภาพการทำงานของคุณ 50% และลดการคลิกเมาส์หลายร้อยครั้งให้คุณทุกวัน!
Comments (12)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Buonasera,

Non conosco il Vs. sito e mi sono imbattuto per caso su questa pagina.
Ho letto quanto scritto sopra e se è possibile avrei bisogno del Vs. aiuto.
Mi occorrerebbe una macro che in un percorso variabile ad un Host facente parte della stessa lan verifiche se è aperto un file exel dal nome variabile, e nel caso sia aperto chiuda il file e cancella tutto il contenuto della cartella compreso il file stesso.
Provo a spiegarmi meglio:
nel percorso :\\host01\Users\utente\Desktop\liste\Nome_Cognome_Gennaio\Operatore_16_Gennaio.xlsm
è presente un file excel dal nome : Operatore_16_Gennaio.xlsm

Il percorso non sempre è lo stesso così come il nome del file excel. Infatti il percorso cambia solo nel Nome_Cognome,es: :\\host01\Users\utente\Desktop\liste\Tizio_Caio_Gennaio\Operatore_16_Gennaio.xlsm) mentre nel file cambia solo il numero dell'operatore (Es: :\\host01\Users\utente\Desktop\liste\Sempronio_zeta_Gennaio\Operatore_15_Gennaio.xlsm.)

E' possibile avere una macro che fa quanto descritto sopra?

Ringrazio anticipatamente
This comment was minimized by the moderator on the site
How to create folder in desktop with vba whenever the excel book is opened, if exist, ignore.
Message if create new folder, silent if the folder exist.

Private Sub Workbook_Open()

Dim cOb As Variant
Dim FolderName As String, FolderExists As String
FolderName = "C:\Users\" & Environ("username") & "\Desktop\MyFolder\" '--->Change folder name to suit.
FolderExists = Dir(FolderName, vbDirectory)

Application.ScreenUpdating = False

If FolderExists = vbNullString Then
MsgBox "The desktop folder doesn't exist. Creating a new folder now.", vbExclamation, "INFORMATION"
cOb = CreateObject("wscript.shell").specialfolders("Desktop") & "\" & "MyFolder" '--->Change folder name to suit.
MkDir cOb
Else: Exit Sub
End If

Application.ScreenUpdating = True

End Sub
This comment was minimized by the moderator on the site
How to create folder in desktop with vba whenever the excel book is opened, if exist, ignore.
Message if create new folder, silent if the folder exist.


Private Sub Workbook_Open()

Dim cOb As Variant
Dim FolderName As String, FolderExists As String
FolderName = "C:\Users\AAAAA\Desktop\A New Folder" '---->Change folder name to suit. Change the AAAAA to your requirement.
FolderExists = Dir(FolderName, vbDirectory)

Application.ScreenUpdating = False

If FolderExists = vbNullString Then
MsgBox "The desktop folder doesn't exist. Creating a new folder now.", vbExclamation, "INFORMATION"
cOb = CreateObject("wscript.shell").specialfolders("Desktop") & "\" & "A New Folder" '--->Change folder name to suit.
MkDir cOb
Else: Exit Sub
End If

Application.ScreenUpdating = True

End Sub

This comment was minimized by the moderator on the site
excelente, me sirvió mucho el Objeto. Uso para carpetas como archivos. Muchas gracias
This comment was minimized by the moderator on the site
Hi, This works great, would there be any chance that the folder name used when checking if a folder already exists is derived from a cell within the spreadsheet, say A2??

I use a template spreadsheet which is updated automatically from another source, so cell A2 constantly changes which requires new folders being created in the same name.

Also, could there be such a command which does the above but also saves the active spreadsheet in the found / created folder?

Any hope? TIA
This comment was minimized by the moderator on the site
I'm running this macro, but in the step to create the folder, the process goes down.

can you help me????


'Comprobar si la carpeta existe

Dim ruta As String
Dim libro As String

M = ActiveWorkbook.Name

ruta = Application.Workbooks(M).Sheets("Diccionario").Range("B5").Value

If Right(ruta, 1) <> "\" Then
ruta = ruta & "\"
End If
If Dir(ruta, vbDirectory) <> vbNullString Then
MsgBox "Folder exist, please continue"
Else
MsgBox "Folder doesn't exist"
End If

'Crea la carpeta que necesitas

Dim fdObj As Object
Dim folder As String

folder = Application.Workbooks(M).Sheets("Dicionario").Range("B5").Value (here is where the process falls)

Application.ScreenUpdating = False
Set fdObj = CreateObject("Scripting.FileSystemObject")
If fdObj.FolderExists(folder) Then
MsgBox "Found it, pleace continue."
Else
fdObj.CreateFolder (folder)
MsgBox "It has been created."
End If
Application.ScreenUpdating = True
This comment was minimized by the moderator on the site
Super Thanks!
This comment was minimized by the moderator on the site
Thanks Man, work amazing
This comment was minimized by the moderator on the site
This is really helpful! thanks!
This comment was minimized by the moderator on the site
Great article. Just what I was looking for :)
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations