如何在不激活纸张的情况下引用纸张上的单元格?

时间:2016-09-21 21:29:34

标签: excel vba excel-vba

所以我对一组数字文件进行了审核,但我认为我写的方式很复杂,所以也许有人可以帮助我......

我无法弄清楚为什么activesheet.cells工作正常,但当我尝试使用工作簿引用单元格时(" Master List")。工作表(" Master发货")。单元格给我一个错误。我已经尝试了完整的文件位置,我已经尝试使用worksheets.cells,我已经尝试过使用sheet.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

1 个答案:

答案 0 :(得分: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