2013-05-03 224 views
0

我需要複製粘貼大單,這需要很多時間。 我建議不要在工作表上使用.copy過程,而是逐個單元地進行。爲新細胞提供古細胞的各種特性。這是我在這裏做的:Saving only some sheets in another WorkbookExcel/VBA最後一行/列

要做到這一點細胞我需要知道最後一個單元格包含信息。 (不僅價值,而且顏色,邊界等等)。我在互聯網上看到了很多簡單的解決方案,但它們都有問題。

ActiveSheet.UsedRange.Rows.Count往往給人太多的價值觀......我有一個810 * 16000的答案爲5 * 18表

範圍( 「A」 & activesheet.rows.count).END (xlup).row僅適用於第一列...

什麼是最好的方式來最終確定真正的最後一行的價值? containig信息(值,顏色,邊框...)

+2

的http://計算器。com/questions/11169445/error-finding-last-used-cell-in-vba – 2013-05-03 13:13:04

+0

它只適用於一列,我感覺列上的循環會不雅觀。 – 2013-07-11 15:14:49

+0

你是什麼意思?上面的代碼會給你最後一行有數據。或者我誤解了你的問題? – 2013-07-11 16:50:03

回答

2

此命令在Excel 2010中ActiveCell.SpecialCells(xlLastCell).Select將光標(活動單元格)移動到最後一個有一個不平凡的價值,即使當前單元格是空白

該命令Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select將選擇當前到最後一個具有非平凡值的所有單元格。

1

這兩種方法都適用於獲取最後一個單元格2007.我也在Excel 2003中使用了「UsedRange」方法。

如果他們不適合你,那麼你的電子表格可能會有一些Excel沒有顯示給你的東西。這發生在我以前。修復的方法是選擇真實數據下方的每一行,然後右鍵單擊 - >刪除它們(與右邊的列相同)。

快捷方式來刪除行:Shift +空格鍵,Shift +控制+ DownArrow中文,Rightclick->刪除

快捷方式刪除列:控制+空格,Shift +控制+ RIGHTARROW,Rightclick->刪除

例子:

set lastCell = ActiveCell.SpecialCells(xlLastCell) 

Set lastCell = worksheetObj.UsedRange.Item(worksheetObj.UsedRange.Cells.Count) 
+0

感謝您的答案,有什麼辦法只選擇具有可見屬性的單元格嗎?例如,我不在乎一個單元格是粗體還是沒有價值 – 2013-07-11 15:17:47

0

保存下面的代碼到類文件名FinalRowLocator

Public Property Get FinalRow(Optional ByVal Col As String, Optional ByVal Min As Boolean) As Long 
    FinalRow = pFinalRow(Col, Min) 
End Property 
Public Property Get Verify(Optional ByVal Col As String, Optional ByVal Min As Boolean) As Long 
    Verify = pVerify(Col, Min) 
End Property 
Private Function pVerify(Optional ByVal Col As String, Optional ByVal Min As Boolean) As Long 
    Dim i As Long 
    Dim j As Long 
    Dim rVerify As Long 
    Dim Votes(1 To 5) As Byte 
    Dim Congress(1 To 5) As Long 
    Dim FRL As New FinalRowLocator 
     Congress(1) = FRL.Columbus 
     Congress(2) = FRL.GosEgg 
     Congress(3) = FRL.OldTimer 
     Congress(4) = FRL.RainMan 
     Congress(5) = FRL.Slacker 
    For i = 1 To 5 
     For j = 1 To 5 
      If Congress(i) = Congress(j) Then Votes(i) = Votes(i) + 1 
     Next j 
    Next i 
    For i = 1 To 5 
     If rVerify < Congress(i) Then rVerify = i 
    Next i 
    pVerify = Congress(rVerify) 
End Function 
Public Property Get GosEgg(Optional ByVal Col As String) As Long 
    GosEgg = pFinalRow_M1(Col) 
End Property 
Public Property Get RainMan(Optional ByVal Col As String) As Long 
    RainMan = pFinalRow_M2(Col) 
