【问题标题】:Select a column by letter from activeCell (without activeCell.EntireColumn)从 activeCell 中按字母选择一列(没有 activeCell.EntireColumn)
【发布时间】:2014-12-29 01:02:56
【问题描述】:

首先,以下内容按预期工作。我正在尝试使宏模仿我们在单词中的宏。我们的 word 宏将选择整个列,只是为了显示当前正在处理的列(选择对象不用于任何实际处理)。

在 excel 中,当我尝试选择列 (activecell.entirecolumn.select) 时,如果有合并单元格,它将显示多个列。我只需要它来选择活动单元格的字母列(与单击顶部的字母几乎相同)。我希望有一种方法,如果可能的话,不需要我解析单元格的地址(我觉得字符串解析很草率)。

Sub setwidths()
Dim rangeName As String
Dim selectedRange As range
Dim tempRange As range
Dim x As Integer

'If only 1 cell is selected, attempt to find the correct named range
If Selection.Cells.Count = 1 Then 
    rangeName = Lib.getNamedRange(Selection) 'Built in function from my lib (works I promise)

    If rangeName <> "" Then
        Application.Goto reference:=rangeName
    End If
End If

Set selectedRange = Selection

'Go column by column asking for the width
'Made to mimic a word MACRO's behavior and moving backwards served a point in word
For x = selectedRange.Columns.Count To 1 Step -1
    Set tempRange = selectedRange.Columns(x)
    tempRange.Cells(tempRange.Cells.Count, 1).Select
'This is where the code should go to select the column
    tempRange.ColumnWidth = InputBox("This columns?")
Next
End Sub

是否可以从活动单元格中按字母选择列(例如 range("A:A").select)?

编辑: Record MACRO显示点击顶部字母时使用了columns("A:A").select;但是,在即时窗口中输入同一行将选择合并单元格合并的所有列,与 range("A:A").select 和 activecell.selectcolumn

Sub NSTableAdjust()
Dim rangeName As String
Dim selectedRange As range
Dim tempRange As range
Dim cellsColor() As Long
Dim cellsPattern() As XlPattern
Dim cellsTaS() As Long
Dim cellsPTaS() As Long
Dim result As String
Dim abort As Boolean

Dim x As Integer
Dim y As Integer

'Delete the block between these comments and run macro on 10x10 grid in excel to test
If Selection.Cells.Count = 1 Then
    rangeName = Lib.getNamedRange(Selection)

    If rangeName <> "" Then
        Application.Goto reference:=rangeName
    End If
End If
'Delete the block between these comments and run macro on 10x10 grid in excel to test

Set selectedRange = Selection
ReDim cellsArr(1 To selectedRange.Rows.Count)
ReDim cellsColor(1 To UBound(cellsArr))
ReDim cellsPattern(1 To UBound(cellsArr))
ReDim cellsTaS(1 To UBound(cellsArr))
ReDim cellsPTaS(1 To UBound(cellsArr))
abort = False

For x = selectedRange.Columns.Count To 1 Step -1
    Set tempRange = selectedRange.Columns(x)
    tempRange.Cells(tempRange.Cells.Count, 1).Select

    For y = 1 To UBound(cellsColor)
        With tempRange.Cells(y, 1).Interior
            cellsColor(y) = .Color
            cellsPattern(y) = .Pattern
            cellsTaS(y) = .TintAndShade
            cellsPTaS(y) = .PatternTintAndShade
            .Color = 14136213
        End With
    Next

    result = InputBox("This Column?")

    If IsNumeric(result) Then
        tempRange.ColumnWidth = result
    Else
        abort = True
    End If

    For y = 1 To UBound(cellsColor)
        With tempRange.Cells(y, 1).Interior
            .Color = cellsColor(y)
            .Pattern = cellsPattern(y)
            .TintAndShade = cellsTaS(y)
            .PatternTintAndShade = cellsPTaS(y)
        End With
    Next

    If abort Then
        Exit Sub
    End If
Next
End Sub

我目前的解决方案是简单地对单元格进行着色,然后在处理完列后恢复其原始着色。

【问题讨论】:

  • @GeneSkuratovsky 我是否花了 5 个小时浏览我能找到的每个帖子?否。我是否使用不同的关键字进行了多次搜索并单击了填写问题时出现的所有建议主题?是的。您能否发布指向您提到的众多帖子之一的链接,而不是粗鲁?显然不是。
  • 可能唯一的方法是删除合并的单元格。有了它们,当以编程方式选择列时,似乎也选择了属于合并单元格一部分的所有其他列。
  • @simoco,Toby 想使用SELECT 来突出显示该列,所以在这种情况下,也许那篇好帖子并不相关。
  • 我同意@dee 的观点,除非您取消合并单元格,否则您无法避免这种行为。这就是为什么合并的单元格是邪恶的:)。一种解决方法可能是只选择列中未合并单元格的区域。或者也许你可以用条件格式做一些事情。或者可能是另一种方法,例如消息框或状态栏中的信息,告诉用户正在做什么。

标签: vba excel


【解决方案1】:

在帖子上的 cmets 进行了明显冗长的讨论之后。看来我的问题的答案只是“不可能”。

为了尽可能接近我正在寻找的“外观”而确定的解决方案如下:

For x = selectedRange.Columns.Count To 1 Step -1
    Set tempRange = selectedRange.Columns(x) 'Range of the column

    'Our standards dictate the last cell in the range will not be merged
    With tempRange.Cells(tempRange.Cells.Count, 1) 
        .Select 'Selecting here will for excel to make sure the range is in view
        'Very simple/basic conditional formatting rule
        Set fCondition = .EntireColumn.FormatConditions. _
            Add(Type:=xlExpression, Formula1:="=True")
            fCondition.Interior.Color = 15123099
        'Make sure it is the highest priority rule
        fCondition.Priority = 1
    End With

    'Get user input
    result = InputBox("This Column?")

    'Delete rule
    fCondition.Delete

    'Validate user input
    If IsNumeric(result) Then
        tempRange.ColumnWidth = result
    Else
        abort = True
    End If

    If abort Then
        Exit Sub
    End If
Next

【讨论】:

    猜你喜欢
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多