How to set a variable equal to a cell address?

2019-08-10 19:37发布

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!!

    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

2条回答
ら.Afraid
2楼-- · 2019-08-10 20:02

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

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

Set inserir = ActiveCell
查看更多
看我几分像从前
3楼-- · 2019-08-10 20:19

You need to set the value

Set inserir = ActiveCell
查看更多
登录 后发表回答