① 如何用VBA打開選擇文件和選擇路徑的對話框
'注意引用microsoftoffice 10.0 (或以上)object library
'在文件對話框對中返回選擇一個文件夾的路徑.
Public Function ChooseFolder() As String
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogFolderPicker)
With dlgOpen
If .Show = -1 Then
ChooseFolder = .SelectedItems(1)End IfEnd With
Set dlgOpen = Nothing
End Function
'--------------------------------------------------------
'在文件對話框對中,選擇一個文件。
Public Function ChooseOneFile(Optional TitleStr As String
= 選擇你要的文件, Optional TypesDec As String = 所有文件,
Optional Exten As String = *.*) As String
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogFilePicker)
With dlgOpen
.Title = TitleStr
.Filters.Clear '清除所有的文件類型.
.Filters.Add TypesDec, Exten
.AllowMultiSelect = False '不能多選.
If .Show = -1 Then
' .AllowMultiSelect = True '多個文件
' For Each vrtSelectedItem In .SelectedItems
' MsgBox Path name: & vrtSelectedItem
' Next vrtSelectedItem
ChooseOneFile = .SelectedItems(1) '第一個文件End IfEnd With
② 如何運用VBA將指定路徑下的某一文件復制到另一指定路徑下
這種dos命令能緩攜完成的操作,直接在VBA里用核散shell執行即可,比純vba代碼省事太多了。詳情改哪氏見
excelhome
。
③ vba如何設置首次打開文件路徑
1、首先打開vba,並登錄自己的賬號。
2、其次選中地址所在單元格,運行下述代碼即可打開指定的文件夾。
3、最後選中路徑,點擊打開即可。
④ VBA如何獲取當前EXCEL文件的路徑
1、首先打來開Excel數據表,在開發自工具中打開VBA編輯器。
⑤ vba打開文件後如何獲得打開文件的路徑
S=activeworkbook.Path & "\" & activeworkbook.Name
或
S=workbooks("test.xls").Path & "\" & workbooks("test.xls").Name
⑥ VBA中怎麼通過打開文件的對話框獲取選擇文件的路徑並在其他模塊中引用該路徑
1.插入一來個模塊或在已有的源模塊最上面一行定義一個全局變數
比如: Public iFileName As String
2.在這個工作薄的其他模塊中都可以引用這個變數
比如:(先執行一次test1,再執行test2)
Sub test1()
'打開一個選擇文件的對話框
iFileName = Application.GetOpenFilename
End Sub
Sub test2()
If iFileName = "False" Then
MsgBox "沒有選擇文件!"
Else
wz = InStrRev(iFileName, "\")
Path = Left(iFileName, wz)
fname = Right(iFileName, Len(iFileName) - wz)
MsgBox "選擇的文件名為:" & fname & vbCrLf & "路徑為:" & Path
End If
End Sub
⑦ 請問高人 vba中 選擇文件的路徑,和文件名 放在哪個控制項中 怎麼實現謝謝你
Sub DateSave()
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:= _
ThisWorkbook.Path & "\" & Date & ".xls", _
FileFormat:=xlNormal, _
Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, _
CreateBackup:=False
Application.DisplayAlerts = True
End Sub
請將「ThisWorkbook.Path & "\" & Date & ".xls", _」改為「"d:\" & Date & ".xls", _」,其中「"d:\" 」就是你要存的路徑。
希望我的回答對你有所幫助。
⑧ VBA中怎樣實現當一個文件用滑鼠拖到From界面的文本框中的時候,把文件名顯示在文本框中並記錄它的路徑
在窗體上添加如下控制項各1個:
DriveListBox --- 驅動器列表
DirListBox ---- 文件夾列表
FileListBox ---- 文件列表
TextBox ---- 文本框
CommandButton ---- 命令按鈕
完整代碼如下:
Option Explicit
Private Sub Command1_Click()
Unload Me
End Sub
Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub
Private Sub Drive1_GotFocus()
OldDrive = Drive1.Drive
End Sub
Private Sub File1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
File1.Drag 1
End Sub
Private Sub File1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
File1.Drag 2
End Sub
Private Sub Drive1_Change()
Dim msg As String, title As String
Dim style, response
On Error GoTo ErrorNum1
Dir1.Path = Drive1.Drive
Exit Sub
ErrorNum1:
msg = "驅動器沒有準備好!" & Chr(13) & Chr(10) & Chr(13) & Chr(10) & _
"或者磁神派盤損壞。"
style = vbRetryCancel + vbCritical + vbDefaultButton1
title = "錯誤"
response = MsgBox(msg, style, title)
If response = vbRetry Then
Resume
Else
Drive1.Drive = OldDrive
Exit Sub
End If
End Sub
Private Sub Form_Load()
File1.Pattern = "*.TXT"
File1.Path = Dir1.Path
End Sub
Private Sub Text1_DragDrop(Source As Control, X As Single, Y As Single)
Dim MyLineStr As String
Dim TextStr As String
Dim MyFileN As Long
MyFileN = FreeFile
TextStr = ""
Open Dir1.Path & "\" & File1.FileName For Input As #MyFileN ' 打開文件。
Do While Not EOF(MyFileN) ' 循環至文件尾。
Line Input #1, MyLineStr ' 讀入一行數據並將其賦予某變數。
TextStr = TextStr & MyLineStr & Chr(13) & Chr(10)
Loop
Close #1 ' 關閉文件脊瞎早櫻雀。
Text1.Text = TextStr
End Sub
⑨ 如何通過VBA,獲得本文件所在的文件夾路徑。
Subs()
Dimpth$
pth=ThisWorkbook.Path
MsgBox"本文件的路徑為:"&pth
EndSub
⑩ 如何運用VBA將指定路徑下的某一文件復制到另一指定路徑下
1、首先打開工作表凳正空,點擊【開發工具】下的【Visual Basic 】選項。