首页 \ 问答 \ 在VBA中使用列名作为范围(Using column names as range in VBA)

在VBA中使用列名作为范围(Using column names as range in VBA)

我有下面的代码,使用col范围作为值在工作表中搜索列。 相反,我需要使用col名(在该表中)作为搜索条件并执行相同的功能。 我如何实现这一点?

Sub WBR()
Dim Count1Criteria As Variant
Dim Count3Criteria As Variant
Dim test As Variant
Dim wf As WorksheetFunction
Set wf = Application.WorksheetFunction

Filter1InSummary = Array(Array("AE4", "Latency", "O:O", "Pass"), _
                     Array("AE51", "TT", "G:G", "Yes"), _
                     Array("AE52", "TT", "G:G", "No"), _
                     Array("AE61", "Reactive", "R:R", "Item"))

Filter3InSummary = Array(Array("AE43", "TT", "I:I", "<>Duplicate TT", _
                                         "G:G", "<>Not Tested", _
                                         "U:U", "Item"))
For Each test In Filter3InSummary
With Worksheets(test(1))
    Range(test(0)) = wf.CountIfs(.Range(test(2)), test(3), _
                                 .Range(test(4)), test(5), _
                                 .Range(test(6)), test(7))
End With
Next

I have the below code that searches a column in a sheet using the col range as value. Instead of that, I need to use the col name (in that sheet) as he search criteria and perform the same function. How do I implement this?

Sub WBR()
Dim Count1Criteria As Variant
Dim Count3Criteria As Variant
Dim test As Variant
Dim wf As WorksheetFunction
Set wf = Application.WorksheetFunction

Filter1InSummary = Array(Array("AE4", "Latency", "O:O", "Pass"), _
                     Array("AE51", "TT", "G:G", "Yes"), _
                     Array("AE52", "TT", "G:G", "No"), _
                     Array("AE61", "Reactive", "R:R", "Item"))

Filter3InSummary = Array(Array("AE43", "TT", "I:I", "<>Duplicate TT", _
                                         "G:G", "<>Not Tested", _
                                         "U:U", "Item"))
For Each test In Filter3InSummary
With Worksheets(test(1))
    Range(test(0)) = wf.CountIfs(.Range(test(2)), test(3), _
                                 .Range(test(4)), test(5), _
                                 .Range(test(6)), test(7))
End With
Next

原文:https://stackoverflow.com/questions/41581155
更新时间:2024-05-02 07:05

最满意答案

你能用COALESCE吗?

例如:

SELECT COALESCE(COUNT(*),0) AS orders
FROM `orders`
GROUP BY DATE(`order_datetime`)
ORDER BY DATE(`order_time`) ASC

它返回列表中的第一个非空值 - 如果它不为空则计数,如果不是则为0。

http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce

挂起..是没有这些日期的行的问题,所以你只是得到那些日期的结果?

如果是这样,您也可以返回订单日期,并且对于如何显示结果更加聪明:

SELECT  order_datetime,COUNT(*) AS orders
FROM `orders`
GROUP BY DATE(`order_datetime`)
ORDER BY DATE(`order_time`) ASC

然后在输出结果时可能会创建一个循环,遍历您感兴趣的日期,如果该日期没有行,则打印0:

for (datetime  d = startdate; d <= enddate; datetime.adddays(1)){
    if (recordset[order_datetime] == d){
        output (d,recordset[orders]);
        recordset.movenext();
    }else
    {
        output (d,0);
    }
}

(注意我不知道会是什么编程语言;-)

如果你想在数据库中完成所有操作,那么请查看这个SO问题,了解如何动态创建日期表,然后你就可以将它的外部联接留给你查询日期了。

获取两个日期之间的日期列表


can you use COALESCE?

eg:

SELECT COALESCE(COUNT(*),0) AS orders
FROM `orders`
GROUP BY DATE(`order_datetime`)
ORDER BY DATE(`order_time`) ASC

it returns first non null value in the list - count if its not null and 0 if it is.

http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce

hang on.. is the issue that there are no rows with those dates so you just get results for those dates?

if so you could return the order date too and be a bit more clever about how you display your result:

SELECT  order_datetime,COUNT(*) AS orders
FROM `orders`
GROUP BY DATE(`order_datetime`)
ORDER BY DATE(`order_time`) ASC

then when outputting your results maybe create a loop that iterates through the dates you are interested in and prints a 0 if there is no row for that date:

for (datetime  d = startdate; d <= enddate; datetime.adddays(1)){
    if (recordset[order_datetime] == d){
        output (d,recordset[orders]);
        recordset.movenext();
    }else
    {
        output (d,0);
    }
}

(note i have no idea what programming language that would be ;-)

if you want to do it all in the DB then look at this SO question for how to create a table of the dates on the fly and then you can left outer join from it to you query on date..

Get a list of dates between two dates

相关问答

