简体   繁体   English

如何插入基于单元格的值

[英]How can I insert a value based on a cell

How can I tell VBA to select whatever is in A9, use the value in A9 (which is B5), then go to B5 and insert a value 22 (which is in B9). 如何告诉VBA选择A9中的任何内容,使用A9中的值(即B5),然后转到B5并插入值22(B9中)。

在此处输入图片说明

The output should look something like this: 输出应如下所示:

在此处输入图片说明

22 value inserted in B5. B5中插入了22个值。 Is there a way I can do this using excel formula or VBA programming language. 有没有一种方法可以使用excel公式或VBA编程语言来实现。

Sub DoIt1()
    Range(Range("A9")).Value = Range("B9").Value

End Sub

You might want to do something like this: 您可能想要执行以下操作:

Sub asd()

    Dim d 

    d = [A9].Value

    Range(d) = [A9].Offset(0, 1)

End Sub

Try this module: 试试这个模块:

sub copy_paste_fun()

    dim new_range as string
    new_range = range("A9").value

    dim new_value as double
    new_value = range("B9").value

    range(new_range) = new_value
end sub

To refer to value in A9 you can use 要引用A9中的值,您可以使用

Range("A9").select

To refer to value in adjacent B column you can use Offset function. 要引用相邻的B列中的值,可以使用偏移功能。

Range("A9").Offset(0, 1).Select

But I feel there is no need of using offset function too: 但是我觉得也不需要使用偏移功能:

You can use: 您可以使用:

Range(Range("A9")).Value = Range("B9").Value

In response to a question in the comments (checking if the cell has a Range in its contents first): 回应评论中的问题(首先检查单元格的内容是否具有范围):

Sub DoDoDo()
    Dim rng As Range

    On Error Resume Next
        Set rng = Range(Range("A9").Value)
    On Error GoTo 0

    If Not rng Is Nothing Then
        rng.Value = 99
    Else
        'Do Nothing or Catch the error
    End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM