VBA: Conver Text -> Number multiplying by 1

前端 未结 1 562
太阳男子
太阳男子 2020-12-22 04:17

I have a table linked to a SQL database that I use to create some reports. The problem is that the numbers come as text and I have to convert them to numbers everytime, I\'v

相关标签:
1条回答
  • 2020-12-22 04:43

    xlDown can be highly unpredictive. Don't use that. In fact find the last row and then create your range and then use that for your operations.

    Also just changing the format will not convert those to number. You will have to replicate the F2-Enter behavior.

    Try this (TRIED AND TESTED)

    Sub Sample()
        Dim ws As Worksheet
        Dim Rng As Range, acell As Range
        Dim lRow As Long
    
        Set ws = ThisWorkbook.Sheets("Sheet1")
    
        With ws
            lRow = .Range("AC" & .Rows.Count).End(xlUp).Row
    
            Set Rng = .Range("A4:AC" & lRow)
    
            .Range("B2").Copy
    
            Rng.PasteSpecial Paste:=xlPasteValues, _
            Operation:=xlMultiply, SkipBlanks:=False, Transpose:=False
    
            Rng.NumberFormat = "0" ' OR "0.00" as applicable
    
            For Each acell In Rng
                acell.Formula = acell.Value
            Next
        End With
    End Sub
    

    SCREENSHOT

    enter image description here

    0 讨论(0)
提交回复
热议问题