2012-11-14 3 views
1

"Sheet1 : Sheet5! A1 : D10"형식의 VBA 하위로 전달 된 문자열이 동일한 통합 문서 (Sheet2, Sheet3 및 Sheet4는 Sheet1과 Sheet5 사이에 있음)의 여러 워크 시트에있는 범위를 참조합니다. .여러 시트 범위 참조 문자열에서 개별 Excel 워크 시트 범위를 캡처하는 방법은 무엇입니까?

문제는이 참조를 범위 변수에 적용 할 수 없다는 것입니다.이 참조는 여러 참조로 인해 발생할 수 있으며 범위가 다른 시트에 있으므로 application.union을 사용할 수 없습니다.

이 문자열에서 Sheet1! A1 : D10, Sheet2! A1 : D10 등의 다섯 가지 개별 범위를 어떻게 추출 할 수 있습니까?

답변

3

이렇게하면 각각의 문자열 표현을 얻은 다음 배열에 저장합니다. 그것은 몇 가지 가정을합니다. (시트가 존재하고 첫 번째는 인덱스 순서에서 첫 번째이며 인덱스와 관련하여 모든 것을 원한다는 것을 나타냅니다.) 그러나 상황에 따라 다를 수 있습니다.

Sub Parse() 

    Dim wbk As Workbook 
    ' Arbitrary array size of 10 to store the ranges (as an example) 
    Dim myRanges(10) As Variant 

    Set wbk = ActiveWorkbook 

    ' Split the string at the ! 
    s = "Sheet1:Sheet3!A1:D10" 
    s = Split(s, "!") 

    ' Range is the second element of the split (A1:D10) 
    TargetRange = s(1) 

    ' The first contains the sheets, so split on the : 
    SheetNames = Split(s(0), ":") 

    ' These sheets are the lower/upper bounds, so use their indices in a loop 
    ' that cycles over the sheets in between them (inclusive) 
    j = 0 ' This is for the array - you may not need it 
    For i = wbk.Sheets(SheetNames(0)).Index To wbk.Sheets(SheetNames(1)).Index 
     ' Range is the concatenation of the sheet at this index and the target range 
     Set Rng = Range(wbk.Sheets(i).Name & "!" & TargetRange) 
     ' Drop it in to our array (or handle how you want) 
     myRanges(j) = Rng 
     j = j + 1 
    Next i 

End Sub 
+0

물론 감각, 정말 감사합니다! –

+0

@ManofLeng 아무런 문제가 없습니다. 도움이되기를 바랍니다! – RocketDonkey

+0

+1 멋지게 완료되었습니다. – brettdj

0

하위 구문 분석()

Dim wbk As Workbook 
' Arbitrary array size of 10 to store the ranges (as an example) 
Dim myRanges(10) As Variant 

Set wbk = ActiveWorkbook 

' Split the string at the ! 
s = "Sheet1:Sheet3!A1:D10" 
s = Split(s, "!") 

' Range is the second element of the split (A1:D10) 
TargetRange = s(1) 

' The first contains the sheets, so split on the : 
SheetNames = Split(s(0), ":") 

' These sheets are the lower/upper bounds, so use their indices in a loop 
' that cycles over the sheets in between them (inclusive) 
j = 0 ' This is for the array - you may not need it 
For i = wbk.Sheets(SheetNames(0)).Index To wbk.Sheets(SheetNames(1)).Index 
    ' Range is the concatenation of the sheet at this index and the target range 
    Set Rng = Range(wbk.Sheets(i).Name & "!" & TargetRange) 
    ' Drop it in to our array (or handle how you want) 
    myRanges(j) = Rng 
    j = j + 1 
Next i 

최종 하위

+0

약간의 설명 만하면됩니다. –

관련 문제