【问题标题】:How can I find the last row with a cell containing data vba to set print area?如何找到包含数据 vba 的单元格的最后一行以设置打印区域?
【发布时间】:2017-08-16 20:03:46
【问题描述】:

我知道这个问题可能看起来与过去的问题几乎相同,但我的工作表中有一个细微差别,除了前 7 行之外,我的一列完全是空的。问题是我的代码找到了 ALL 单元格包含数据的最后一行,而不是包含至少一个数据项的最后一行。 IE。 A1:Q7 包含数据,并且由于所有行都包含数据,我的代码将打印区域设置为 A1:Q7,尽管 C14 中有数据。我希望我的打印区域为 A1:Q14。我将如何去做这件事。代码如下。

Sub SetPrintArea()

Dim ALastFundRow As Integer
Dim AFirstBlankRow As Integer
Dim wksSource As Worksheet
Dim ws As Worksheet

Dim rngSheet As Range

Set wksSource = ActiveWorkbook.Sheets("WIRE SCHEDULE")
Set ws = ThisWorkbook.Sheets("WIRE SCHEDULE")

'Finds last row of content
ALastFundRow = wksSource.Range("A8").End(xlDown).Row
 'Finds first row without content
AFirstBlankRow = ALastFundRow + 1

Set rngSheet = ws.Range("A1:Q" & LastFundRow + 7)

'Sets PrintArea to the last Column with a value and the last row with a value
ws.PageSetup.PrintArea = rngSheet.Address

结束子

任何事情都会有所帮助。谢谢!

【问题讨论】:

  • 仅供参考,您在 Set rngSheet 中仅使用 LastFundRow 而不是 ALastFundRow。另外,wksSourcews 有什么区别?最后,您可以只使用 C 列作为 ALastFundRow 中的列,不是吗?或者它不会总是拥有最多数据的 C?
  • 哦,哎呀。没有弄乱功能,所以我没有注意到。我对此很陌生,所以感谢您的提示,我尝试了但它没有做任何事情:(
  • C 列有空格吗?试试ALastFundRow = wksSource.Range("C" & Rows.Count).End(xlUp).Row

标签: vba excel


【解决方案1】:

GetLastCell() 函数将查找包含数据的最后一行和最后一列

Option Explicit

Public Sub SetPrintArea()

    Dim ws As Worksheet

    Set ws = ThisWorkbook.Sheets("WIRE SCHEDULE")

    ws.PageSetup.PrintArea = ws.Range("A1:" & GetLastCell(ws).Address).Address

End Sub

Public Function GetLastCell(Optional ByVal ws As Worksheet = Nothing) As Range
    Dim uRng As Range, uArr As Variant, r As Long, c As Long
    Dim ubR As Long, ubC As Long, lRow As Long

    If ws Is Nothing Then Set ws = Application.ThisWorkbook.ActiveSheet
    Set uRng = ws.UsedRange:    uArr = uRng
    If IsEmpty(uArr) Then
        Set GetLastCell = ws.Cells(1, 1):   Exit Function
    End If
    If Not IsArray(uArr) Then
        Set GetLastCell = ws.Cells(uRng.Row, uRng.Column):  Exit Function
    End If
    ubR = UBound(uArr, 1):      ubC = UBound(uArr, 2)
    For r = ubR To 1 Step -1    '----------------------------------------------- last row
        For c = ubC To 1 Step -1
            If Not IsError(uArr(r, c)) Then
                If Len(Trim$(uArr(r, c))) > 0 Then
                    lRow = r:   Exit For
                End If
            End If
        Next
        If lRow > 0 Then Exit For
    Next
    If lRow = 0 Then lRow = ubR
    For c = ubC To 1 Step -1    '----------------------------------------------- last col
        For r = lRow To 1 Step -1
            If Not IsError(uArr(r, c)) Then
                If Len(Trim$(uArr(r, c))) > 0 Then
                    Set GetLastCell = ws.Cells(lRow + uRng.Row - 1, c + uRng.Column - 1)
                    Exit Function
                End If
            End If
        Next
    Next
End Function

【讨论】:

    【解决方案2】:
     With ActiveSheet 'or whatever worksheet  
     LastRow = .Cells.Find(what:="*", after:=.Cells(1, 1), _
                        LookIn:=xlValues, searchorder:=xlByRows, _
                        searchdirection:=xlPrevious).Row
    End With
    

    您可以对最后一列使用类似的算法。

    LastCol = .Cells.Find(what:="*", after:=.Cells(1, 1), _
                    LookIn:=xlValues, searchorder:=xlByColumns, _
                    searchdirection:=xlPrevious).Column
    

    请注意,我们正在寻找xlValues,因此不会包含带有返回空字符串的公式的单元格。

    如果工作表为空,代码会产生错误;所以如果这可能是一种可能性,你应该测试一下。

    【讨论】:

      【解决方案3】:

      试试这个代码。

      .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

      Sub SetPrintArea()
      
      Dim ALastFundRow As Integer
      Dim AFirstBlankRow As Integer
      Dim wksSource As Worksheet
      Dim ws As Worksheet
      
      Dim rngSheet As Range
      
      Set wksSource = ActiveWorkbook.Sheets("WIRE SCHEDULE")
      Set ws = ThisWorkbook.Sheets("WIRE SCHEDULE")
      
      'Finds last row of content
      'ALastFundRow = wksSource.Range("A8").End(xlDown).Row
      ALastFundRow = wksSource.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
       'Finds first row without content
      AFirstBlankRow = ALastFundRow + 1
      
      Set rngSheet = ws.Range("A1:Q" & LastFundRow)
      
      'Sets PrintArea to the last Column with a value and the last row with a value
      ws.PageSetup.PrintArea = rngSheet.Address
      End Sub
      

      【讨论】:

        猜你喜欢
        • 2020-06-22
        • 1970-01-01
        • 2016-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多