如何通过VBA在Excel中将所选范围与所选数字相乘

时间:2017-05-16 08:02:42

标签: excel vba excel-vba

我想简化我在Excel中使用的宏来乘以所选范围。我经常从SAP导出数据,并将值格式化为文本。将它们更改为数字的简单方法是将它们乘以1.我有时也需要将值乘以-1或任何其他数字。 我创建了一个宏,它可以满足我的需求,但正如您所看到的,它必须找到一个空白单元格,在其中写入值,乘以所选范围并删除该值。我想有一个宏,没有找到空白单元格并在其中写入任何内容的过程。

Sub macro()
    Dim Cinitel As Integer
    Dim PocetRadku As Long
    Dim PocetSloupcu As Integer

    Cinitel = InputBox("Enter a number to multiply with")

    PocetSloupcu = Range("a1").CurrentRegion.Columns.Count
    PocetRadku = Range("a1").CurrentRegion.Rows.Count

    Cells(PocetRadku + 1, PocetSloupcu) = Cinitel
    Cells(PocetRadku + 1, PocetSloupcu).Copy

    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlMultiply, _
      SkipBlanks:=True, Transpose:=False

    Cells(PocetRadku + 1, PocetSloupcu) = ""

    Calculate
End Sub

我不能使用DataObject在剪贴板中放置一些值。

我想出了类似的东西,但它显然不起作用:)

Sub macro1() 
    Dim myString As String 
    Dim number As Integer 

    number = InputBox("Enter a number to multiply with") 
    myString = Selection.Address 
    myString.Formula = Application.Evaluate("=" & myString & "*" & number)

你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

试试这个..

Sub test1()

    Dim rng As Range, nbr as integer

    Set rng = Selection
    nbr = InputBox("Enter a number to multiply with") 
    rng.Value = Evaluate("IF(" & rng.Address & "<>""""," & _
                rng.Address & "*" & nbr & ","""")")

    Set rng = Nothing

End Sub