2016-12-05 102 views
1

我有一個現有的VBA項目,我只需修改即使尖叫將被重寫一天。VBA - 檢查兩個不同的路徑位置

的片材有一個稱爲選項隱藏薄片,在B3列出的文件路徑,並且該路徑被稱爲\文件服務器\起草\ MBS_JOBS \

然後,代碼分配一個變量此路徑:

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

最後,後來,它這使它們放在一起:

strFileToOpen = strpathtofile & ActiveCell.Value & strFilename 

我現在需要做的是檢查它的第二路徑。所以我重複了一些代碼。

我首先把新路徑放在OPTIONS頁面的B7中。然後,我創建了一個變量並賦予它:

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

所以,我需要做的是有這個計劃還檢查了這另一條路徑。所以,想知道如果我需要某種形式的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 
+1

將有助於看到您正在使用的strpathtofile所以我們可以更好地指導你如何去檢查兩路碼。 – Sorceri

+0

你是什麼意思「也檢查這個其他路徑」? 「檢查」和「看」是什麼意思 - 也就是說,「If」會被測試什麼? – Comintern

+0

我已經添加了子程序原來的職位... – Brian

回答

0

通過的標題來判斷你的問題這是你需要什麼,但我有點困惑您的問題:

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 
+0

我會看看這是否適用於我。目前,如上所述,它從隱藏工作表上的單元格獲取路徑。我知道這可能是懶惰的,但這就是他們最初製作這張表的原因,並不想將其改變太多。但我會看到我能用這個做什麼。 – Brian

+0

@brian我已更新它,以便它將從「SuperSecretHiddenSheet」中的單元格A1和A2中獲取值。您可以根據需要更改範圍 – User632716