将存储为文本的数字转换为具有相同范围的多个工作表中的值

时间:2018-02-27 14:47:09

标签: excel-vba vba excel

今天我的问题很简单但很难像我这样的新手。我有两个名为Sales和Pipeline的工作表,它们具有相同的标题,因此范围相同,我想在两个工作表中同时将存储为文本的数字转换为值,作为文本的数字存在于第I列和第Y列,我有使用下面的代码,但无法找到激活管道表的方法,当前代码也将I到Y的所有值都更改为不需要的值,

Sub ConvertNumber()
    Dim rng As Range

    ThisWorkbook.Sheets("Sales").Activate

    Range("I2:I10000", "Y2:Y10000").Select
    With Selection
        Selection.NumberFormat = "General"
        .Value = .Value

    End With
    MsgBox "Company ID and DUNS Number Converted to Values"
End Sub

3 个答案:

答案 0 :(得分:1)

将其拆分为两个潜艇,并将其调用两次:

Sub Main()
    ConvertNumber ThisWorkbook.Worksheets("Sales")
    ConvertNumber ThisWorkbook.Worksheets("Pipeline")
    MsgBox "Company ID and DUNS Number Converted to Values"
End Sub

Sub ConvertNumber(MySheet As Worksheet)
    With MySheet.Range("I2:I10000", "Y2:Y10000")
        .NumberFormat = "General"
        .Value = .Value
    End With
End Sub

答案 1 :(得分:0)

这使用数组和循环:

Sub ConvertNumber()
    Dim shtArr As Variant
    Dim rngArr As Variant
    Dim i As Long, j As Long
    shtArr = Array("Sales", "Pipeline")
    rngArr = Array("I2:I10000", "Y2:Y10000")
    For i = LBound(shtArr) To UBound(shtArr)
        For j = LBound(rngArr) To UBound(rngArr)
            With ThisWorkbook.Worksheets(shtArr(i)).Range(rngArr(j))
                .NumberFormat = "General"
                .Value = .Value
            End With
        Next j
    Next i
End Sub

答案 2 :(得分:0)

考虑使用2个循环:

Sub marine()
    For Each s In Array("Sales", "Pipeline")
        For Each t In Array("I2:I10000", "Y2:Y10000")
            With Sheets(s).Range(t)
                .NumberFormat = "General"
                .Value = .Value
            End With
        Next t
    Next s
End Sub