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

在数据上运行函数时,使用VBA将excel工作表数据复制到另一个工作表或文件,vba,excel,Vba,Excel,我试图将工作表的整个数据复制到另一个工作表或文件,但我想检查每个单元格,如果其数据类型为文本,则将其反转。到目前为止,我得到了查找数据类型的函数和反转文本的函数。 以下是功能: Function reverseText(text) reverseText = StrReverse(text) End Function 以及: 功能单元类型(Rng) 应用程序。挥发性 设定转速=转速范围(“A1”) 选择Case True 案例为空(Rng) CellType=“Blank” 案例工作表

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

Function reverseText(text)
    reverseText = StrReverse(text)
End Function
以及:

功能单元类型(Rng)
应用程序。挥发性
设定转速=转速范围(“A1”)
选择Case True
案例为空(Rng)
CellType=“Blank”
案例工作表function.IsText(Rng)
CellType=“Text”
案例工作表function.IsLogical(Rng)
CellType=“逻辑”
案例工作表function.iser(Rng)
CellType=“错误”
案例IsDate(Rng)
CellType=“日期”
案例说明(1,Rng.text,“:”)0
CellType=“时间”
案例为数字(Rng)
CellType=“值”
结束选择
端函数

我该怎么做呢?

你可以这样做-

Case WorksheetFunction.IsText(Rng)
    CellType = "Text"
    Rng = StrReverse(Rng.Value)
Case WorksheetFunction.IsText(Rng)
        CellType = "Text"
        reverseText (Rng.Value)
        Rng = reverseText
如果你想调用你的函数,你可以这样做-

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

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

Case WorksheetFunction.IsText(Rng)
    CellType = "Text"
    Rng = StrReverse(Rng.Value)
Case WorksheetFunction.IsText(Rng)
        CellType = "Text"
        reverseText (Rng.Value)
        Rng = reverseText
如果你想调用你的函数,你可以这样做-

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

您可以使用
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
公共子测试反向()
暗室
对于ActiveSheet.UsedRange中的每个单元格
如果单元类型(单元)=“文本”,则
cell.Value=reverseText(cell.Value)
如果结束
下一个细胞
端接头
函数reverseText(文本)作为字符串
reverseText=StrReverse(文本)
端函数
函数单元类型(Rng)作为字符串
应用程序。挥发性
选择Case True
案例为空(Rng)
CellType=“Blank”
案例工作表function.IsText(Rng)
CellType=“Text”
案例工作表function.IsLogical(Rng)
CellType=“逻辑”
案例工作表function.iser(Rng)
CellType=“错误”
案例IsDate(Rng)
CellType=“日期”
案例说明(1,Rng.text,“:”)0
CellType=“时间”
案例为数字(Rng)
CellType=“值”
结束选择
端函数

您可以使用
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
公共子测试反向()
暗室
对于ActiveSheet.UsedRange中的每个单元格
如果单元类型(单元)=“文本”,则
cell.Value=reverseText(cell.Value)
如果结束
下一个细胞
端接头
函数reverseText(文本)作为字符串
reverseText=StrReverse(文本)
端函数
函数单元类型(Rng)作为字符串
应用程序。挥发性
选择Case True
案例为空(Rng)
CellType=“Blank”
案例工作表function.IsText(Rng)
CellType=“Text”
案例工作表function.IsLogical(Rng)
CellType=“逻辑”
案例工作表function.iser(Rng)
CellType=“错误”
案例IsDate(Rng)
CellType=“日期”
案例说明(1,Rng.text,“:”)0
CellType=“时间”
案例为数字(Rng)
CellType=“值”
结束选择
端函数

非常感谢@WhiteHat!工作得很有魅力:-)非常感谢@WhiteHat!工作得很有魅力:-)