将工作表和单元格设置为变量 [英] Setting a Sheet and cell as variable

查看:509
本文介绍了将工作表和单元格设置为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VBA编码中是新的。让我说我从Sheet3.Cell(23,4)获取一个值的值,是否有任何方式在VBA代码让我把它设置为一个变量?



例如,我已经改变了界面,让值保留在Sheet4.Cell(20,1),我的代码中的所有参考Sheet3.Cell(23,4)都需要更改为Sheet4.Cell(20, 1)。我想是否有这样的情况编码VBA的最佳做法?

解决方案

是的。为此,请确保您声明工作表



例如



上一个代码

  Sub Sample()
Dim ws As Worksheet

设置ws =表格(Sheet3 )

Debug.Print ws.Cells(23,4).Value
End Sub

新代码

  Sub Sample()
Dim ws As Worksheet

设置ws =表格(Sheet4)

Debug.Print ws.Cells(23,4).Value
End Sub


I am new in VBA coding. Lets say I am retrieving value from Sheet3.Cell(23, 4) for a value, is there any way in the VBA code which let me set this as a variable?

For example, I have changed the interface and let the value stay at Sheet4.Cell(20,1), everywhere in my code which refer to Sheet3.Cell(23, 4) need to be changed to Sheet4.Cell(20, 1). I am thinking is there any best practice for coding VBA for situation like this?

解决方案

Yes. For that ensure that you declare the worksheet

For example

Previous Code

Sub Sample()
    Dim ws As Worksheet

    Set ws = Sheets("Sheet3")

    Debug.Print ws.Cells(23, 4).Value
End Sub

New Code

Sub Sample()
    Dim ws As Worksheet

    Set ws = Sheets("Sheet4")

    Debug.Print ws.Cells(23, 4).Value
End Sub

这篇关于将工作表和单元格设置为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