2013-09-03 2 views
0

안녕하세요 다시 stackoverflow. 내 질문은 오늘 몇 가지 상수로 다른 시트에있는 셀에서 값을 반환하는 것입니다. 나는 일련의 컬럼을 가지고 있으며,이 컬럼은 변경 불가능한 입력을 받아 몇 가지 수식과 매크로를 사용하여 유용한 데이터로 변환합니다. 최종 결과는 다음과 유사합니다. 내가하고 싶은 무엇위치를 매일 변경하는 다른 통합 문서에서 값을 반환하는 방법

(A)        
Cage 
10 
ct 
Cage 
5 
ct 
Bin 
3 
CT 
Bin 
4 
CT 
CT 
Bin 
11 
CT 

은 A1에서 "빈"을 찾을 수 있습니다 : A16 다음 시트 2의 셀에 그 아래에 직접 값을 반환합니다. 그런 다음, 얼마나 많은 "ct 's"가 "Bin"아래에 있는지 계산해야하며, 출력을 위해 sheet2의 합계에 인접한 셀에 값을 반환해야합니다.

Bin1 3 1 Bin2 4 2 Bin3 11 1 

정보의 위치는 매일 바뀌지 만 동일한 행에 머물러 있습니다. 순서는 항상 일정합니다. 예를 들어 3은 첫 번째 빈과 일치하고, 4는 두 번째, 11은 세 번째와 일치합니다. 매일 더 많거나 적을 수도 있습니다.

제공 할만한 도움을 주셔서 감사합니다.

+0

순서가 일정하므로 가장 쉬운 해결책은 1D 배열을 다른 시트의 적절한 테이블로 변환하는 VBA 루틴을 만드는 것입니다. 그 일을 시작하는 방법을 알고 있습니까? – Arconath

+0

불행히도, 아니오. 나는 Excel에 평범하지 않으며 VBA에 대한 나의 경험은 거의 존재하지 않는다. 나는 보통 내가 원하는 것을하기 위해 코드를 편집 할 수는 있지만 그것을 직접 작성하기 위해 나는 길을 잃는다. – user2727453

답변

0

"Bin"에서 요청한 값은 항상 존재하며 각 A1, A2, A3 ...에 Bin XY를 쓰고 싶다고 가정하고 있습니다. 이미 (그래서 이것은 기본적으로 당신을 위해 새로운 열립니다) 열지 않은 : 당신의 정보가 매일 매일 변경 때문에

Sub Bin_count() 

Dim i As Byte, j As Byte, CTs As Byte 
Dim ThisPage As Worksheet, TargetPage As Worksheet 
Dim NewBook As Workbook 

Set ThisPage = ThisWorkbook.ActiveSheet 
Set NewBook = Workbooks.Add ' or you can address an existing file with workbooks.open(filename) 
Set TargetPage = NewBook.Worksheets(1) ' you can also decide the sheet where you want to write your values (here by default is the first excel's "tab") 

j = 1 ' Results cells scanner 

For i = 1 To 17 

    If ThisPage.Cells(i, 1).Text = "Bin" Then          ' if "Bin is found... 

     ThisPage.Activate 
     Cells(i + 1, 1).Activate 

     Do Until IsEmpty(ActiveCell)            ' ...scan all cells below... 
      If ActiveCell.Text = "CT" Then CTs = CTs + 1       '...to find all CTs and store their number in a variable... 
      ActiveCell.Offset(1, 0).Activate 
     Loop 

     TargetPage.Cells(1, j) = "Bin " & ThisPage.Cells(i + 1, 1) & " " & CTs '... at the end and write Bin + number under it + number of CTs found 
     j = j + 1 ' should you need to write in column instead of row, simply write targetpage.cells(j,1) instead of the previous statement 

    End If 

Next i 

End Sub 
+0

당연히 당신은 "활성화"하지 않고 그것을 할 수 있지만 제 생각에는 당신이 미경험이기 때문에 디버깅이 더 쉬울 수도 있습니다 ... 다만을 위해서) – Noldor130884

