VBA – 检查两个不同的path位置

我有一个现有的VBA项目,我只需要修改,即使有尖叫被重写一天。

该工作表有一个名为“选项”的隐藏工作表,其中列出了B3中的文件path,该工作path称为\ fileserver \ Drafting \ MBS_JOBS \

代码然后分配一个variables这个path:

strpathtofile = Sheets("Options").Range("B3").Value 

最后,它将这一切放在一起:

 strFileToOpen = strpathtofile & ActiveCell.Value & strFilename 

我现在需要做的是检查第二条路。 所以我重复了一些代码。

我首先把新path放在OPTIONS页面的B7中。 然后,我创build了一个variables并分配给它:

 Public strpathtoProj As String strpathtoProj = Sheets("Options").Range("B7").Value 

所以,我需要做的是这个程序也检查这个其他的path。 所以想知道这个部分是否需要某种IF,THEN或ELSE声明:

 strFileToOpen = strpathtofile & ActiveCell.Value & strFilename 

也可以看看strpathtoProj。

我是一个“工作在进步中”的VBA开发者,作为一个小型企业的独立IT人员,我正在学习。

下面是使用strpathtofile的模块(你可以看到我已经有一些代码在那里,我现在需要使用strpathtoProj):

 Sub RUN_SUMMARY_REPORT() 'assign variable... this is here just in case they haven't ran the "TEST" button strpathtofile = Sheets("Options").Range("B3").Value strFilename = Sheets("Options").Range("B4").Value strThisBook = Sheets("Options").Range("B5").Value strExtraInformation = Sheets("Options").Range("B6").Value strpathtoProj = Sheets("Options").Range("B7").Value 'assign variable... this is here just in case they haven't ran the "TEST" button Application.ScreenUpdating = False Application.DisplayAlerts = False ActiveSheet.Unprotect 'Remove any past data SHOW_WARNING (False) ' Extended The Range To Remove data that continued below line 44. Brian 1/20/2015 ' Range("C2:C200").ClearContents ' Jobs Range("F4:S13").ClearContents ' Bar Range("G17:G23").ClearContents ' Web Plate Range("J17:J19").ClearContents ' Cable Range("M17:M23").ClearContents ' Rod Range("P17:P25").ClearContents ' Angle 'Remove any past data 'initialize ExtraInformation Sheets(strExtraInformation).Range("A1:K1000").ClearContents Sheets(strExtraInformation).Select Range("A1").Select 'initialize ExtraInformation SHOW_SHEETS (True) INITIALIZE_PUBLIC_VARS IMPORT_ALL_INFORMATION PRINT_WEB_DATA PRINT_BAR_DATA PRINT_BRAC_DATA PRINT_ROD_DATA PRINT_ANGLE_DATA SHOW_SHEETS (False) ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True End Sub Sub TEST_FOR_BAD_JOB_MUMBERS() Dim bFound As Boolean On Error GoTo EXPLAIN Application.ScreenUpdating = False 'increase performance Application.DisplayAlerts = False 'Unhide all sheets Sheets("REPORT").Visible = True 'Unhide all sheets 'Get all of the settings for this macro and assign variables strpathtofile = Sheets("Options").Range("B3").Value strFilename = Sheets("Options").Range("B4").Value strpathtoProj = Sheets("Options").Range("B7").Value 'Get all of the settings for this macro and assign variables Sheets("REPORT").Select ActiveSheet.Unprotect Range("C2").Select Do Until ActiveCell.Value = "" bFound = True Dim fso As Object Set fso = CreateObject("Scripting.FileSystemObject") 'Wow! What an efficiency increase! If Not fso.FileExists(strpathtofile & ActiveCell & strFilename) Then 'Wow! What an efficiency increase! Error (53) 'file not found error End If ActiveCell.Font.Color = RGB(0, 0, 0) ActiveCell.Font.Bold = False ActiveCell.Offset(1, 0).Select Loop Range("c2").Select 'Clean up the look of this thing! Sheets("Options").Visible = False Sheets("REPORT").Select If bFound Then MsgBox "Test Has Passed! All Job Numbers Found on X-Drive" Else MsgBox "No Jobs!" End If ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True Exit Sub EXPLAIN: 'Clean up the look of this thing! Sheets("Options").Visible = False Sheets("REPORT").Select ActiveCell.Font.Color = RGB(255, 0, 0) ActiveCell.Font.Bold = True MsgBox "One Or More Jobs Do Not Exist. Please Check for RED Highlighted Job." ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True End Sub Sub IMPORT_ALL_INFORMATION() 'Set variables Dim file_in As Long Dim strInput As Variant 'end setting variables Sheets("REPORT").Select Range("C2").Select Do Until ActiveCell.Value = "" '//loop through each job file_in = FreeFile 'next file number strFileToOpen = strpathtofile & ActiveCell.Value & strFilename Open strFileToOpen For Input As #file_in Put_Data_In_Array (file_in) Organize_Array_For_Print Close #file_in ' close the file file_in = file_in + 1 Sheets("REPORT").Select ActiveCell.Offset(1, 0).Select Loop End Sub 

从你的问题的标题来看,这是你所需要的,但是我对你的问题有点困惑:

 sub MainSub() FileOne = worksheets("SuperSecretHiddenSheet").range("A1").value FileTwo = worksheets("SuperSecretHiddenSheet").range("A2").value if bothfileExists(FileOne, FileTwo) = true then 'do stuff end if End Sub function bothfileExists(ByRef FileOne as string, ByRef fileTwo as string) as boolean if (dir(fileone) <> "" and dir(fileTwo) <> "") then bothfileExists = True else bothfileExists = False end if end function