简体   繁体   中英

Set the column value of a found cell to a name

I am trying to set values for variables to be used later on in my code based on the column value of a found cell. Thanks to some existing subject, I was able to find the cell, but I am unable to set its column value to a name.

Here is my code:

Dim rFind As Range


With Range("A1:DD1")
Set rFind = .Find(What:="FIND", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)

MsgBox rFind.Column


End With

End Sub

The MsgBox returns the correct column number but my attempts at getting it set to a name has failed.

Thanks for your help !

EDIT:

My goal is to create an automatic table with data extracted from another table. I want to use the column number to extract data for each row of my table from the correct column. I currently use a system where I "hardcode" my names for the current column number (eg: Publi Const example As Integer = 5). However this is not a flexible solution if my data table were to change (new or removed columns). Finding the column to then set it would solve the issue.

Perhaps this, to name the whole column?

rFind.EntireColumn.Name = "Fred"

Fuller code

Sub x()

Dim rFind As Range

With Range("A1:DD1")
    Set rFind = .Find(What:="FIND", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
    If Not rFind Is Nothing Then
        rFind.EntireColumn.Name = "Fred"
    Else
        msgbox "Not found"
    End If
End With

End Sub

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