简体   繁体   English

Excel VBA范围计算

[英]Excel VBA Range Calculation

I'm working on creating a monthly consolidation function which doesn't like how I'm using this particular snippet of my code: 我正在创建一个月度整合功能,它不喜欢我如何使用我的代码的这个特定片段:

For Each ws In Worksheets

 If Len(ws.Name) <= 5 Then
    ws.Range("B7:C7").Select
    Selection.ClearContents
    Selection.AutoFill Destination:=Range("B7:C100"), Type:=xlFillDefault
    Range("B7:C100").Select

 End If

 Next

All client profiles are filled out via a template which self duplicates for each new individual so the profiles are all standardised. 所有客户端配置文件都通过模板填写,该模板为每个新个人自行复制,因此配置文件都是标准化的。 All user profiles are <=5 characters (acronyms). 所有用户配置文件都<= 5个字符(首字母缩写词)。 So the intent of my code is to check if the tab is <=5 characters, and then if it is - select the first range B7:C7 and clear this. 所以我的代码的意图是检查标签是否<= 5个字符,然后如果是 - 选择第一个范围B7:C7并清除它。 Next we apply this cleared filter to all the other cells as well (down to 100). 接下来,我们将此清除的过滤器应用于所有其他单元格(低至100)。

I've defined ws as my Worksheet variable and I don't understand why I can't set the ws.range as B7:C7. 我已经将ws定义为我的工作表变量,我不明白为什么我不能将ws.range设置为B7:C7。 I'd appreciate your insight! 我很感激你的见解!

I've attached a snapshot of what the form looks like. 我附上了表格的快照。 工具快照

For Each ws In Worksheets
    If Len(ws.Name) <= 5 Then ws.Range("B7:C100").ClearContents
Next

Notes: 笔记:

  • the .Select statement requires ws to be activated .Select语句要求激活ws
  • you don't need to use .Select in this case (you can act on the range directly) 你不需要使用。在这种情况下选择(你可以直接采取行动)
  • the .AutoFill is not bad but here is not needed .AutoFill不错,但这里不需要

    • its usefulness is when incrementing initial data 它的用处是递增初始数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM