计算列中的百分比

时间:2010-11-02 21:12:31

标签: excel vba worksheet-function

我有一个Excel工作表,其信息格式与测试类似:

Choice     Chosen        Percentage
A 
B 
C 
D 
TOTAL 
False 
True 
TOTAL 

选择列提供每个选项的选择次数,以及相应问题的总行总数。我需要一种方法来快速找到每个被选中的百分比(选择/总计),但我不确定如何去做。

2 个答案:

答案 0 :(得分:1)

我认为你不需要VBA。如果我理解这个问题,这只是一个简单的公式。假设这些是标题并从单元格A1开始,则列b的百分比(选项为A)将类似于= B2 / $ F2,并将列格式化为百分比类型。

请注意,$ F是对F的绝对引用,因此当您填充时,它总是指向行F(总行),但列号会按原样更改。

答案 1 :(得分:0)

我正在编写代码并在此窗口中编写代码,因此您需要注意错误。此外,您还需要输入伪代码的代码

dim currRow as integer
dim frs as boolean
firstRow = 1
frs = false
range("A1").select
<loop over column A until you reach a blank value>
  If selection.value == "Choice" then 
    ' move to next row, nothing to see here
  else if not selection.value == "Total" then
    if frs = false then 
      firstRow = selection.row
      frs = true
    endif
  else if selection.value = "Total" then
    frs = false
    dim i as integer
    for i = firstRow to selection.row -1
      range("C" + cstr(i)).formula = "=B"+ cstr(1)+"/B" + cstr (selection.row) ' or something to that effect
    next
  end if
  Selection.cells(2,1).select ' selects the next cell vertically
end loop

我正在使用frs表示'firstrow'已经设定

相关问题