Skip i'r prif gynnwys

 Sut i wirio a oes ffolder yn bodoli ac os nad ei greu?

A ydych erioed wedi ceisio gwirio a yw ffolder yn bodoli ai peidio o daflen waith Excel? Yn yr erthygl hon, byddaf yn siarad am wirio a yw ffolder yn bodoli mewn llwybr penodol, os na, bydd y ffolder yn cael ei greu yn awtomatig o dan y llwybr.

Gwiriwch a yw ffolder yn bodoli mewn llwybr ffeil penodol gyda chod VBA

Creu’r ffolder os nad yw’n bodoli mewn llwybr ffeil penodol gyda chod VBA


swigen dde glas saeth Gwiriwch a yw ffolder yn bodoli mewn llwybr ffeil penodol gyda chod VBA

Efallai y bydd y cod VBA canlynol yn eich helpu i wirio a oes ffolder yn bodoli mewn llwybr ffeil penodol, gwnewch fel hyn:

1. Daliwch i lawr y ALT + F11 allweddi i agor y Microsoft Visual Basic ar gyfer Ceisiadau ffenestr.

2. Cliciwch Mewnosod > Modiwlau, a gludwch y cod canlynol yn y Modiwlau Ffenestr.

Cod VBA: Gwiriwch a yw ffolder yn bodoli mewn llwybr ffeil penodol:

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

Nodyn: Yn y cod uchod, dylech newid llwybr ac enw'r ffolder C: \ Defnyddiwr \ DT168 \ Penbwrdd \ Ffolder prawf i'ch angen chi.

3. Yna pwyswch F5 yn allweddol i redeg y cod hwn, fe gewch y canlyniadau canlynol:

ffolder doc yn bodoli 1


swigen dde glas saeth Creu’r ffolder os nad yw’n bodoli mewn llwybr ffeil penodol gyda chod VBA

Gwiriwch a yw ffolder yn bodoli mewn llwybr ffeil, os na, i'w greu o dan y llwybr ffeil penodol hwn, gall y cod VBA canlynol eich helpu i orffen y swydd hon.

1. Daliwch i lawr y ALT + F11 allweddi i agor y Microsoft Visual Basic ar gyfer Ceisiadau ffenestr.

2. Cliciwch Mewnosod > Modiwlau, a gludwch y cod canlynol yn y Modiwlau Ffenestr.

Cod VBA: Creu ffolder os nad yw'n bodoli mewn llwybr ffeil:

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

Nodyn: Yn y cod uchod, dylech newid llwybr ac enw'r ffolder C: \ Defnyddiwr \ DT168 \ Penbwrdd \ Ffolder prawf i'ch angen chi.

3. Ar ôl pasio'r cod, a gwasgwch F5 allwedd i'w redeg:

(1.) Os yw'r ffolder yn bodoli, bydd blwch prydlon yn ymddangos fel y dangosir y llun a ganlyn:

ffolder doc yn bodoli 2

(2.) Os nad yw'r ffolder yn bodoli, bydd yn cael ei greu o dan y llwybr penodol ar unwaith, a bydd blwch prydlon yn popio allan i'ch atgoffa bod y ffolder wedi'i chreu, gweler y screenshot:

ffolder doc yn bodoli 3

Offer Cynhyrchiant Swyddfa Gorau

🤖 Kutools AI Aide: Chwyldro dadansoddi data yn seiliedig ar: Cyflawniad Deallus   |  Cynhyrchu Cod  |  Creu Fformiwlâu Personol  |  Dadansoddi Data a Chynhyrchu Siartiau  |  Invoke Swyddogaethau Kutools...
Nodweddion Poblogaidd: Darganfod, Amlygu neu Adnabod Dyblygiadau   |  Dileu Rhesi Gwag   |  Cyfuno Colofnau neu Gelloedd heb Colli Data   |   Rownd heb Fformiwla ...
Super-edrych: VLookup Meini Prawf Lluosog    VLookup Gwerth Lluosog  |   VLookup Ar Draws Taflenni Lluosog   |   Edrych Niwlog ....
Rhestr gwympo Uwch: Creu Rhestr Gollwng yn Gyflym   |  Rhestr Gollwng Dibynnol   |  Rhestr Gollwng Aml-ddewis ....
Rheolwr Colofn: Ychwanegu Nifer Penodol o Golofnau  |  Symud Colofnau  |  Toglo Statws Gwelededd Colofnau Cudd  |  Cymharwch Ystodau a Cholofnau ...
Nodweddion dan Sylw: Ffocws ar y Grid   |  Golwg Dylunio   |   Bar Fformiwla Mawr    Rheolwr Llyfr Gwaith a Thaflen   |  Llyfrgell Adnoddau (Testun Auto)   |  Dewiswr Dyddiad   |  Cyfuno Taflenni Gwaith   |  Amgryptio/Dadgryptio Celloedd    Anfon E-byst trwy Restr   |  Hidlo Super   |   Hidlo Arbennig (hidlo mewn print trwm/italig/strikethrough...) ...
15 Set Offer Gorau12 Testun offer (Ychwanegu Testun, Dileu Cymeriadau,...)   |   50 + Siart Mathau (Siart Gantt,...)   |   40+ Ymarferol Fformiwlâu (Cyfrifwch oedran yn seiliedig ar ben-blwydd,...)   |   19 mewnosod offer (Mewnosod Cod QR, Mewnosod Llun o'r Llwybr,...)   |   12 Trosi offer (Rhifau i Eiriau, Trosi arian cyfred,...)   |   7 Uno a Hollti offer (Rhesi Cyfuno Uwch, Celloedd Hollt,...)   |   ... a mwy

Supercharge Eich Sgiliau Excel gyda Kutools ar gyfer Excel, a Phrofiad Effeithlonrwydd Fel Erioed Erioed. Kutools ar gyfer Excel Yn Cynnig Dros 300 o Nodweddion Uwch i Hybu Cynhyrchiant ac Arbed Amser.  Cliciwch Yma i Gael Y Nodwedd Sydd Ei Angen Y Mwyaf...

Disgrifiad


Mae Office Tab yn dod â rhyngwyneb Tabbed i Office, ac yn Gwneud Eich Gwaith yn Haws o lawer

  • Galluogi golygu a darllen tabbed yn Word, Excel, PowerPoint, Cyhoeddwr, Mynediad, Visio a Phrosiect.
  • Agor a chreu dogfennau lluosog mewn tabiau newydd o'r un ffenestr, yn hytrach nag mewn ffenestri newydd.
  • Yn cynyddu eich cynhyrchiant 50%, ac yn lleihau cannoedd o gliciau llygoden i chi bob dydd!
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