简体   繁体   中英

Select a column by letter from activeCell (without activeCell.EntireColumn)

First and foremost, the below works as expected. I'm trying to make the macro mimic one we have in word. Our word macro will select the entire column simply to display which column is currently being processed (the selection object is not used for any actual processing).

In excel, when I attempt to select the column (activecell.entirecolumn.select) if there is a merged cell it will show multiple columns. I need it only to select the letter column (pretty much the same as clicking the letter at the top) of the active cell. I'm hoping for a method that wont require me to parse the address of the cell if possible (I feel like string parsing is sloppy).

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

Is there anyway to select a column by letter (range("A:A").select for instance) from within an active cell?

Edit: Record MACRO shows that columns("A:A").select is used when clicking the letter at the top; however, entering that same line into the immediate window will select all columns that merged cells are merged across same as with range("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

My current solution to simply shade the cells and then restore their original shading after processing the column.

After an obviously lengthy discussion in the comments on the post. It appears the answer to my question is simply "Not Possible."

The solution I settled on in an attempt to get as close to the "Look" I was searching for is below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM