VBA - Attempt to Create A String From A Static Text String Added To A Number

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am trying to create a string from a vvlookup result returning a number and a static text string.

Rich (BB code):
trc_sb = "5"
.Cells(i, 8) = Application.WorksheetFunction.VLookup(en, ws_roster.Range("A2:B45"), 2, False) & "-" & trc_sb

The values in ws_roster.range A2:B45 are numeric. Example: 5.
Example trc_sb = "5"
Column 8 is by default custom formatted as "000" so that without the static string added, the result would be 005.

What I am trying to accomplish with my formula, using the example values would be "005-5"
What I am getting instead through testing is 43956

Looking for a solution to accomplish the result I seek.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
have you tried either of these?

.Cells(i, 8).Text = Application.WorksheetFunction.VLookup(en, ws_roster.Range("A2:B45"), 2, False) & "-" & trc_sb

OR

.Cells(i, 8) = CSTR(Application.WorksheetFunction.VLookup(en, ws_roster.Range("A2:B45"), 2, False)) & "-" & trc_sb

Unfortunately, those may simply return "43956 " instead of 43956. Don't know, I haven't tried it.
 
Upvote 0
Use
VBA Code:
.Cells(i, 8).Formula = CStr(Application.WorksheetFunction.VLookup(en, ws_roster.Range("A2:B45"), 2, False) & "-" & trc_sb)
to circumvent Excel's auto behavior, otherwise it will interpret your outcome as a date. The number 43956 is actualy (within Excel) equal to May 5th, 2020.
 
Upvote 0
Thanks guys. I appreciate all the effort. GWteB's solution, similar to dataluver's did the trick.
However, I need to have it formatted as "000-0". With th esolution provided, I am getting 5-5, whe n I am looking for 005-5. I'm still pretty shaky with the proper format function for numbers and I certainly wouldn't know where to apply the format in this formula.
 
Upvote 0
How about
VBA Code:
   Dim Res As Variant
   trc_sb = "5"
   Res = Application.VLookup(en, ws_roster.Range("A2:B45"), 2, False)
   If Not IsError(Res) Then
      .Cells(i, 8).Value = Format(Res, "000") & "-" & trc_sb
   End If
 
Upvote 0
Thank you Fluff. Excatly what was needed.
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,331
Members
449,077
Latest member
jmsotelo

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