End Property 
'Public Property Get MathIt() As Long 
' MathIt = pFinalRow_M3 
'End Property 
Public Property Get OldTimer() As Long 
    OldTimer = pFinalRow_M4 
End Property 
Public Property Get Columbus() As Long 
    Columbus = pFinalRow_M5 
End Property 
Public Property Get Slacker(Optional ByVal Col As Long) As Long 
    Slacker = pFinalRow_M6(Col) 
End Property 
Private Function pFinalRow(Optional ByVal Col As String, Optional ByVal Min As Boolean) As Long 
    Dim FinalRow As Long 
     Select Case Col 
      Case Is = "" 
       Select Case Min 
        Case False 
         If pFinalRow_M1 > pFinalRow_M2 Then FinalRow = pFinalRow_M1 
         If pFinalRow_M1 < pFinalRow_M2 Then FinalRow = pFinalRow_M2 
         'If pFinalRow_M3 > FinalRow Then FinalRow = pFinalRow_M3 
         If pFinalRow_M5 > FinalRow Then FinalRow = pFinalRow_M5 
         If pFinalRow_M6 > FinalRow Then FinalRow = pFinalRow_M6 
        Case True 
         If pFinalRow_M1 < pFinalRow_M2 Then FinalRow = pFinalRow_M1 
         If pFinalRow_M1 > pFinalRow_M2 Then FinalRow = pFinalRow_M2 
         'If pFinalRow_M3 < FinalRow Then FinalRow = pFinalRow_M3 
         If pFinalRow_M5 < FinalRow Then FinalRow = pFinalRow_M5 
         If pFinalRow_M6 < FinalRow Then FinalRow = pFinalRow_M6 
       End Select 
      Case Is <> 0 
        Select Case Min 
         Case False 
          If pFinalRow_M1(Col) > FinalRow Then FinalRow = pFinalRow_M1(Col) 
          If pFinalRow_M2(Col) > FinalRow Then FinalRow = pFinalRow_M2(Col) 
         Case True 
          If pFinalRow_M1(Col) < FinalRow Then FinalRow = pFinalRow_M1(Col) 
          If pFinalRow_M2(Col) < FinalRow Then FinalRow = pFinalRow_M2(Col) 
        End Select 
     End Select 

     'If pFinalRow_M4 > FinalRow Then FinalRow = pFinalRow_M4 'Disabled, lags behind. 

      pFinalRow = FinalRow 
End Function 

Private Function pFinalRow_M1(Optional ByRef ColLtr As String) As Long 
    If ColLtr = "" Then ColLtr = "A" 
    pFinalRow_M1 = Range(ColLtr & "65536").End(xlUp).Row 
End Function 

Private Function pFinalRow_M2(Optional ByRef Col As String) As Long 
    Dim i As Byte 
    Dim FinalRow As Long 
    Select Case Col 
     Case Is = "" 
      For i = 1 To 26 
       If FinalRow < Cells(ActiveSheet.Rows.Count, i).End(xlUp).Row Then FinalRow = Cells(ActiveSheet.Rows.Count, i).End(xlUp).Row 
      Next i 
     Case Is <> "" 
      FinalRow = Cells(ActiveSheet.Rows.Count, Col).End(xlUp).Row 
    End Select 
     pFinalRow_M2 = FinalRow 
End Function 

Private Function pFinalRow_M3() As Long 
    Dim FinalRow As Long 
    Dim ASUC As Long 
     ASUC = ActiveSheet.UsedRange.Count 
    FinalRow = ASUC/pFinalRow_M2 
    pFinalRow_M3 = FinalRow 
End Function 

Private Function pFinalRow_M4() As Long 
    'Works on unmodified (saved) sheet only. 
    Selection.SpecialCells(xlCellTypeLastCell).Select 
    pFinalRow_M4 = ActiveCell.Row 
End Function 

Private Function pFinalRow_M5() As Long 
On Error GoTo ErrorHandler 
    'May have problems with hidden rows 
    'This Method returns 0 on a sheet with no data while the others return 1 
    pFinalRow_M5 = Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row 
