Multiply Entire Range By Value?

前端 未结 3 1946
你的背包
你的背包 2020-12-03 19:34

So The best way I could think of to accomplish this over a large range (about 450k rows) was to use the following Sue-do code:

Range(\"A1\").Copy \' A1 Cont         


        
相关标签:
3条回答
  • 2020-12-03 19:45
    Sub Test()
    
        Dim rngData As Range
    
        Set rngData = ThisWorkbook.Worksheets("Sheet1").Range("A1:B10")
        rngData = Evaluate(rngData.Address & "*2")
    End Sub
    

    Kind of outdated but is that what you were looking for ?

    0 讨论(0)
  • 2020-12-03 20:03

    Do not update cell by cell. It is very slow and there is a better way with VBA. Here is the outline:

    1. Set a Range to all rows/columns needed to process
    2. Copy values into array in VBA
    3. Process the array
    4. Write the array back into the worksheet in one operation

    Here is an example:

    Public Sub FactorRange(ByRef r_first as Range, ByVal N_rows as Long, _
    ByVal N_cols as Long, ByVal factor as Double)
        Dim r as Range
        'Set range from first cell and size
        Set r = f_first.Resize(N_rows,N_cols)
        Dim vals() as Variant
        ' Copy cell values into array
        vals = r.Value
        Dim i as Long, j as Long
        ' Do the math
        For i=1 to N_rows
          For j=1 to N_cols
            vals(i,j) = factor * vals(i,j)
          Next j
        Next i
        ' Write values back
        r.Value = vals
    End Sub
    
    0 讨论(0)
  • 2020-12-03 20:06
    return_sheet = ActiveSheet.Name
    ActiveWorkbook.Sheets.Add
    ActiveSheet.Name = "CopyPaste"
    Selection.Value = 1
    Selection.Copy
    Sheets(return_sheet).Select 'if necessary select range you whant to multiply
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
    SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
        Application.DisplayAlerts = False
        Sheets("CopyPaste").Delete
        Application.DisplayAlerts = True
    Sheets(return_sheet).Select
    
    0 讨论(0)
提交回复
热议问题