VBA code to select first digit/letter of each column

Sekiro

New Member
Joined
Aug 29, 2023
Messages
10
Office Version
  1. 2021
Platform
  1. Windows
Hello.

I need some of your help that may allow me to save some time.

I need a VBA code that on the second column (B) ,"Unique codes" , to automatically appear the first letter/digit of each of the other columns, but the full country code and if one of the columns it's empty, this will not effect the other letters.

For example for Spain, the unique code: VA7IT , for Romania: 7NI8GB and for Poland: AA2T3ESM

Unique codes.png


Thank you for all your help.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hello.

I need some of your help that may allow me to save some time.

I need a VBA code that on the second column (B) ,"Unique codes" , to automatically appear the first letter/digit of each of the other columns, but the full country code and if one of the columns it's empty, this will not effect the other letters.

For example for Spain, the unique code: VA7IT , for Romania: 7NI8GB and for Poland: AA2T3ESM

View attachment 119583

Thank you for all your help.
Dates and Times.xlsm
ABCDEFGHI
1CompanyUnique CodesAddress Line 1Address Line 2Address Line 3CityPostal CodeCountry CodeProvince
2SpainVA7ITVia AmericaAmsterdam73623IT
3Romania7NI8GB73 StreetNear officeIstanbul83727GB
4PolandAA2T3ESMAvenida 63Alcala28Toledo3425ESMadrid
Sheet3
Cell Formulas
RangeFormula
B2:B4B2=LEFT(C2,1) & LEFT(D2,1) & LEFT(E2,1) & LEFT(F2,1) & LEFT(G2,1) & H2 & LEFT(I2,1)
 
Upvote 0
Best if you identified your columns; better still if you used XL2BB to post data. You don't need vba but if that's what you want then this un-elegant code may suffice:
VBA Code:
Public Function Sekiro() As String
Dim rng As Range, cel As Range
Dim sht As Worksheet
Dim str As String, i As Long

i = Application.Caller.Parent.Cells.row
Set sht = ActiveSheet
Set rng = sht.Range("C" & i & ":" & "H" & i)
For Each cel In rng
    If cel <> "" Then str = str & Left(cel, 1)
Next
str = str & sht.Range("I" & i)
If sht.Range("J" & i) <> "" Then str = str & sht.Range("J" & i)
Sekiro = str
   
MsgBox Sekiro
End Function
The call would be as =Sekiro() in whatever cells you want to construct the value in. The function has to be in a standard module.
 
Upvote 0
If you decided that you don't really need a macro then you could try this

Sekiro.xlsm
ABCDEFGHI
1CompanyUnique CodesAddress Line 1Address Line 2Address Line 3CityPostal CodeCountry CodeProvince
2SpainVA7ITVia AmericaAmsterdam73623IT
3Romania7NI8GB73 StreetNear officeIstanbul83727GB
4PolandAA2T3ESMAvenida 63Alcala28Toledo3425ESMadrid
Sheet1
Cell Formulas
RangeFormula
B2:B4B2=CONCAT(IF(C$1:I$1="Country Code",C2:I2,LEFT(C2:I2,1)))



If you do really want vba then it could be done like this.

VBA Code:
Sub Codes()
  With Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)
    .Formula2 = "=CONCAT(IF(C$1:I$1=""Country Code"",C2:I2,LEFT(C2:I2,1)))"
    .Value = .Value
  End With
End Sub
 
Upvote 0
Hello,

Thank you all for the help, but when I try to write the formula or even the VBA code, I get this,
1732469423299.png
 
Upvote 0
Looks like your cells are formatted as Text. Format them as General and then re-enter the first formula and copy it down or run the vba.
 
Upvote 0

Forum statistics

Threads
1,223,996
Messages
6,175,867
Members
452,678
Latest member
will_simmo

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