excel - How to specify the sheet a worksheet function should use via a range object? -
i have code searches column of values in sheet(3) in format of string "value1 - value 2"
value2 first value in column in sheet(2) , value1 value in same column, in cell further down sheet.
the setup have is:
in sheet(1), cells c2:c6 have values a-e respectively
in sheet(2), cell c1 has value "yes" , cells c2:c6 have values 1-5 respectively
in sheet(3), cell a2 has value "4 - yes"
so code should countif column in sheet2 first value being yes , cells value 4, , put result in cell b2 on sheet(3)
what find yes column (column c) , search same column on sheet(1) (so message boxes show letters rather numbers).
is there way can more precisely specify sheet countif function uses?
i'm using excel 2000 on windows 7
private sub test_click() scenario_count = 6 dim integer = 1 sheets(2).select j = 2 24 if sheets(2).cells(1, j).value = right(sheets(3).cells(i + 1, 1).value, len(sheets(3).cells(i + 1, 1).value) - instrrev(sheets(3).cells(i + 1, 1).value, "-") - 1) msgbox ("number of scenarios " & scenario_count) msgbox ("value searching " & "'" & left(sheets(3).cells(i + 1, 1).value, instrrev(sheets(3).cells(i + 1, 1).value, "-") - 2) & "'") msgbox ("range searched " & range(cells(2, j), cells(scenario_count, j)).address & " in " & activesheet.name) msgbox ("number of occurrences " & sheets(2).application.worksheetfunction.countif(range(cells(2, j), cells(scenario_count, j)), left(sheets(3).cells(i + 1, 1).value, instrrev(sheets(3).cells(i + 1, 1).value, "-") - 2))) sheets(2).select sheets(3).cells(i + 1, 2).value = sheets(2).application.worksheetfunction.countif(range(cells(2, j), cells(scenario_count, j)), left(sheets(3).cells(i + 1, 1).value, instrrev(sheets(3).cells(i + 1, 1).value, "-") - 2)) each c in range(cells(2, j), cells(scenario_count, j)) msgbox ("comparing " & c.address & " " & c.value & " " & left(sheets(3).cells(i + 1, 1).value, instrrev(sheets(3).cells(i + 1, 1).value, "-") - 2)) next c goto endofif2 end if next endofif2: end sub
where have 'worksheetfunction.countif(range(cells(2, j)', insert sheet before range reference, so:
sheets(2).range(sheets(2).cells(2, j), sheets(2).cells(scenario_count, j))
edit full formula references sheet both cells , range functions blatently taken @rory's comment.
Comments
Post a Comment