2017-09-15 82 views
1

下面的代碼是去一張桌子,並過濾哪個動物進來。動物的選擇是狗,貓和倉鼠。然後它會抽取日期和時間並將其粘貼到另一張紙上。如何選擇過濾表的第一個和最後一個單元格?

我的問題與做狗的隨機數可以顯示任何一天,讓我們說37顯示出來。

我可以得到的數據,以正確拉,但後來當我去把它開始於行38貓和我的代碼保持從排拉動2.

我怎樣才能讓我的代碼開始在無論選擇哪種動物並選擇過濾表的第一個和最後一個單元格?

Sub VetDate() 
str = "" 
str2 = "" 
With Sheet1 
    .AutoFilterMode = False 
    With .Range("A1:N1") 
    .AutoFilter 
    .AutoFilter Field:=9, Criteria1:="dog" 
    str = .Range("A2").Value 'How to fix this part 
    str2 = .Range("A1").End(xlDown).Value 'How to fix this part 
    End With 
    End With 
    With Sheet2 
    .Range("D36:D37").ClearContents 
    .Range("D36").Value = Format(str, "mmm-dd-yy hh:mm am/pm") 
    .Range("D37").Value = Format(str2, "mmm-dd-yy hh:mm am/pm") 
    .Range("D36:D37").HorizontalAlignment = xlCenter 
End With 
End Sub 

回答

2

請試試這個:

Sub VetDate() 

Dim StrRow As Long 
Dim Str2Row As Long 

str = "" 
str2 = "" 
With Sheet1 
    .AutoFilterMode = False 
    With .Range("A1:N1") 
    .AutoFilter 
    .AutoFilter Field:=9, Criteria1:="dog" 
    StrRow = Activesheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 2).Row 
    Str2Row = Range("A" & Rows.Count).End(xlUp).Row 
    str = .Range("A" & Str2Row).Value 'How to fix this part 
    str2 = .Range("A" & StrRow).Value 'How to fix this part 
    End With 
    End With 
    With Sheet2 
    .Range("D36:D37").ClearContents 
    .Range("D36").Value = Format(str, "mmm-dd-yy hh:mm am/pm") 
    .Range("D37").Value = Format(str2, "mmm-dd-yy hh:mm am/pm") 
    .Range("D36:D37").HorizontalAlignment = xlCenter 
End With 
End Sub 

希望這有助於

+1

這確實有很大的幫助。我不得不做出一些改變。 'StrRow =表( 「工作表Sheet」)。AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1,2).Row'和'STR2 = .Range( 「A1」)。完(xlDown)。 Value' – DaBeau96

相關問題