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

时间:2016-02-15 13:22:41

标签: excel vba excel-vba

守则:

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个消息框。现在它的组合版本是变量“mNummerGanz”。

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

有没有人对此有建议?

2 个答案:

答案 0 :(得分:0)

如果我已正确理解,您构建了一个字符串,然后您要测试该字符串是否为有效的文件名,如果是,请将其打开?

在这种情况下,此代码段应该为您执行此操作

If Len(Dir(outputpath & mNummerGanz)) <> 0 Then
    Workbooks.Open (outputpath & mNummerGanz)
Else
    MsgBox ("That file does not exist")
End If

检查文件是否存在(outputpath =文件夹位置) 如果是的话,打开它。

答案 1 :(得分:0)

我可以帮忙!同样在德国:) Ich kann dir auf Deutsch helfen :)

 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").Value 
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

答案:

'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