如何修剪和清洁VBA中的单元

时间:2019-05-21 05:25:09

标签: vba

我有一个代码,该代码将列名作为用户输入,然后在工作表的末尾添加具有相同列名的列。我面临的问题是我的代码无法执行我键入的行来修剪和清理新添加的列名

Set sht = ThisWorkbook.Worksheets("Input")

LastColumn = sht.Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
'MsgBox LastColumn

ColumnName = InputBox("Name of column to be added")
'MsgBox ColumnName

sht.Cells(1, LastColumn + 1).Value = ColumnName

MsgBox sht.Cells(1, LastColumn + 1).Value

Trim (sht.Cells(1, LastColumn + 1).Value)
sht.Cells(1, LastColumn + 1).Value = Application.WorksheetFunction.Clean(sht.Cells(1, LastColumn + 1))
UCase (sht.Cells(1, LastColumn + 1).Value)

1 个答案:

答案 0 :(得分:0)

您需要将Trim()之后的结果保存在变量中,

Dim variableText As String

variableText = Trim(sht.Cells(1, LastColumn + 1).Value)
variableText = Application.WorksheetFunction.Clean(sht.Cells(1, LastColumn + 1))
variableText = UCase(sht.Cells(1, LastColumn + 1).Value)

或在单元格本身中:

sht.Cells(1, LastColumn + 1).Value = Trim(sht.Cells(1, LastColumn + 1).Value)
sht.Cells(1, LastColumn + 1).Value = Application.WorksheetFunction.Clean(sht.Cells(1, LastColumn + 1))
sht.Cells(1, LastColumn + 1).Value = UCase(sht.Cells(1, LastColumn + 1).Value)