我试图让Excel根据A列中输入的值创建一个文件夹,并将文档复制到新创建的文件夹中。我暂时将复制功能注解掉了。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Z As Long
Dim xVal As String
On Error Resume Next
If Intersect(Target, Range("A:A")) Is Nothing Then Exit Sub
Application.EnableEvents = False
For Z = 1 To Target.Count
If Target(Z).Value > 0 Then
Call MakeFolders
'Call FileCopy
End If
Next
Application.EnableEvents = True
End Sub
Sub MakeFolders()
Dim Rng As Range
Dim LastRow As Long
Dim LastColumn As Long
Dim maxRows As Integer
Dim r As Integer
Dim c As Integer
Set sht = Worksheets("NPI")
Set StartCell = Range("A2")
LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = 1
sht.Range(StartCell, sht.Cells(LastRow, LastColumn)).Select
Set Rng = Selection
maxRows = Rng.Rows.Count
For c = 1 To 1
r = 1
Do While r <= maxRows
If Len(Dir(ActiveWorkbook.Path & "\" & Rng(r, c), vbDirectory)) = 0 Then
MkDir (ActiveWorkbook.Path & "\" & Rng(r, c))
On Error Resume Next
End If
r = r + 1
Loop
Next c
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
End Sub
'Sub FileCopy()
'Dim SourcePath As String
'Dim DestinationPath As String
'SourcePath = "C:\Users\xxxxx.xxxxx\Documents\Directory\NPI\test.txt"
'DestinationPath = "C:\Users\xxxxx.xxxxx\Documents\Directory\NPI\NP10001\test.txt"
'FileCopy SourcePath, DestinationPath
'End Sub
我遇到了一个问题的目的地,有没有一种方法,我可以让它调用Mkdir文件夹?即使是最新的文件夹也可以。
我试着命名一个目录并手动将文件移出,但整个子目录不值得做,除非这部分是自动完成的
1条答案
按热度按时间7cwmlq891#
尝试