vba 如何设置一个变量等于一个单元格地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19234132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:50:03  来源:igfitidea点击:

How to set a variable equal to a cell address?

excel-vbavbaexcel

提问by Felipe

I have to find the next empty cell in column B (variable coluna_amostras), save its address (using variable inserir) and use it to paste a new row data. However, I couldn't figure out how to store the address in inserir variable. As it′s defined, excel returns "Run-time error 91 - Object variable or with block variable not set". Could someone help me? Thanks!!

我必须在 B 列(变量 coluna_amostras)中找到下一个空单元格,保存其地址(使用变量 inserir)并使用它来粘贴新行数据。但是,我无法弄清楚如何将地址存储在 inserir 变量中。正如定义的那样,excel 返回“运行时错误 91 - 对象变量或块变量未设置”。有人可以帮助我吗?谢谢!!

    Sub CopiarOriginais()


    Dim Certeza As VbMsgBoxResult
    Dim sample As String
    Dim coluna_amostras As Range
    Dim inserir As Range


        ActiveSheet.Name = Range("Y1").Value
        sample = Range("Y1").Value

    Certeza = MsgBox("Você tem certeza de que os dados originais já n?o foram copiado? Utilizar novamente essa fun??o, após o teste 2-sigma ter sido aplicado, comprometerá os seus dados originais.", vbYesNo)

        If Certeza = vbNo Then End

        Sheets("Results").Activate
        Range("B2").End(xlDown).Offset(1, 0).Select
        inserir = ActiveCell

  Sheets(sample).Activate

        Range("B3:D122").Copy
        Range("B132").PasteSpecial xlPasteValues
        Application.CutCopyMode = False


        Worksheets(sample).Range("ratio143144").Copy

            Worksheets("Results").Activate
            Range("D" & inserir.Row).Select
            ActiveSheet.PasteSpecial Link:=True

回答by GSerg

inserir = ActiveCellis the same as inserir.Value = ActiveCell.Value, which fails because inseriris Nothing.

inserir = ActiveCell与 相同inserir.Value = ActiveCell.Value,失败是因为inserirNothing

If want to save a reference to an object, you must use Set:

如果要保存对对象的引用,则必须使用Set

Set inserir = ActiveCell

回答by user2140261

You need to set the value

您需要设置值

Set inserir = ActiveCell