Добрый вечер! Наверное я с очень простым вопросом, но буду искренне признательна за ответ:
на userform размещены несколько checkbox`ов, при кликании на которые в переменную последовательно заносятся соответствующие текстовые значения. На событие click каждого checkbox`а стоит вызов процедуры (см. код). С помощью процедуры между значениями ставится запятая. Беда в том, что в результате запятая ставится и после завершающего текстового значения. Подскажите, пожалуйста, как можно в итоговой переменной (S) программно удалить последний символ (запятую)?
Sub Result()
Const p = «, »
Dim S$
S = «на основании: »
If Me.cb_Sobstv.Value = True Then S = S & «права собственности» & p
If Me.сb_Arenda.Value = True Then S = S & «договора аренды» & p
If Me.cb_Xran.Value = True Then S = S & «договора ответственного хранения» & p
If Me.cb_Bank.Value = True Then S = S & «договора залога» & p
Vlad.Value = S
End Sub
You can use the following basic syntax to remove the last character from a string using VBA:
Sub RemoveLastChar()
Dim i As Integer
Dim myString As String
For i = 2 To 11
myString = Range("A" & i)
Range("B" & i) = Left(myString, Len(myString) - 1)
Next i
End Sub
This particular example removes the last character from each string in the range A2:A11 and outputs the results in the range B2:B11.
The following example shows how to use this syntax in practice.
Example: Using VBA to Remove Last Character from Strings
Suppose we have the following list of basketball team names in Excel:
Suppose we would like to remove the last character from each team name.
We can create the following macro to do so:
Sub RemoveLastChar()
Dim i As Integer
Dim myString As String
For i = 2 To 11
myString = Range("A" & i)
Range("B" & i) = Left(myString, Len(myString) - 1)
Next i
End Sub
When we run this macro, we receive the following output:
Column B displays each of the strings in column A with the last character removed.
If you would instead like to remove the last n characters from a string, simply change the 1 in the Left method to a different number.
For example, we can create the following macro to remove the last 2 characters from a string:
Sub RemoveLastTwoChar()
Dim i As Integer
Dim myString As String
For i = 2 To 11
myString = Range("A" & i)
Range("B" & i) = Left(myString, Len(myString) - 2)
Next i
End Sub
When we run this macro, we receive the following output:
Column B displays each of the strings in column A with the last two characters removed.
Note: You can find the complete documentation for the VBA Left method here.
Additional Resources
The following tutorials explain how to perform other common tasks using VBA:
VBA: How to Count Occurrences of Character in String
VBA: How to Check if String Contains Another String
VBA: How to Count Cells with Specific Text
OBJECTS
Worksheets: The Worksheets object represents all of the worksheets in a workbook, excluding chart sheets.
Range: The Range object is a representation of a single cell or a range of cells in a worksheet.
PREREQUISITES
Worksheet Name: Have a worksheet named Analyst.
String: If using the same VBA code ensure that the string from which you want to remove the last character is captured in cell («B5»).
Number of Characters to Remove: Given this tutorial shows how to remove the last character from a string, the number of characters to remove will always be 1. Therefore, you can enter this value (1) directly into the VBA code, given it’s a constant no matter which string the formula is applied against.
ADJUSTABLE PARAMETERS
Output Range: Select the output range by changing the cell reference («D5») in the VBA code to any cell in a worksheet, that doesn’t conflict with the formula.
String: Select the string from which you want to remove the last character by changing the cell reference («B5») to any cell in the worksheet that contains the string and doesn’t conflict with the formula.
in this example below I want to remove the «B» using VBA. Thus, I download these values which spam from B5:F50 and all in the end have «B». My question is twofold. A) Is there a way to delete the «B» using VBA and replace these values in the same cells? B) Or have to be in new cells?
77.65B 86.73B 92.97B 84.7B 89.4B
asked Aug 16, 2017 at 13:56
2
The code would be like this
Sub test()
Dim rngDB As Range, vDB
Dim s As String, i As Integer, j As Integer
Set rngDB = Range("b5:f50")
vDB = rngDB
r = UBound(vDB, 1)
c = UBound(vDB, 2)
For i = 1 To r
For j = 1 To c
s = vDB(i, j)
s = Left(s, Len(s) - 1)
vDB(i, j) = s
Next j
Next i
rngDB = vDB
End Sub
Or
Sub test2()
Dim rngDB As Range
Set rngDB = Range("b5:f50")
rngDB.Replace "B", "", lookat:=xlPart
End Sub
answered Aug 16, 2017 at 14:11
0
The easiest (shortest) way I can see to do this is with a Search/Replace, but this would be on the assumption that all cells end in B and a B doesn’t legitimately occur elsewhere in the cells.
Sheets("YourSheetNameHere").Range("B5:F50").Replace What:="B", Replacement:="", LookAt:=xlPart
Alternatively, you could loop through each cell and RIGHT
/LEN
etc. but why bother?
answered Aug 16, 2017 at 14:13
CLRCLR
10.7k1 gold badge10 silver badges26 bronze badges
0