Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在excel vba中设置块变量_Vba_Excel - Fatal编程技术网

如何在excel vba中设置块变量

如何在excel vba中设置块变量,vba,excel,Vba,Excel,我收到一个运行时错误:91“对象变量或未设置块变量 这是我的密码,我不知道我做错了什么 With Sheet1 lastrowcell = Range("B" & Rows.Count).End(xlUp).Row pr_high = 14 For n = pr_high To lastrowcell Max = WorksheetFunction.Max(Range(Cells(n - pr_high_1, 6), Cells(n, 6))) If Max > 0 Then

我收到一个
运行时错误:91“对象变量或未设置块变量

这是我的密码,我不知道我做错了什么

With Sheet1
lastrowcell = Range("B" & Rows.Count).End(xlUp).Row
pr_high = 14
For n = pr_high To lastrowcell

Max = WorksheetFunction.Max(Range(Cells(n - pr_high_1, 6), Cells(n, 6)))

If Max > 0 Then
    rowNum = .Columns(6).Find(What:=Max, after:=.Cells(n - period_high, 6), LookIn:=xlFormulas, lookat:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext, MatchCase:=False).Row
    '>>>>At above line I'm getting an error.

    Range(Cells(n - pr_high_1, 8)).Formula = "=Count(" & Range(Cells(n - pr_high_1, 6), Cells(rowNum, 6)).Address(False, False) & ")"
End If

Next

提前感谢。

在OP发布完整代码后编辑

我想我在您的
函数calculate\u high\u low
中发现了两行不正确的代码,即

1) 在“计数最大高”块中

应该是

