Macro to Change Header in Row 1

Michael151

Board Regular
Joined
Sep 20, 2010
Messages
247
<!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:Compatibility> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]--> Hello all,

Trying to write a macro that will replace the header in row 1 based on the contents of that column.

In the row 1 header, there is a column marked “System”. I’d like to change this header to the first four letters of the first cell in this column that isn’t blank or contains the word “OPEN.”

For example:

Before:

SYSTEM
OPEN
OPEN
RTSG


After:

RTSG
OPEN
OPEN
RTSG

Another example with a blank cell in row 2:

Before:

SYSTEM

OPEN
RTSF

After:

RTSF

OPEN
RTSF


Any help is most appreciate - thank you!
 

Some videos you may like

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Michael151

Board Regular
Joined
Sep 20, 2010
Messages
247
This would be something like:

Find: "System" in Rows(1:1)

Within Found Column:

Find First Cell <> "OPEN" or <> * (blank cell)

With Found Cell, take First 4 Characters

Move to header and replace "System" in Rows(1:1)
 

Rasm

Well-known Member
Joined
Feb 9, 2011
Messages
500
Try something like this

ThisWorkBook
Code:
Option Explicit
Option Compare Text
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    Select Case Target.Column
        Case 1
            if Target.row <> 1 then exit sub
            i = 2
            Do Until Not IsEmpty(ActiveSheet.Cells(i, Target.Column).Value) And Trim(ActiveSheet.Cells(i, TargetColumn).Value) <> "Open"
                i = i + 1
                If i > 65000 Then MsgBox "Hmmmmm": Exit Sub
            Loop
            ActiveSheet.Cells(1, TargetColumn.Value) = ActiveSheet.Cells(i, TargetColumn).Value
            Exit Sub
        Case 2
            MsgBox "Do what you like"
    End Select
End Sub
 
Last edited:

mirabeau

Banned user
Joined
Nov 4, 2010
Messages
2,075

ADVERTISEMENT

Code:
Sub changeheader()
Dim coln As Integer, e, nd
coln = Application.Match("system", Cells.Resize(1), 0)
If Not IsError(coln) Then
    nd = Cells(Rows.Count, coln).End(3).Row
    For Each e In Cells(2, coln).Resize(nd)
        If (e <> "") And (e <> "OPEN") Then Cells(1, coln) = e: Exit For
    Next
End If
End Sub
Match case???
 

Rasm

Well-known Member
Joined
Feb 9, 2011
Messages
500
Correction


ActiveSheet.Cells(1, Target.Column).Value =
 

Watch MrExcel Video

Forum statistics

Threads
1,109,351
Messages
5,528,192
Members
409,807
Latest member
nicky736

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top