[英]Capitalize Dynamic Range in VBA
(Coding rookie posting first ever question so please pardon my mistakes) (编码菜鸟发布第一个问题所以请原谅我的错误)
I'm trying to learn simple methods of data validation. 我正在尝试学习简单的数据验证方法。 I read another post similar to what I'm doing: convert-entire-range-to-uppercase, but it doesn't work when I change the range to fit my needs.
我读了另一篇类似于我正在做的帖子: convert-whole-range-to-uppercase,但是当我改变范围以满足我的需要时它不起作用。 Couldn't find anything else that addressed this.
无法找到解决此问题的任何其他内容。
I have an Excel column named "Block" that appears in different locations in different workbooks, and I need to capitalize any letters that occur in that column. 我有一个名为“Block”的Excel列出现在不同工作簿的不同位置,我需要将该列中出现的任何字母大写。 I think the code works as intended until the final line, which results in "#NAME?"
我认为代码按预期工作,直到最后一行,结果是“#NAME?” filling the whole range.
填补整个范围。
This is what I have so far: 这是我到目前为止:
Dim LastColumn As Long
Dim LastRow As Long
Dim BlockColumn As Long
Dim BlockRange As Range
'defines LastColumn, LastRow & BlockColumn
LastColumn = Cells.Find(What:="*", After:=Range("a1"), LookAt:=xlPart, LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column
LastRow = Cells.Find(What:="*", After:=Range("a1"), LookAt:=xlPart, LookIn:=xlFormulas, _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row
BlockColumn = Cells.Find(What:="Block", After:=Range("a1"), LookAt:=xlPart, LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
'capitalizes any text in BlockColumn
Set BlockRange = Range(Cells(2, BlockColumn), Cells(LastRow, BlockColumn))
BlockRange = [UPPER(BlockRange)]
Aside from wondering where I made a mistake, I'm sure I've over-complicated this. 除了想知道我犯了什么错误之外,我确信我已经过度复杂了。 Could someone show me a way to rethink or simplify it?
有人能告诉我一种重新思考或简化它的方法吗? I was also wondering the general pros and cons to accomplishing a task like this via looping (as opposed to this method), but not sure if this is the place to ask that...
我也想知道通过循环完成这样的任务的一般利弊(而不是这种方法),但不确定这是否是要求...
[]
is shorthand for Evaluate and does not accept variables. []
是Evaluate的简写,不接受变量。
You will need to actually use Evaluate. 您需要实际使用Evaluate。
You also need INDEX to not overwrite the entire range with the first value. 您还需要INDEX不用第一个值覆盖整个范围。
blockRange.value = blockRange.Parent.Evaluate("INDEX(UPPER(" & blockRange.Address & "),)")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.