Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2397|回复: 3

[已解决]如何让函数带上参数

[复制链接]
发表于 2014-6-27 11:09 | 显示全部楼层 |阅读模式
本帖最后由 linbohen 于 2014-7-3 13:35 编辑

请各位看看,如何让这个函数带上如图红色框里的参数,实现任意工作表里任意列的统计。谢谢。

最佳答案
2014-6-27 12:49
  1. Function tongji(sht1 As Worksheet, sht2 As Worksheet, col2 As Long)
  2.     Dim Sheet1 As Worksheet, Sheet2 As Worksheet
  3.     Dim mycol As Integer, cheng As Integer
  4.     tongji = " "
  5.     If col2 <= 0 Or col2 > Columns.Count Then Exit Function
  6.    
  7.     For mycol = 1 To 500
  8.         If Sheet1.Cells(mycol, col2).Value <> " " Then
  9.             cheng = sht1.Cells(mycol, col2).Value * sht1.Cells(mycol, 3).Value
  10.             tongji = tongji & " " & sht1.Cells(mycol, 1).Value & sht1.Cells(mycol, col2).Value & "*" & sht1.Cells(mycol, 3).Value & "=" & CStr(cheng)
  11.         End If
  12.     Next mycol
  13. End Function
复制代码
楼主自己测试吧。
01.jpg

样表.rar

9.27 KB, 下载次数: 4

发表于 2014-6-27 12:47 | 显示全部楼层
你的函数返回结果也不需要用?
那这统计有何用?
另外sheet2也没见有用到。

评分

参与人数 1 +1 收起 理由
linbohen + 1 返回结果要用,可不知道怎么返回,sheet2可.

查看全部评分

回复

使用道具 举报

发表于 2014-6-27 12:49 | 显示全部楼层    本楼为最佳答案   
  1. Function tongji(sht1 As Worksheet, sht2 As Worksheet, col2 As Long)
  2.     Dim Sheet1 As Worksheet, Sheet2 As Worksheet
  3.     Dim mycol As Integer, cheng As Integer
  4.     tongji = " "
  5.     If col2 <= 0 Or col2 > Columns.Count Then Exit Function
  6.    
  7.     For mycol = 1 To 500
  8.         If Sheet1.Cells(mycol, col2).Value <> " " Then
  9.             cheng = sht1.Cells(mycol, col2).Value * sht1.Cells(mycol, 3).Value
  10.             tongji = tongji & " " & sht1.Cells(mycol, 1).Value & sht1.Cells(mycol, col2).Value & "*" & sht1.Cells(mycol, 3).Value & "=" & CStr(cheng)
  11.         End If
  12.     Next mycol
  13. End Function
复制代码
楼主自己测试吧。

评分

参与人数 1 +1 收起 理由
linbohen + 1 老师,用什么样的编辑器呢?可以有行号

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2014-6-27 13:13 | 显示全部楼层
tongji(sheet1,2)
这样调用对么?
为什么显示#VALUE!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-26 20:25 , Processed in 0.370239 second(s), 18 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表