我是否缺少声明或工作表对象标头?

时间:2019-05-16 23:39:31

标签: excel vba

当您只获得线性素材和纸的厚度时,我正在建立一个循环来计算纸卷上的纸层数。循环代表一个序列的总和,并且仅在总和等于线性镜头时才停止。它从工作表中读取变量,并将结果写入工作表中的另一个单元格。我相信我已正确设置了循环,并且已正确声明了所有内容。我试图运行我的代码,但是它给了我

  

编译错误:必需对象”并突出显示“ Sub NumLayers()

在调试器中。我不知道我缺少什么对象或声明。

这是针对2007版的excel。我已经检查了语法,并在Google上寻找了缺少的标题或声明,但似乎找不到任何线索

Sub NumLayers()

    Dim Layer As Integer
    Dim radius As Double
    Dim Footage As Integer
    Dim Thick As Double
    Dim FootSum As Double
    'Declared my variables

    Dim ws As Excel.Worksheet
    Set ws = ActiveWorkbook.Sheets("Equation")
    'initiated and set the worksheet object

    Set Thick = ws.Range("D35").Value
    Set radius = ws.Range("D27").Value
    Set Footage = ws.Range("C16").Value
    'Set variable values to values from cells in worksheet

    Set Layer = 1
    'Set counter variable at 1
    Set FootSum = 2 * 3.14159265 * radius
    'Set initial value before loop

    Do Until FootSum >= Footage
        FootSum = FootSum + (2 * 3.14159265 * (radius + (Counter * Thick)))
        Layer = Layer + 1

    Loop

    ws.Range("D37").Value = Layer
    'Write total amount of layers needed to achieve linear footage, to a cell
End Sub

我希望循环能够运行,并为我提供等于或大于给定的线性镜头(作为整数)所需的迭代次数。

1 个答案:

答案 0 :(得分:1)

如果您需要Dim ws as excel.worksheet,我不确定excel的版本 我通常使用:

Dim wb as Workbook, ws as Worksheet
set wb = ActiveWorkbook
set ws = wb.worksheets("Equation")

您的主要问题是您仅将“对象”设置为某物。像这样定义变量(不带“设置”):

Thick = ws.Range("D35").Value
radius = ws.Range("D27").Value
Footage = ws.Range("C16").Value
'variable values to values from cells in worksheet

Layer = 1
'counter variable at 1
FootSum = 2 * 3.14159265 * radius
'initial value before loop