如何在VBA中修剪和清洁单元格

如何在VBA中修剪和清洁单元格,vba,Vba,我有一个代码,它将列名作为用户的输入,然后在工作表的末尾添加一个具有相同列名的列。我面临的问题是,我的代码没有执行我键入的行来修剪和清理新添加的列名 Set sht = ThisWorkbook.Worksheets("Input") LastColumn = sht.Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column 'MsgBox LastColum

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

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)


您需要在
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)

没问题。通常情况下,没有必要说谢谢,只需将答案标记为已接受即可。这样其他观众就知道哪个答案对海报有效。