Link to home
Start Free TrialLog in
Avatar of mcrmg
mcrmg

asked on

Look for a header and add value

Hi,

Is there a way to look for a specific header, say, "ReportAsOfDate"

And add a new value to the entire column? thanks
Avatar of Daniel Pineault
Daniel Pineault

In what software?  What file?  Please explain further.
Avatar of mcrmg

ASKER

I am sorry. I am trying to do it in EXCEL vba. thanks
In Excel using VBA you would like to find column with the header (1st row?) entitled ReportAsOfDate and apply a specific value to all the rows of data in that column?  Is that right?

Any chance you can provide us with a sample workbook this always helps, but isn't obligatory.  We just need to properly understand your need.
Avatar of mcrmg

ASKER

I actually need to construct this workbook from SQL as I do not have it now. I am just using "ReportAsOfDate" as an example.

Once I have the Excel from SQL. I will need to add some fields based on the needs (eg. ReportASOfDate)

I would like to see if it is possible to check first if ReportASOfDate exists, if it does, add static value to that column.  If this field does not exist, stop.

thanks
Sorry, I'm still not 100% sure of the setup.

Using Excel (VBA) you want to query a database and extract data?  What database (Access, SQL Server, ORACLE, MySQL, ...)?
Avatar of mcrmg

ASKER

Sorry again.
Please see the sample file. When I export data from SQL. the file will look like the same file. (of course, more data and more fields)

I will then send it to the biz side, they will work on the file and send back with new requests.

I might need to add new fields. Say I would like to search if "ReportAsOfDate" exists, if not,
I would like to add it before "Name" and add static value to the column.

thanks
Book1.xlsx
You could do something along the lines of
Function a()
    Dim lColumn               As Long
    Dim lReportAsOfDateCol    As Long
    Dim lNameCol              As Long

    On Error GoTo Error_Handler

    Sheets(1).Activate
    lColumn = ActiveSheet.Cells(1, ActiveSheet.Columns.Count).End(xlToLeft).Column
    For i = 1 To lColumn
        If Cells(1, i) = "Name" Then lNameCol = i
        If Cells(1, i) = "ReportAsOfDate" Then lReportAsOfDateCol = i
    Next i
    If lReportAsOfDateCol = 0 Then
        Columns(lNameCol).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Cells(1, lNameCol) = "ReportAsOfDate"
    End If

Error_Handler_Exit:
    On Error Resume Next
    Exit Function

Error_Handler:
    MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: a" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occured!"
    Resume Error_Handler_Exit
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Daniel Pineault
Daniel Pineault

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mcrmg

ASKER

This is exactly what I need. thank you very much
My pleasure.  Glad I could assist.