vba 在Excel中选择ActiveCell行的第1到10列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16394036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:33:31  来源:igfitidea点击:

Select column 1 to 10 of ActiveCell row in Excel

excelvbaexcel-vba

提问by partha

this is regarding a macro in an excel.

这是关于excel中的宏。

While a combination of keys are pressed (i.e. the macro is triggered), I need to do some format changes to the columns 1 to 10 of the row that has the ActiveCell.

当按下组合键(即触发宏)时,我需要对具有 ActiveCell 的行的第 1 到 10 列进行一些格式更改。

At the moment I have am selecting the entire row

目前我正在选择整行

ActiveCell.EntireRow.Select

However, I need to select only the row 1 to 10. I think it ought to be something like

但是,我只需要选择第 1 到 10 行。我认为它应该是这样的

ActiveCell.Range(1, 10).Select

But that does not work.

但这不起作用。

Just to be clear, I have read about

为了清楚起见,我已经阅读了

ActiveCell.Offset(5, -4).Select

But that is not going to work in my case. The ActiveCell could be any column of the row and hence a hardcoded offset is not going to help.

但这对我来说是行不通的。ActiveCell 可以是行的任何列,因此硬编码的偏移量无济于事。

So, the excel gurus there, I am hoping this is a quick stuff, just that somehow I can't find the answer. Please help.

所以,那里的 excel 大师,我希望这是一个快速的东西,只是不知何故我找不到答案。请帮忙。

回答by Alex P

If it is always columns 1 to 10 (i.e. A to J) then this ought to work:

如果它始终是第 1 到 10 列(即 A 到 J),那么这应该有效:

Range("A" & ActiveCell.Row & ":J" & ActiveCell.Row)

For example if the activecell is M14then this will select the range A14:J14. You can then format this how you like.

例如,如果 activecell 是M14那么这将选择范围A14:J14。然后,您可以按照自己的喜好对其进行格式化。

Hope this helps

希望这可以帮助

回答by partha

Ok. This is what I did, and it works.

好的。这就是我所做的,并且有效。

ActiveSheet.Range(Cells(ActiveCell.Row, 1), Cells(ActiveCell.Row, 10)).Select

In my mind this is sort of a hacky way. If there is anything better, please respond. Till we get something better, I guess this is the best answer for people who come looking for the anwer, after me.

在我看来,这有点像 hacky 方式。如果有更好的,请回复。在我们得到更好的东西之前,我想这是对我之后来寻找答案的人的最佳答案。

回答by Dan Donoghue

This will work

这将工作

Range("A" & ActiveCell.Row).Resize(1,10).select

回答by davex

if applicable.. spent alot of time to find, needed a more programmable - automated answer, for results - work on 1 line.

如果适用.. 花了很多时间去寻找,需要一个更可编程的——自动化的答案,为了结果——在 1 行上工作。

        Dim N7 As String: N7 = RANGE("N7")    'workcell N7 shows eg:  A:J    
        Intersect(Rows(ActiveCell.row), RANGE(N7).Columns).Select   'YES ANSWER

    'other:
        If application.Max(Intersect(Rows(ActiveCell.row), RANGE(N7).Columns)) > 0 Then

      MsgBox "YES" & Space(10), vbQuestion  ', "title": end if    & vbCr &
Else: MsgBox "NO" & Space(10), vbQuestion: End If ', "title": end if    & vbCr &

'Cells(ActiveCell.row, J6).OFFSET(, 1).RESIZE(, 6).Select    'YES: offset works on immediate cols for 1 row
'Cells(ActiveCell.row, J6).RESIZE(, 5).Select                'yes on 4 immediate cols for 1 row   (row, col)
'Cells(ActiveCell.row, B5).select

workcell N7 has: =SUBSTITUTE(SUBSTITUTE(CELL("address",$A7),"$",""),ROW(),"")&":"&SUBSTITUTE(SUBSTITUTE(CELL("address",$J7),"$",""),ROW(),"")

工作单元 N7 有: =SUBSTITUTE(SUBSTITUTE(CELL("address",$A7),"$",""),ROW(),"")&":"&SUBSTITUTE(SUBSTITUTE(CELL("address",$J7) ),"$",""),ROW(),"")