2016-09-21 40 views
1

因此,我正在對一組數字文件進行審計,但我認爲我寫的方式非常複雜,所以也許有人可以幫助我...如何在不激活紙張的情況下引用紙張上的單元格?

我無法弄清楚爲什麼activesheet.cell工作正常,但是當我嘗試使用Workbooks(「Master List」)工作表(「Master Shipped」)單元格引用單元格時,它給了我一個錯誤。我已經用完整的文件位置嘗試過了,我用worksheets.cells試過了,我用sheets.cells試過了,我試過讓工作表變成一個變量,但是我無法使它工作。

它的工作,在那裏我命名變量的頂部,但它不工作,我的if語句這是我想要的工作,因爲我不想有激活片各10次循環。

Application.ScreenUpdating = False 
LR = Cells(Rows.Count, 2).End(xlUp).Row 
Dim AuditSheet As Worksheet, Mastersheet As Worksheet 
Set AuditSheet = ThisWorkbook.Sheets("Audit") 
Set Mastersheet = ThisWorkbook.Sheets("Master Shipped") 

For r = 3 To LR 
    FpathBOL = "U:\Warehouse Associate\Shipped Orders 2016\Bill of Lading\" 
    fnamebol = Mastersheet.Cells(r, 21).Text 
    FNamePOD = Left(Mastersheet.Cells(r, 21).Text, (Len(Mastersheet.Cells(r, 21)) - 8)) 
    FpathFile = "V:\LVA Files\" & Mastersheet.Cells(r, 4).Value & "\Line " & Mastersheet.Cells(r, 10).Value & "\" 
    FnameFile = Mastersheet.Cells(r, 4).Value & "-" 
    BOL = FpathBOL & "\" & fnamebol & ".pdf" 
    POD = FpathBOL & FNamePOD & "POD.pdf" 
    File1 = FpathFile & FnameFile & "PO.pdf" 
    File2 = FpathFile & FnameFile & "EIC PO.pdf" 
    File3 = FpathFile & FnameFile & "EDI 855.pdf" 
    File4 = FpathFile & FnameFile & "EDI 870.pdf" 
    File5 = FpathFile & FnameFile & "VENDOR INVOICE.pdf" 
    File6 = FpathFile & FnameFile & "EIC INVOICE.pdf" 
    File7 = FpathFile & FnameFile & "PCGR.pdf" 
    File8 = FpathFile & FnameFile & "PL.pdf" 
    File9 = FpathFile & FnameFile & "EDI.pdf" 
If Dir(File1) = "" Then 
    Workbooks("Master List.xlsm").Worksheets("Audit").Activate 
    ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Activate 
    ActiveCell.Value = File1 
End If 
If Dir(File2) = "" Then 
    Worksheets("Audit").Activate 
    ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Activate 
    ActiveCell.Value = File2 
End If 

If Dir(File3) = "" And Dir(File9) = "" Then 
    Worksheets("Audit").Activate 
    ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Activate 
    ActiveCell.Value = File3 
End If 
If Dir(File4) = "" And Dir(File9) = "" Then 
    Worksheets("Audit").Activate 
    ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Activate 
    ActiveCell.Value = File4 
End If 
If Dir(File5) = "" Then 
    Worksheets("Audit").Activate 
    ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Activate 
    ActiveCell.Value = File5 
End If 
If Dir(File6) = "" Then 
    Worksheets("Audit").Activate 
    ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Activate 
    ActiveCell.Value = File6 
End If 
If Dir(File7) = "" Then 
    Worksheets("Audit").Activate 
    ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Activate 
    ActiveCell.Value = File7 
End If 
If Dir(File8) = "" Then 
    Worksheets("Audit").Activate 
    ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Activate 
    ActiveCell.Value = File8 
End If 
If Dir(BOL) = "" Then 
    Worksheets("Audit").Activate 
    ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Activate 
    ActiveCell.Value = FnameFile & BOL 
End If 
If Dir(POD) = "" Then 
    Worksheets("Audit").Activate 
    ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Activate 
    ActiveCell.Value = FnameFile & POD 
End If 
Next r 
Application.ScreenUpdating = True 
End Sub 
+0

閱讀,學習和愛情:怎樣避免使用'.Select' /'.Activate'](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select -in-Excel的VBA的宏)。 :D – BruceWayne

+0

語法爲'Workbooks(「Master List.xlsm」)。Sheets(「Audit」)。Cells(1,1)'修改Cell'A1'。你也可以使用'Workbooks(「Master List.xlsm」)。表格(「Audit」)。Range(「A1」)' – user1274820

回答

1

要利用現有的代碼片段:

If Dir(File1) = "" Then 
    Workbooks("Master List.xlsm").Worksheets("Audit").Activate 
    ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Activate 
    ActiveCell.Value = File1 
End If 

作爲一個經驗法則,你可以刪除後.Activate和領先ActiveSheet/ActiveCell報表和合併線:

If Dir(File1) = "" Then 
    Workbooks("Master List.xlsm").Worksheets("Audit").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Value = File1 
End If 

但是,這只是一個經驗法則。注意例如使用Rows.Count。這與活動工作表有關,因此如果不進行進一步編輯就無法工作。

If Dir(File1) = "" Then 
    With Workbooks("Master List.xlsm").Worksheets("Audit") 
     .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Value = File1 
    End With 
End If 
+0

呃,那太簡單了,我討厭自己沒有得到它。非常感謝你! – Victoria

+0

很高興我能幫到你。但請參閱我的添加/編輯 – ThunderFrame

相關問題