2012-08-24 9 views
0

"Phonetic Name"이라는 첫 번째 열 머리글에 대해 테이블을 검색하는 VBA 서브 루틴을 만들려고합니다. 그런 다음 오른쪽 하단의 테이블에서 절대 마지막 셀을 찾고 셀을 마지막 셀 위의 한 행으로 셀 변수로 변수를 저장합니다. 서브 루틴은 첫 번째 셀 "Phonetic Name"과 "LastCell"변수 사이의 모든 셀을 선택합니다.Excel VBA에서 범위 선택

Dim LastCol As Integer 

TL = ActiveSheet.Range("A:A").Find("Phonetic Name", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True).Row 

Set LastRow = Cells.Find("*", [a1], , , xlByRows, xlPrevious) 

With ActiveSheet 
    LastCol = .Cells(TL, .Columns.Count).End(xlToLeft).Column 
End With 

Set LastCell = ActiveSheet.Cells(LastRow.Row - 1, LastCol) 
'I would like to do something like the following... 
ActiveSheet.Range("TL:LastCell").Select 
Selection.Copy 

어떻게하면이 논리를 VBA에 친숙한 방식으로 다시 쓸 수 있습니까?

+0

그 코드에 무슨 문제가 있습니까? – ApplePie

+1

'ActiveSheet.Range (TL, LastCell) .Copy'는 당신이 찾고있는 것입니다 (그러나'.Find()'줄의 끝에서'.Row'를 제거합니다) –

답변

2
Dim LastCol As Integer 
Dim TL as Range 

Set TL = ActiveSheet.Range("A:A").Find("Phonetic Name", _ 
       LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True) 

If Not TL Is Nothing Then 

    Set LastRow = Cells.Find("*", [a1], , , xlByRows, xlPrevious) 
    With ActiveSheet  
     LastCol = .Cells(TL.Row, .Columns.Count).End(xlToLeft).Column 
    End With 
    Set LastCell = ActiveSheet.Cells(LastRow.Row - 1, LastCol) 
    ActiveSheet.Range(TL,LastCell).Copy 

End If 
+0

고마워,하지만 코드와 유형이 일치하지 않습니다. – AME

+0

TL 선언을 '범위'로 변경 했습니까? 그걸 보여주기 위해 내 게시물을 편집했습니다 ... –

+0

예, 했어요. 왜 아직도 같은 오류가 발생하는지 확신 할 수 없습니다. – AME