2017-08-16 29 views
0

我知道這個問題看起來可能與過去的問題幾乎完全相同,但是我的工作表中有一個細微差別,那就是我的一列除了前7行外完全是空的。問題是我的代碼找到最後一行,其中ALL單元格包含數據,而不是最後一行至少包含一個數據項。即A1:Q7包含數據,並且由於所有行都包含數據,我的代碼將打印區域設置爲A1:Q7,儘管C14中有數據。我希望我的打印區域是A1:Q14。我將如何去做這件事。下面的代碼。如何找到包含數據vba的單元格的最後一行以設置打印區域?

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 

末次

什麼會有所幫助。謝謝!

+0

FYI你只用'LastFundRow',而不是'ALastFundRow'在'設置rngSheet'。另外,'wksSource'和'ws'有什麼區別?最後,你可以使用列C作爲'ALastFundRow'中的列,否?或者,對於大多數數據,它會不會總是C? – BruceWayne

+0

哦哎呀。沒有弄亂功能,所以我沒有注意到。我很新,所以感謝提示,我試過但它沒有做任何事情:( –

+0

列C有空隙嗎?試試'ALastFundRow = wksSource.Range(「C」&Rows.Count).End (xlUp).Row' – BruceWayne

回答

0

功能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 

UR

0
With ActiveSheet 'or whatever worksheet 
LastRow = .Cells.Find(what:="*", after:=.Cells(1, 1), _ 
        LookIn:=xlValues, searchorder:=xlByRows, _ 
        searchdirection:=xlPrevious).Row 
End With 

可以使用SI最後一列的milar算法。

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

注意,我們正在返回一個空字符串公式尋找xlValues所以細胞將不被包括在內。

如果工作表爲空,則代碼將產生錯誤;所以如果這可能是一種可能性,你應該測試一下。

0

試試看看這個代碼。

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

我明白了!謝謝大家! –

相關問題