在数据上运行函数时,使用VBA将Excel工作表数据复制到另一个工作表或文件

时间:2015-12-15 14:36:42

标签: excel vba excel-vba

我试图将工作表的整个数据复制到另一个工作表或文件,但我想检查每个单元格,如果其数据类型是文本,则将其反转。到目前为止,我得到了查找数据类型的函数和反转文本的函数。 以下是功能:

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

我该怎么做?

2 个答案:

答案 0 :(得分:0)

你可以在你的情况下这样做 -

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

如果你想打电话给你的功能,那么你会做这样的事情 -

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

如果你这样做,我会给你的函数一个类型。像 -

这样的东西
Function reverseText(text As String) As String
    reverseText = StrReverse(text)
End Function

答案 1 :(得分:0)

您可以使用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