2011-11-01 31 views
0

好吧,虐待尝试是最好的,我可以先解释一下这个...更复杂擅长平均环

我的程序创建一个动态的信息表,我试着用一个循环来简化一些信息。为了简单起见,我只会说表格从A1开始,然后转到(columnindex:rowindex * 6)(该部分已经完成)。

我想要做的是取每列的平均值,并将它们放在同一张Excel表格的其他位置。 ((columnindex)1:columnindex(rowindex * 6))的平均值(A1:A(rowindex * 6)),平均值(B1:B(rowindex * 6))等。

现在棘手的问题....

开始在A((rowIndex位置* 6)5)....因此5行以上在A列开始见下表....

去吧...

Average(A), Average(B) 
Average(C), Average(D) 
Average(E), Average(F) 

等等直到所有的列都是li STED ...

当我奋力的转换columnindex到相应的字母总是有34-40列

我知道,我会想要做的事,如:

i = 5 

for x = 1 to x = columnindex 

    dim Num2Let1 as string = a=1, b=2, c=3, so on.. 
    dim Num2Let2 as string = a=1, b=2, c=3, so on.. 

    xlWorkSheet2.Cells((rowindex*6)+i), 1) = "Average(" & Num2Let1.ToString & cstr(1) & ":" & Num2Let1.ToString & cstr(rowindex*6)) 
    xlWorkSheet2.Cells((rowindex*6)+i), 2) = "Average(" & Num2Let2.ToString & cstr(1) & ":" & Num2Let2.ToString & cstr(rowindex*6)) 

    i = i + 1 
    x = x + 2 

    loop 
next 

如果任何人都可以用我的方式提出一些建议,不管这是最有利于我的问题的方法,还是有更简单的解决方案,都将不胜感激。

感谢

回答

1

这里是给你基于其索引中的列名的函数:

Private Function GetExcelColumnName(columnNumber As Integer) As String 
    Dim col As Integer = columnNumber 
    Dim columnName As String = "" 
    Dim num As Integer 

    While col > 0 
     num = (col - 1) Mod 26 
     columnName = Convert.ToChar(65 + num).ToString() & columnName 
     col = CInt((col - num) \ 26) 
    End While 

    Return columnName 
End Function 

这应该帮助你创建你的平均函数字符串。

+0

感谢您的帮助! :) – Nefariis

+0

高兴地帮忙,自己需要几次......甚至在我的工具箱:) – Jay