+0

이 Noldor에 감사드립니다. 나는 당신이 더 많은 정보를 원한다면 아래의 응답에 대한 내 의견을보고, 나는 문제가있는 스프레드 시트를 연결했다. 다시 한 번 감사드립니다 – user2727453

+0

흠 ... 나는 어떤 링크도 찾지 못했습니다 ... 제가 제안한 절차에 오류가 있다면 알려주실 수 있습니까? – Noldor130884

0

당신은 Range.Find 방법으로 Do...Loop를 사용하여 시도 할 수 있습니다. 당신이 사용하는 경우

Dim bin_count As Integer 'bin count 
bin_count = 1 
Dim sheet2_plcmnt As Integer 'sheet 2 placeholder 
sheet2_plcmnt = 1 

Dim cur_row As Long 'current row 
Dim nxt_row As Long 'next row 
Dim loop_tst As Integer 'loop test 

'---prime do...loop--- 
cur_row = Worksheets("Sheet1").Range("A:A").Find("Bin").Row 
nxt_row = Worksheets("Sheet1").Range("A:A") _ 
    .FindNext(Cells(cur_row, 1)).Row 
loop_tst = 0 
'delete previous data in Sheet2, row 1 
Worksheets("Sheet2").Rows(1).Delete 
'use .activate so CountIf works properly in loop below 
Worksheets("Sheet1").Activate 

'---run loop through all cells in column A--- 
'= 0 have not reached end; = 1 reached end but need to run 
'one more time; = 2 exit loop 
Do While loop_tst < 2 
    'put in values... 
    'bin # (contiguous, starting at 1) 
    Worksheets("Sheet2").Cells(1, sheet2_plcmnt).Value = _ 
    "Bin" & bin_count 
    '# after bin 
    Worksheets("Sheet2").Cells(1, sheet2_plcmnt + 1).Value = _ 
    Worksheets("Sheet1").Cells(cur_row + 1, 1).Value 
    '# of CTs after bin 
    If loop_tst < 1 Then 
    'if haven't reached end, check between cur_row and nxt_row 
    Worksheets("Sheet2").Cells(1, sheet2_plcmnt + 2).Value = _ 
     WorksheetFunction.CountIf(Range(Cells(cur_row, 1), _ 
     Cells(nxt_row, 1)), "CT") 
    Else 
    'if have reached end, check between cur_row and 
    'last row in column A 
    Worksheets("Sheet2").Cells(1, sheet2_plcmnt + 2).Value = _ 
     WorksheetFunction.CountIf(Range(Cells(cur_row, 1), _ 
     Range("A1").End(xlDown)), "CT") 
    End If 
    '...change counters as needed 
    bin_count = bin_count + 1 
    sheet2_plcmnt = sheet2_plcmnt + 3 
    cur_row = nxt_row 
    'set next row 
    nxt_row = Worksheets("Sheet1").Range("A:A") _ 
    .FindNext(Cells(cur_row, 1)).Row 
    'adjust to determine loop behavior 
    If loop_tst = 1 Then loop_tst = 2 
    If cur_row > nxt_row Then loop_tst = 1 
Loop 

, 그것은 그래서 나머지는 이전 카운트에서 남아 있지 시트 2 행 1에서 이전 데이터가 삭제됩니다 유의하시기 바랍니다 : 정렬이 필요하고, 그 변화 데이터를 유연하게 유지되지 않습니다 예를 들어.
CountIf 기능이 올바르게 작동하는지 확인하기 위해 Worksheet.Activate 메서드를 한 번 사용합니다. 누군가이를 피할 수있는 방법을 알고 있다면 코드는 Select 또는 Activate없이 작동하도록 수정할 수 있습니다.

+0

사실 더 많은 것을 최적화하고 싶다면, sheet2_plcmnt는'(1/3 * bin_count) + (2/3)'로 대체 될 수 있습니다. –

관련 문제