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.
 
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.
Original Column: State Abbreviation
Desired Result: State_Abbreviation
Result from Macro: 0 [zero]
 
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Are you saying it replaced all the headers with 0?
 
Upvote 0
Ok, found the problem, try
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 = .Worksheet.Evaluate(Replace("if(iserror(left(@)+0),@,""YR""&@)", "@", .Address))
      End With
   Next ws
End Sub
 
Upvote 0
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 = .Worksheet.Evaluate(Replace("if(iserror(left(@)+0),@,""YR""&@)", "@", .Address)) End With Next ws End Sub
Great, looks good. Thanks!!
 
Upvote 0
Are you happy to use that, or would you still prefer a function?
 
Upvote 0
If it's not too much trouble, a function would be very helpful.
 
Upvote 0
Ok how about
VBA Code:
Function Alex(Cl As Range) As Variant
   Alex = Replace(Cl.Value, " ", "_")
   If IsNumeric(Left(Alex, 1)) Then Alex = "YR" & Alex
End Function
Used like
+Fluff 1.xlsm
AB
11990 Start DateEnd date
2YR1990_Start_DateEnd_date
Main
Cell Formulas
RangeFormula
A2:B2A2=Alex(A1)
 
Upvote 0
Great, I will try tweaking it to handle some more characters, but it is a wonder ful help.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,923
Messages
6,127,717
Members
449,399
Latest member
VEVE4014

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