LEFT Function in Macro

S

Shauna Koppang

If I have a specific cell say on Sheet1!B8 that I want to
go to and use the left function on, in VB coding how do I
do this?

What I have is the user picks a Vendor number - Name item
from ComboBox3 and it inserts into cell Sheet1!B8 but then
once inserted I want to remove the text leaving only the
numbers.
Eg. In List: 1000 - Company Name
Result desired: 1000

It is always 4 digits, a space, a dash, a space, then a
name, so the length is always 4.

So I had the macro go to the cell via:

Application.Goto Reference:=Worksheets("Sheet1").Range
("B8")

Which goes correctly and selects the cell, but how to I
then say - with the current contents remove all but the 4
left most characters?

Sorry, I am new at VB.

Thanks!

SHauna
 
J

Jim Carlock

lngNumber = CLng(Left$(ThisWorkSheet.Cells("B8").Value, 4))
dblNumber = CDbl(Left$(ThisWorkSheet.Cells("B8").Value, 4))
sngNumber = CSng(Left$(ThisWorkSheet.Cells("B8").Value, 4))

Hope that helps.
 
S

Shauna Koppang

Thank you thank you thank you!!!

Worked perfectly!

Application.Goto Reference:=Worksheets("Sheet1").Range
("B8")
ActiveCell.Value = Left(ActiveCell.Value, 4)

Shauna
 
N

Norman

dim temp
temp = Left(Worksheets("Sheet1").Range("B8"),4)
Worksheets("Sheet1").Range("B8")=temp
 
J

Jim Carlock

Oops,

Here's the correction:

Dim lngNumber&, dblNumber As Double
Dim sngNumber As Single

ThisWorkbook.ActiveSheet.Cells(1, 3) = "1234-xl-2003"
lngNumber = CLng(Left$(ThisWorkbook.ActiveSheet.Cells(1, 10).Value, 4))
dblNumber = CDbl(Left$(ThisWorkbook.ActiveSheet.Cells(1, 10).Value, 4))
sngNumber = CSng(Left$(ThisWorkbook.ActiveSheet.Cells(1, 10).Value, 4))
ThisWorkbook.ActiveSheet.Cells(2, 3) = CStr(lngNumber)
ThisWorkbook.ActiveSheet.Cells(3, 3) = CStr(dblNumber)
ThisWorkbook.ActiveSheet.Cells(4, 3) = CStr(sngNumber)
 
J

Jim Carlock

Oops,

Here's the correction:

Dim lngNumber&, dblNumber As Double
Dim sngNumber As Single

ThisWorkbook.ActiveSheet.Cells(1, 3) = "1234-xl-2003"
lngNumber = CLng(Left$(ThisWorkbook.ActiveSheet.Cells(1, 10).Value, 4))
dblNumber = CDbl(Left$(ThisWorkbook.ActiveSheet.Cells(1, 10).Value, 4))
sngNumber = CSng(Left$(ThisWorkbook.ActiveSheet.Cells(1, 10).Value, 4))
ThisWorkbook.ActiveSheet.Cells(2, 3) = CStr(lngNumber)
ThisWorkbook.ActiveSheet.Cells(3, 3) = CStr(dblNumber)
ThisWorkbook.ActiveSheet.Cells(4, 3) = CStr(sngNumber)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top