如何在VBA中使用"输入框"替换用户的输入

  • 本文关键字:quot 替换 用户 VBA vba
  • 更新时间 :
  • 英文 :


嗨,我正在编写一个VBA,在用户定义的路径中创建几个文件夹,并按用户命名。

文件夹结构如下文件夹1>gt>版本号

首先:我需要从用户那里获取他想要创建文件夹的路径第二:我需要用户为文件夹指定名称,文件夹应该创建在相同的名称上第三:我需要用户提到需要反映在子文件夹名称(Rev#(中的文件夹的Rev#

为此编写了以下代码。请看一看,让我知道如何获得上述输出

Sub Create_Folder()
'
' Create_Folder Macro
Dim a As String
Dim b As String
Dim c As String
'
a = InputBox("Enter File Path", "FILE PATH")
b = InputBox("Enter File Name", "FILE NAME")
c = InputBox("Enter Rev #", "REV NUMBER")
Dim OriginalText As String
Dim CorrectedText As String
OriginaText = "C:UsersusernameDesktopMacro"
CorrectedText = Replace(OriginalText, "C:UsersusernameDesktopMacro", "a")
OriginaText = "Folder1"
CorrectedText = Replace(OriginalText, "Folder 1", "b")
OriginaText = "Rev #"
CorrectedText = Replace(OriginalText, "Rev #", "c")
MkDir ("C:UsersusernameDesktopMacroFolder 1")
MkDir ("C:UsersusernameDesktopMacroFolder 1Rev #")`    
End sub

对此进行测试。

Dim UserName as string, DefaultLoc as string, Destination as string, FileName as string
DefaultLoc = "C:Users"
Destination = InputBox("Enter File Path", "FILE PATH")
FileName = InputBox("Enter File Name", "FILE NAME")
UserName = Application.UserName 'note this may not be the type of username you are looking for as I believe this is the Office 365 username.
Location = DefaultLoc & UserName & "DesktopMacro" & Destination & FileName

ActiveWorkbook.SaveAs Filename:= Location

我认为这应该有效,其想法是收集saveas函数所需的所有数据,我认为该函数占用了整个路径,但我不知道它是否创建了位置。如果它还不存在,你可能需要一行代码来创建它上面的文件夹结构,然后再尝试保存它。希望它有效。

最新更新