Vba 如果文件名等于用户输入变量,则打开该文件

Vba 如果文件名等于用户输入变量,则打开该文件,vba,excel,Vba,Excel,代码: mNummer = InputBox("Please typ a number") If mNummer = "" Then MsgBox ("Makro wont function!") Exit Sub End If Year= InputBox("Select Year", Worksheets("Vorgaben").Range("B14").Value) If Year= "" Then MsgBox("Makro wird abgebrochen!") Ex

代码:

mNummer = InputBox("Please typ a number")
 If mNummer = "" 
Then MsgBox ("Makro wont function!")
 Exit Sub 
End If

Year= InputBox("Select Year", Worksheets("Vorgaben").Range("B14").Value)
 If Year= "" 
Then 
MsgBox("Makro wird abgebrochen!") 
Exit Sub 
End If

 Welle = InputBox("Bitte Welle auswählen", , "0" & Worksheets("Vorgaben").Range("B15"))
 If Welle = "" Then MsgBox ("Makro wird abgebrochen!") 
Exit Sub 
End If

 'Combine the variables in mNummerGanz '
mNummerGanz = mNummer & "_" & Year& "_" & Welle   
Worksheets("Eingabefeld").Range("F2").Value =mNummerGanz
问题是:

所以这里我组合了3个变量,它们要求用户输入3个MessageBox。现在,它的组合版本在变量“mNummerGanz”中

现在我想打开任何Excel文件,方法是转到任何目录并选择它。但是我的宏应该检查所选Excel文件的名称是否等于“mNummerGanz.xls”。如果是,则应打开该文件,如果该文件不等于“mNummerGanz.xls”,则应打印“error”


有人对此有什么建议吗?

如果我理解正确,您正在构建一个字符串,然后要测试它是否是有效的文件名,如果是,请打开它

在这种情况下,这个代码段应该可以为您做到这一点

If Len(Dir(outputpath & mNummerGanz)) <> 0 Then
    Workbooks.Open (outputpath & mNummerGanz)
Else
    MsgBox ("That file does not exist")
End If
如果Len(Dir(outputpath&mNummerGanz))为0,则
工作簿。打开(outputpath和mNummerGanz)
其他的
MsgBox(“该文件不存在”)
如果结束
它检查文件是否存在(outputpath=文件夹位置)
如果是这样,请打开它。

我可以帮忙!也用德语:)我可以用德语说:)

回答:

'typical excel variables
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog

'Retrieve Target FilePath From User
  Set FldrPicker = Application.FileDialog(msoFileDialogFilePicker)

    With FldrPicker
      .Title = "Select A Target File"
      .AllowMultiSelect = False
        If .Show <> -1 Then GoTo NextCode
        myPath = .SelectedItems(1) & "\"
    End With

'In Case of Cancel
NextCode:
  myPath = myPath
  If myPath = "" Then GoTo ResetSettings

'Target File Extension (must include wildcard "*")
  myExtension = "*.xls"

'Target Path with Ending Extention
  myFile = Dir(myPath & myExtension)



'Loop through each Excel file in folder
  If myFile = "mNummerGanz.xls"
    Debug.Print "myFile = " & myFile
    'Set variable equal to opened workbook

      Set wb = Workbooks.Open(Filename:=myPath & myFile)

        'Do your stuff here, man.
        With wb.Worksheets(1)
            'add in your string manipulation / cell dumping here
            'with a few lines


        End With


      'Close opened *.xls, save
      wb.Close SaveChanges:=True
    Else
        GoTo ResetSettings
  End If


ResetSettings:
  'Reset Macro Optimization Settings
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
典型的excel变量 将wb设置为工作簿 将myPath设置为字符串 将myFile设置为字符串 Dim myExtension作为字符串 Dim FldrPicker As FILE对话框 '从用户检索目标文件路径 设置FldrPicker=Application.FileDialog(msoFileDialogFilePicker) 用FldrPicker .Title=“选择目标文件” .AllowMultiSelect=False 如果.Show-1,则转到下一个代码 myPath=.SelectedItems(1)和“\” 以 "如果取消, 下一个代码: myPath=myPath 如果myPath=”“,则转到重置设置 '目标文件扩展名(必须包含通配符“*”) myExtension=“*.xls” '具有结束扩展名的目标路径 myFile=Dir(myPath&myExtension) '循环浏览文件夹中的每个Excel文件 如果myFile=“mNummerGanz.xls” 调试。打印“myFile=”&myFile '将变量设置为等于打开的工作簿 设置wb=Workbooks.Open(文件名:=myPath&myFile) “在这里做你的事,伙计。 带wb.工作表(1) '在此处添加字符串操作/单元格转储 “几句话 以 '关闭打开的*.xls,保存 wb.Close SaveChanges:=真 其他的 转到重置设置 如果结束 重置设置: '重置宏优化设置 Application.EnableEvents=True Application.Calculation=xlCalculationAutomatic Application.ScreenUpdating=True
Wow!够了,Vielen Dank!Dankeschön,我在德国安特卫普!!
'typical excel variables
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog

'Retrieve Target FilePath From User
  Set FldrPicker = Application.FileDialog(msoFileDialogFilePicker)

    With FldrPicker
      .Title = "Select A Target File"
      .AllowMultiSelect = False
        If .Show <> -1 Then GoTo NextCode
        myPath = .SelectedItems(1) & "\"
    End With

'In Case of Cancel
NextCode:
  myPath = myPath
  If myPath = "" Then GoTo ResetSettings

'Target File Extension (must include wildcard "*")
  myExtension = "*.xls"

'Target Path with Ending Extention
  myFile = Dir(myPath & myExtension)



'Loop through each Excel file in folder
  If myFile = "mNummerGanz.xls"
    Debug.Print "myFile = " & myFile
    'Set variable equal to opened workbook

      Set wb = Workbooks.Open(Filename:=myPath & myFile)

        'Do your stuff here, man.
        With wb.Worksheets(1)
            'add in your string manipulation / cell dumping here
            'with a few lines


        End With


      'Close opened *.xls, save
      wb.Close SaveChanges:=True
    Else
        GoTo ResetSettings
  End If


ResetSettings:
  'Reset Macro Optimization Settings
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True