PDA

View Full Version : Solved: VBA not working



parscon
02-16-2012, 12:44 PM
I have this VBA Code but when run this code



Sub Test()
Dim v As Variant
For Each cell In Range("D1:D5")

cell.Value = "MS-" & Right$(cell.Value, Len(cell.Value) - 3)
Next cell
v = Application.Transpose(Range("D1:D2000").Value)
With Range("C1")

.Value = Join(v, "-")
Do While InStr(.Value, "--") > 0

.Value = Replace(.Value, "--", "-")
Loop


End With


End Sub





in line


cell.Value = "MS-" & Right$(cell.Value, Len(cell.Value) - 3)



I have error , please help me to fix this VBA .

THank you so much .

parscon
02-16-2012, 12:55 PM
I have another VBA and the same problem :




Sub sample()

With Sheets("sheet1")
.Range(.Range("B1"), .Range("B65536").End(xlUp)).Copy
End With
Sheets("sheet2").[C65536].End(xlUp)(1).PasteSpecial Paste:=xlValues

For Each cell In Range("B1:B5")

cell.Value = "MS-" & Right$(cell.Value, Len(cell.Value) - 3)
Next cell
End Sub

CatDaddy
02-16-2012, 01:14 PM
you have not initialized cell to a range?

add:
Dim cell As Range
to the start of your code

mbarron
02-16-2012, 01:24 PM
It would be helpful if you had included the error message.

I'm guessing one of the values in your range is less than 3 characters long which would cause the Right$() function to select a negative character count.

parscon
02-16-2012, 01:25 PM
Thank you , sloved .

Thanks for all your helps .