VBA在Selection.Find之后按名称选择列 [英] VBA selecting column by name after Selection.Find

查看:434
本文介绍了VBA在Selection.Find之后按名称选择列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索列后,很难让VBA按名称选择整个列(可以有不连续的数据).

I'm having difficulty to have VBA select an entire column by name (can have noncontigous data) after searching for the column.

' Select the first row
Rows("1:1").Select

Selection.Find(What:="LongColumnName", After:=ActiveCell, LookIn:= _
    xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
    xlNext, MatchCase:=False, SearchFormat:=False).Activate
    Selection.End(xlUp).Select

问题是找到单元格后,选择仍然是整个第1行 . 选定的列之后,我将其移到最前面,然后对几列进行此操作.最后,我将插入列并在列值之间进行一些比较.

Problem is after finding the cell the selection is still the entire row 1. After I have the selected column I will move it to the front and will do this for several columns. Lastly I will insert columns and do some comparison between the column values.

谢谢

推荐答案

给出您的描述,如果将Activate更改为Select,将xlUp更改为xlDown,则传递的代码将起作用.这样看起来就像

Given your description, the code you passed would work if you changed Activate for Select and xlUp for xlDown. So it would look like

Rows("1:1").Select

Selection.Find(What:="LongColumnName", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Select
Selection.End(xlDown).Select

但是,如果该列中有空行,这可能会产生一些问题(因为在描述中您已声明要选择整个列).所以我会去做以下事情

However, if you have empty rows in that column that could generate some problems (since in your description you stated you would want to select the entire column). So I would go for the following

Rows("1:1").Select

Selection.Find(What:="LongColumnName", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).EntireColumn.Select

这篇关于VBA在Selection.Find之后按名称选择列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