从activeCell(不含activeCell.EntireColumn)中按字母select一个列

首先,下面的工作如预期。 我试图让macros观模仿我们所说的话。 我们的单词macros将select整个列,只显示当前正在处理的列(select对象不用于任何实际处理)。

在Excel中,当我尝试select列(activecell.entirecolumn.select),如果有一个合并的单元格,它将显示多个列。 我只需要它select活动单元格的字母列(几乎相同于单击顶部的字母)。 我希望有一种方法,不会要求我parsing单元格的地址,如果可能的话(我觉得像stringparsing是马虎)。

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 

反正有没有从活动单元格中select一个字母(范围(“A:A”)。select一个例子)?

编辑:loggingMACRO显示列(“A:A”)。select用于点击顶部的字母; 然而,将同一行input即时窗口将select合并单元格与范围(“A:A”)合并在一起的所有列。select and 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 

我目前的解决scheme只是简单地遮蔽单元格,然后在处理列后恢复原来的阴影。

在对这个post的评论中,经过了很长时间的讨论。 看来我的问题的答案只是“不可能的”。

我试图解决的解决scheme,以接近我正在寻找的“看”是:

 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