Function or Macro to Clean Up Column Headings

Alex20850

Board Regular
Joined
Mar 9, 2010
Messages
146
Office Version
  1. 365
Platform
  1. Windows
I have a spreadsheet with several dozen tabs. Each tab is in a format for converting into databases.
I will be using a program to convert them into Oracle SQL insert scripts.
Currently, some of the column headings start with numbers and have spaces, for example 2010 land area.
It should be changed to YR2010_land_area. I am assuming if it starts with a number it is a year.
This is because Oracle SQL columns cannot start with numbers or have spaces.
I am not a VBA programmer, but I have installed function and macro code before, but I don't what would work better in this case.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Are the headers rows on each tab on row 1 & are they all the same number of columns?
If they are the same number of columns how many?
 
Upvote 0
Are the headers rows on each tab on row 1 & are they all the same number of columns?
If they are the same number of columns how many?
The headers are always in row 1. The number of columns varies.
 
Upvote 0
Ok, how about
VBA Code:
Sub Alex()
   Dim Ws As Worksheet
   
   For Each Ws In Worksheets
      Ws.Range("1:1").Replace " ", "_", xlPart, , , , False, False
      With Ws.Range("A1", Ws.Cells(1, Columns.Count).End(xlToLeft))
         .Value = Evaluate(Replace("if(iserror(left(@)+0),@,""YR""&@)", "@", .Address))
      End With
   Next Ws
End Sub
 
Upvote 0
The macro didn't work and I would like to use a function instead.
I would create a new row and then run the function on the column headings.

I ran the macro on a page:
The column headings:

State AbbreviationState FIPS CodeCounty FIPS CodeFIPS Entity CodeANSI CodeFull NameGU NameEntity Description

After the macro:

0​
000000
0​
 
Upvote 0
In what way?
State AbbreviationState FIPS CodeCounty FIPS CodeFIPS Entity CodeANSI CodeFull NameGU NameEntity Description
Should be:
State_AbbreviationState_FIPS_CodeCounty_FIPS_Codeetc.
 
Upvote 0
But what does the macro give you?
 
Upvote 0
The macro didn't work and I would like to use a function instead.
I would create a new row and then run the function on the column headings.

I ran the macro on a page:
The column headings:

State AbbreviationState FIPS CodeCounty FIPS CodeFIPS Entity CodeANSI CodeFull NameGU NameEntity Description

These are the results:

00000000
 
Upvote 0
I am not going to spend time writing a function until you till me in what way the macro failed. Otherwise the function is likely to fail as well.
 
Upvote 0

Forum statistics

Threads
1,214,887
Messages
6,122,095
Members
449,064
Latest member
Danger_SF

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top