rowNum = .Columns(6).Find(What:=Max, After:=.Cells(n - period_high, 6), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row
rowNum2 = .Columns(6).Find(What:=Min, After:=.Cells(n - period_low, 6), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row
因为你看的是单元格公式,而不是它的内容,所以你不会得到你想要的值

2) 在“计数最小值低”块中

应该是

rowNum = .Columns(6).Find(What:=Max, After:=.Cells(n - period_high, 6), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row
rowNum2 = .Columns(6).Find(What:=Min, After:=.Cells(n - period_low, 6), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row
因为你看的是单元格公式,而不是它的内容,所以你不会得到你想要的值

此外,您使用的是
period\u high
而不是
period\u low

但除此之外,你最好:

  • 在模块顶部使用Option Explicit语句

    它将迫使您进行一些额外的工作,并定义所有变量及其类型,但会使调试和代码维护更加容易。例如,它将弹出所有变量名错误,最有可能的是
    pr\u high
    vs
    pr\u high\u 1
    和(可能)
    period\u high
    vs
    period\u high

  • 对您的范围使用完全限定的引用,最多为工作表和工作簿范围 确保指向正确工作簿的正确工作表,否则它们将默认为活动工作表和工作簿

因此,下面是您的
函数calculate\u high\u low
仅针对上面的进行了重构:

Public Function calculate_high_low(com_tick_ask As String, rng As Variant)

Dim lastrowcell As Long, rowNum As Long, rowNum2 As Long
Dim pr_high As Long, period_high As Long, period_low As Long, pr_low As Long, pr_high_1 As Long, pr_low_1 As Long, n As Long
Dim Max As Double, Min As Double

Dim calcSht As Worksheet


    Range("A:A,E:E,F:F,H:H").Copy '<=it's referring to currently active sheet which, as it's currently called from sub "CommandButton1_Click" above, is the sheet with which the last opened ".csv" file shows
    Worksheets.Add

    Set calcSht = ActiveSheet 'it's the added new sheet

    With calcSht

        .Range("A1").PasteSpecial xlPasteValuesAndNumberFormats
        Application.CutCopyMode = False
        'Range("C1").Value = "Formula High"
        'Range("D1").Value = "Count High"
        'Range("E1").Value = "Formula Low"
        'Range("F1").Value = "Count Low"

        .Range("1:1").Font.FontStyle = "Bold Italic"

        lastrowcell = .Range("B1", .Range("B1").End(xlDown)).Count
        'lastrowcell = .Range("B1:B" & .Cells(.Rows.Count, 2).End(xlDown).Row).Count

        'count average of high and low
        .Range("F2:F" & lastrowcell).Formula = "=Average(" & .Range("B2:C2").Address(False, False) & ")"

        'long
        period_high = high_low.period_1.Text
        period_low = high_low.period_2.Text

        pr_high = period_high + 1
        pr_low = period_low + 1

        pr_high_1 = period_high - 1
        pr_low_1 = period_low - 1


        'Count Maximum High
        For n = pr_high To lastrowcell
            Max = WorksheetFunction.Max(Range(.Cells(n - pr_high_1, 6), .Cells(n, 6)))

            If Max > 0 Then
               rowNum = .Columns(6).Find(What:=Max, After:=.Cells(n - period_high, 6), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row
            'rowNum = WorksheetFunction.Max(Range(Cells(n - period_high, 6), Cells(n, 6)))
               Range(.Cells(n - pr_high_1, 8), .Cells(lastrowcell, 8)).Formula = "=Count(" & Range(.Cells(n - pr_high_1, 6), .Cells(rowNum, 6)).Address(False, False) & ")"
            End If
        Next



        'Count Minium low
        For n = pr_low To lastrowcell
            Min = WorksheetFunction.Min(Range(.Cells(n - pr_low_1, 6), .Cells(n, 6)))

            If Min > 0 Then
'                rowNum2 = .Columns(6).Find(What:=mymin, After:=.Cells(n - period_high, 6), LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row
                rowNum2 = .Columns(6).Find(What:=Min, After:=.Cells(n - period_low, 6), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row
                'rowNum2 = WorksheetFunction.Min(Range(Cells(n - period_high, 6), Cells(n, 6)))
                Range(.Cells(n - pr_low_1, 10), .Cells(lastrowcell, 10)).Formula = "=Count(" & Range(.Cells(n - pr_low_1, 6), .Cells(rowNum2, 6)).Address(False, False) & ")"
            End If

        Next


        'Calculate Formula for High & Low

        'For high
        .Range("G2:G" & lastrowcell).Value = "=(" & period_high & "-" & .Range("H2").Address(False, False) & ")/" & period_high

        'For Low
        .Range("I2:I" & lastrowcell).Value = "=(" & period_low & "-" & .Range("J2").Address(False, False) & ")/" & period_low



    End With
    high_low.Hide

End Function
Public Function compute\u high\u low(com\u tick\u ask作为字符串,rng作为变量)
将lastrowcell变暗为Long,rowNum变暗为Long,rowNum2变暗为Long
暗pr_high As Long,period_high As Long,period_low As Long,pr_low As Long,pr_high_1 As Long,pr_low_1 As Long,n As Long
最大尺寸为双精度,最小尺寸为双精度
Dim calcSht As工作表
范围(“A:A,E:E,F:F,H:H”)。然后复制“0”
rowNum=.Columns(6).Find(What:=Max,After:=.Cells(n-period_high,6),LookIn:=xlValues,LookAt:=xlWhole,SearchOrder:=xlByRows,SearchDirection:=xlNext,MatchCase:=False)。行
'rowNum=WorksheetFunction.Max(范围(单元格(n-period_high,6),单元格(n,6)))
范围(.Cells(n-pr_high_1,8),.Cells(lastrowcell,8)).Formula=“=Count(&Range(.Cells(n-pr_high_1,6),.Cells(rowNum,6)).Address(False,False)&”)
如果结束
下一个
“最低计数
对于n=pr_低至最后一行单元格
Min=工作表函数.Min(范围(.Cells(n-pr_low_1,6),.Cells(n,6)))
如果最小值>0,则
'rowNum2=.Columns(6).Find(What:=mymin,After:=.Cells(n-period_high,6),LookIn:=xlFormulas,LookAt:=xlother,SearchOrder:=xlByRows,SearchDirection:=xlNext,MatchCase:=False)。行
rowNum2=.Columns(6).Find(What:=Min,After:=.Cells(n-period_low,6),LookIn:=xlValues,LookAt:=xlother,SearchOrder:=xlByRows,SearchDirection:=xlNext,MatchCase:=False)。行
'rowNum2=WorksheetFunction.Min(范围(单元格(n-周期高,6),单元格(n,6)))
范围(.Cells(n-pr_low_1,10),.Cells(lastrowcell,10)).Formula=“=Count(&Range(.Cells(n-pr_low_1,6),.Cells(rowNum2,6)).Address(False,False)&”)
如果结束
下一个
'计算高和低的公式
“高
.Range(“G2:G”和lastrowcell).Value=“=(”&period_high&“-”&Range(“H2”)。地址(False,False)&“/”&period_high
“低
.Range(“I2:I”和lastrowcell).Value=“=(“&period_low&”-“&Range(“J2”)。地址(False,False)&/”&period_low
以
高低皮
端函数
正如我所说的,这应该只是您的一个起点,因为您发布的整个代码缺乏良好的编码模式,并且很可能会在这一步之后失败,如果上面的内容能够解决后一步的问题的话


但是,如果你遵循建议的模式,你应该更容易抓住所有可能的问题,即使是那些逻辑问题,因为清晰的代码可以让你专注于他必须开发的算法

什么是pr_high_1?哪一行产生了错误?你能展示你的Sheet1变量声明吗?@karthigh pr_high_1只是
pr_high-1
i。e、 14-1=13@SanjeevShinde,在这一行中,rowNum=.Columns(6)…。它将返回错误,因为您正在为ex:14-14-1减去相同的行号,那么它将是负数。它不应该工作,因为您甚至没有给出所有的代码。相反,它应该可能会引导您到“正确”位置“只要涉及到编码模式,就可以使用路径。您可能希望发布整个代码并上载数据文件(“Sheet1”),并指定代码应该执行的操作以及卡住的位置(哪行)和原因(什么错误)。这里的人可能会帮助你@user3598756这里我给我的文件的链接,请分别上传一个“.xlsx”文件(不是“.xlsm”文件,宏可能有害…)和代码(只需更新你的Q窗格代码)。这是代码,请帮助…见编辑后的答案。我认为,要使整个代码达到所需的健壮性,还有很长的路要走。但我希望我给了你一个好的起点