Exit Function 
ErrorHandler: 
    Select Case Err.Number 
     Case 91 
      'Assume Error is due to no data, return 0 
      pFinalRow_M5 = 0 
      Resume Next 
     Case Else 
      On Error GoTo 0 
    End Select 
End Function 

Private Function pFinalRow_M6(Optional ByRef ColLtr As Long) As Long 
    If ColLtr <= 0 Then ColLtr = 1 
    pFinalRow_M6 = Sheets(ActiveSheet.Name).Cells(Rows.Count, ColLtr).End(xlUp).Row 
End Function 
Public Function Diagnostics_Run() 
    Dim FRL As New FinalRowLocator 
     MsgBox "Columbus: " & FRL.Columbus & Chr(13) _ 
     & "FinalRow: " & FRL.FinalRow & Chr(13) _ 
     & "GosEgg: " & FRL.GosEgg & Chr(13) _ 
     & "OldTimer: " & FRL.OldTimer & Chr(13) _ 
     & "RainMan: " & FRL.RainMan & Chr(13) _ 
     & "Slacker: " & FRL.Slacker '& _ 
     ' _ & "MathIt: " & FRL.MathIt & Chr(13) 
End Function 

Public Property Get DoubleCheck(ByVal Result1 As Long, ByVal Result2 As Long) As Boolean 
    If Result1 <> Result2 Then DoubleCheck = False 
    If Result1 = Result2 Then DoubleCheck = True 
End Property 
Private Property Get pPara() 
    Dim FRL As New FinalRowLocator 
     pPara = FRL.FinalRow(, Not FRL.DoubleCheck(FRL.FinalRow, FRL.Verify)) 
End Property 
Public Property Get Para() 
    Para = pPara 
End Property 

則:

Dim FLR as new FinalRowLocator 
Dim FinalRow as Long 
    FinalRow = FRL.FinalRow 

只要你已經確定了正確的finalrow,下面應該工作。

'This will return the column letter 
'This Function is dependant on FinalRow returning the correct value. 
Dim rInt As Long 
    rInt = ActiveSheet.UsedRange.Count 
    psFinalCol = Chr((rInt/FinalRow) + 64) 
'This will return the column number 
'This Function is dependant on FinalRow returning the correct value. 
Dim rInt As Long 
    rInt = ActiveSheet.UsedRange.Count 
    piFinalCol = rInt/FinalRow 

附加使用信息:

Dim FRL as New FinalRowLocator 'Create an instance of the FinalRowLocator Class 
Dim FinalRow as Long 'Declare the FinalRow Variable as Long 

FinalRow = FRL.FinalRow 'Gets the Highest Number returned from all methods 
FinalRow = FRL.FinalRow("", true) 'Returns the lowest number from all methods 
FinalRow = FLR.FinalRow("A") 'Gets the highest number (column A) returned from methods 1 & 2 
FinalRow = FRL.FinalRow("A", true) 'Gets the lowest number (column A) returned from methods 1 & 2 
'FRL.DoubleCheck(FRL.FinalRow, FRL.Verify) 'returns true or false based on if the values match 
FinalRow.Para is the same as FRL.FinalRow(, Not FRL.DoubleCheck(FRL.FinalRow, FRL.Verify)) 'Returns the lowest row number if the highest one can not be verified. 
'FRL.Verify Determins the FinalRow in a Democratic Manner. 
FRL.Diagnostics 'will display the results of each individual method in a msgbox 
'***** Methods 
FRL.Columbus 'Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row 'May have problems with hidden rows 'This Method returns 0 on a sheet with no data while the others return 1 
FRL.GosEgg 'does not count past 65536 rows [Range(ColLtr & "65536").End(xlUp).Row] 
FRL.OldTimer 'Selection.SpecialCells(xlCellTypeLastCell).Select [Works on Unmodified Saved Sheet Only] 
FRL.RainMain 'Cells(ActiveSheet.Rows.Count, Col).End(xlUp).Row 
FRL.Slacker 'Sheets(ActiveSheet.Name).Cells(Rows.Count, ColLtr).End(xlUp).Row