I have been able to find the column number and convert it to the letter, but I cannot figure out how to use the converted letter in defining a range. I keep getting a run time 1004 error.
I am trying to identify a column which contains the largest value in a specific row. Then use that column to find the first non zero cell and insert a row. Below is my code:
Sub ColNBR_Insert()
Dim maxCol As Long
Dim rngMaxCell As Range, rngVals As Range, C As Range, Where As Range
Dim lastNum0 As String
Dim ColLtr As String
Set rngVals = Worksheets("Charts").Range("F3:M3")
With Application.WorksheetFunction
Set rngMaxCell = rngVals.Find(.max(rngVals), LookIn:=xlValues)
If Not rngMaxCell Is Nothing Then
maxCol = rngMaxCell.Column
' MsgBox "The max value is in column " & maxCol
End If
End With
Set rngVals = Nothing
Set rngMaxCell = Nothing
'Convert To Column Letter
ColLtr = Split(Cells(1, maxCol).Address, "$")(1)
'Display Result
'MsgBox "Column " & maxCol & " = Column " & ColLtr
lastNum0 = "<>0"
Set C = Range(Range(ColLtr), Range(ColLtr))
C.Find(What:=lastNum0, After:=C(1), SearchDirection:=xlPrevious).Select
Selection.Offset(1).EntireRow.Insert Shift:=xlDown
End Sub
No need to do all this conversion... just use Columns
.
Set C = Range(Range(ColLtr), Range(ColLtr))
although problematic, is probably easier done with
Set C = Columns(maxCol)
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.