更多
  • 处理此问题的一种概念上简单的方法是将您的原始查询(略微修改)与UNION一起用于获取group_id为null的所有记录: SELECT * FROM objects WHERE group_id IS NOT NULL GROUP BY group_id UNION SELECT * FROM objects WHERE group_id IS NULL SQLFiddle 顺便说一下,我认为在GROUP BY查询中SELECT列是不好的做法,它们不是聚合或者正在进行分组的列。 因此,最好避免使用GRO ...
  • 下面的查询使用派生表(别名inr )将YearMonth / app_id组合在一起。 然后在left join使用它来获取数据,而不 applestore_ranking 是否存在于表applestore_ranking 。 如果您希望零出现而不是NULL,请使用ifnull() 。 ifnull(AVG(r.rank),0) as Rank变成ifnull(AVG(r.rank),0) as Rank 。 请注意,它可以帮助您简单地将这个分段放在一起,然后单独突出显示inr select的代码并查看其简 ...
  • 你能用COALESCE吗? 例如: SELECT COALESCE(COUNT(*),0) AS orders FROM `orders` GROUP BY DATE(`order_datetime`) ORDER BY DATE(`order_time`) ASC 它返回列表中的第一个非空值 - 如果它不为空则计数,如果不是则为0。 http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce 挂起. ...
  • 如果表中有唯一的列(或列集),则只需向GROUP BY添加另一个表达式即可。 当collection_id为null时,表达式需要为每一行返回唯一值,否则返回一个常量。 假设表中有一个唯一的id列,那么这样的话: ... GROUP BY collection_id, IF(collection_id IS NULL, id, 0) 当collection_id不为null时,GROUP BY中的第二个表达式返回一个常量值,但当collection_id为null时,它会为每一行返回一个唯一值。 (注意, ...
  • 这是正常工作的查询(不剥离NULL ): SELECT `table1`.* GROUP_CONCAT(COALESCE(`table3`.`id`, '') SEPARATOR ';') AS `t3_id`, GROUP_CONCAT(COALESCE(`table3`.`note`, '') SEPARATOR ';') AS `t3_note`, FROM `table1` LEFT JOIN `table3` ON `table3`.`id` = `table1`.`id` ...
  • 它通过一个常量值对整个查询进行分组,这使得它可以随机有效地选择一行来返回。 使用LIMIT 1可以获得相同的结果。 请注意,此习惯用法不是标准SQL,如果在其他数据库中尝试,可能会导致错误。 在MySQL中,您可以通过指定ONLY FULL GROUP BY SQL模式来禁用此行为。 有关详细信息,请参阅文档 。 OTOH, LIMIT 1也是非标准的,但是得到了更广泛的支持,并且在符合SQL:2008和FETCH ... ONLY子句的数据库中可以实现相同的效果。 有关详细信息,请参阅此处。 It's g ...
  • 你的time_milis都是秒,但是hour需要像'10:05:03'这样的time ,你可以使用sec_to_time 。 喜欢这个: SELECT concat(hour(sec_to_time(time_milis)), ' ', sec_to_time(time_milis)) FROM ( SELECT SUM(TIMEDIFF(logout_timestamp,login_timestamp)) AS time_milis FROM t_access_log WHERE logout_tim ...
  • 不要使用select * with group by 。 它是一种反模式,在大多数情况下不受其他数据库的支持。 接受挑战。 弄清楚如何组合结果。 在这种情况下,简单的聚合函数有效: SELECT DATE_FORMAT(FROM_UNIXTIME(`time`), '%Y %m %d %H') as yyyymmddhh, AVG(temperature) as temperature, AVG(pressure) as pressure, AVG(humidity ...
  • 你能用UNION试试吗? (SELECT SQL_CALC_FOUND_ROWS * FROM calls WHERE parent_id = '$term' where job_num != 0 GROUP BY (job_num) ORDER BY date_start DESC LIMIT $page_position, $item_per_page) UNION (SELECT SQL_CALC_FOUND_ROWS * FROM calls WHERE parent_id = '$term' ...

相关文章

更多

最新问答

更多
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的
  • SimplePie问题(SimplePie Problem)
  • 在不同的任务中,我们可以同时使用多少“上下文”?(How many 'context' we can use at a time simultaneously in different tasks?)
  • HTML / Javascript:从子目录启用文件夹访问(HTML/Javascript: Enabling folder access from a subdirectory)
  • 为什么我会收到链接错误?(Why do I get a linker error?)
  • 如何正确定义析构函数(How to properly define destructor)
  • 垂直切换菜单打开第3级父级。(Vertical toggle menu 3rd level parent stay opened. jQuery)
  • 类型不匹配 - JavaScript(Type mismatch - JavaScript)
  • 为什么当我将模型传递给我的.Net MVC 4控制器操作时,它坚持在部分更新中使用它?(Why is it that when I pass a Model to my .Net MVC 4 Controller Action it insists on using it in the Partial Update?)
  • 在使用熊猫和statsmodels时拉取变量名称(Pulling variable names when using pandas and statsmodels)
  • 如何开启mysql计划事件
  • 检查数组的总和是否大于最大数,反之亦然javascript(checking if sum of array is greater than max number and vice versa javascript)
  • 使用OpenGL ES绘制轮廓(Drawing Outline with OpenGL ES)
  • java日历格式(java Calendar format)
  • Python PANDAS:将pandas / numpy转换为dask数据框/数组(Python PANDAS: Converting from pandas/numpy to dask dataframe/array)
  • 如何搜索附加在elasticsearch索引中的文档的内容(How to search a content of a document attached in elasticsearch index)
  • LinQ to Entities:做相反的查询(LinQ to Entities: Doing the opposite query)
  • 从ExtJs 4.1商店中删除记录时会触发哪些事件(Which events get fired when a record is removed from ExtJs 4.1 store)
  • 运行javascript后如何截取网页截图[关闭](How to take screenshot of a webpage after running javascript [closed])
  • 如何使用GlassFish打印完整的堆栈跟踪?(How can I print the full stack trace with GlassFish?)
  • 如何获取某个exe应用程序的出站HTTP请求?(how to get the outbound HTTP request of a certain exe application?)
  • 嗨,Android重叠背景片段和膨胀异常(Hi, Android overlapping background fragment and inflate exception)
  • Assimp详细说明typedef(Assimp elaborated type refers to typedef)
  • 初始化继承类中不同对象的列表(initialize list of different objects in inherited class)
  • 使用jquery ajax在gridview行中保存星级评分(Save star rating in a gridview row using jquery ajax)
  • Geoxml3 groundOverlay zIndex(Geoxml3 groundOverlay zIndex)