Extracting a Variable Number of Letters from a Formula using VBA Code

0 votes
I have a spreadsheet with formulas like =+'Have-in'!$B$. In this case, I want to be able to extract simply the letter "B." I can use Mid for this. For other columns, I do have more formulas, such =+'Have-in'!$AB$. How can I only extract the letters using VBA?
Mar 26, 2023 in Others by narikkadan
• 63,420 points
184 views

1 answer to this question.

0 votes

Please, try the next function:

Function extractLetter(rng As Range) As String
    extractLetter = Split(Split(rng.Formula, "!$")(1), "$")(0)
End Function

It can be tested using such a ways:

Sub testExtrLetter()
   Debug.Print extractLetter(ActiveCell)
End Sub

You can use the next fast way to process a column and return in the next one:

Sub extractAllLetters()
  Dim sh As Worksheet, lastR As Long, arrFin, i As Long
  Const colLett As String = "G" 'use here the letter of the column to be processed
  
  Set sh = ActiveSheet
  lastR = sh.Range(colLett & sh.rows.count).End(xlUp).row 'last row in the processed column
  
  ReDim arrFin(1 To lastR, 1 To 1) 'ReDim the array to receive the processed result
  For i = 1 To lastR
        arrFin(i, 1) = extractLetter(sh.cells(i, colLett))
  Next i
  
  'Drop the final array (arrFin) content, at once:
  sh.Range(colLett & 1).Offset(0, 1).Resize(UBound(arrFin), 1).Value2 = arrFin
End Sub
answered Mar 26, 2023 by Kithuzzz
• 38,010 points

Related Questions In Others

0 votes
1 answer
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,670 points
747 views
0 votes
1 answer

Is there a maximum number of formula fields allowed in Excel (2010)

See http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP010073849.aspx for limits on specs it doesn't indicate ...READ MORE

answered Sep 30, 2022 in Others by narikkadan
• 63,420 points
409 views
0 votes
1 answer

Get number of columns of a particular row in given excel using Java

Use: int noOfColumns = sh.getRow(0).getPhysicalNumberOfCells(); Or int noOfColumns = sh.getRow(0).getLastCellNum(); There ...READ MORE

answered Oct 24, 2022 in Others by narikkadan
• 63,420 points
2,577 views
0 votes
0 answers

Convert Rows to Columns with values in Excel using custom format

1 I having a Excel sheet with 1 ...READ MORE

Feb 17, 2022 in Others by Edureka
• 13,670 points
716 views
0 votes
1 answer

Remove formulas from all worksheets in Excel using VBA

Try this : Option Explicit Sub test1() ...READ MORE

answered Oct 3, 2022 in Others by narikkadan
• 63,420 points
1,444 views
0 votes
1 answer

Calculate monthly average from daily data without PivotTable

Assuming you have the months in column D enter ...READ MORE

answered Oct 3, 2022 in Others by narikkadan
• 63,420 points
1,297 views
0 votes
1 answer

Automate compound annual growth rate (CAGR) calculation

The following PowerPivot DAX formulas worked for ...READ MORE

answered Oct 7, 2022 in Others by narikkadan
• 63,420 points
989 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP