使用VBA将excel表单数据复制到另一个表单或文件,同时运行数据function

我试图将工作表的整个数据复制到另一个工作表或文件,但我想检查每个单元格,如果其数据types是文本,则将其反转。 到目前为止,我得到了find数据types的function,并find了反转文本的function。 这里是function:

Function reverseText(text) reverseText = StrReverse(text) End Function 

和:

 Function CellType(Rng) Application.Volatile Set Rng = Rng.Range("A1") Select Case True Case IsEmpty(Rng) CellType = "Blank" Case WorksheetFunction.IsText(Rng) CellType = "Text" Case WorksheetFunction.IsLogical(Rng) CellType = "Logical" Case WorksheetFunction.IsErr(Rng) CellType = "Error" Case IsDate(Rng) CellType = "Date" Case InStr(1, Rng.text, ":") <> 0 CellType = "Time" Case IsNumeric(Rng) CellType = "Value" End Select End Function 

我该怎么做?

您可以使用ActiveSheet.UsedRange来检查每个填充的单元格的文本,并扭转它…

 Public Sub testReverse() Dim cell For Each cell In ActiveSheet.UsedRange If CellType(cell) = "Text" Then cell.Value = reverseText(cell.Value) End If Next cell End Sub Function reverseText(text) As String reverseText = StrReverse(text) End Function Function CellType(Rng) As String Application.Volatile Select Case True Case IsEmpty(Rng) CellType = "Blank" Case WorksheetFunction.IsText(Rng) CellType = "Text" Case WorksheetFunction.IsLogical(Rng) CellType = "Logical" Case WorksheetFunction.IsErr(Rng) CellType = "Error" Case IsDate(Rng) CellType = "Date" Case InStr(1, Rng.text, ":") <> 0 CellType = "Time" Case IsNumeric(Rng) CellType = "Value" End Select End Function 

你可以这样做,就像这样 –

 Case WorksheetFunction.IsText(Rng) CellType = "Text" Rng = StrReverse(Rng.Value) 

如果你想调用你的函数,那么你会做这样的事情 –

 Case WorksheetFunction.IsText(Rng) CellType = "Text" reverseText (Rng.Value) Rng = reverseText 

如果你这样做,我会给你的函数一个types。 就像是 –

 Function reverseText(text As String) As String reverseText = StrReverse(text) End Function