运行代码时,VBA方法的对象范围_Worksheet失败了? [英] VBA method 'range of object' _Worksheet failed suddenly coming up when running code?

查看:454
本文介绍了运行代码时,VBA方法的对象范围_Worksheet失败了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个代码用于运行正常,那么突然之间它开始失败的方法范围的对象_Worksheet失败?
'
我不是VBA的专家,但我可以自己解决。有没有我想要的东西?

This code used to run fine, then all of a sudden it started failing on "method 'range of object' _Worksheet failed"? ' I'm not an expert with VBA, but I can get myself around it. Is there something I'm missing?

Public Sub Run_Count_Click()

'// Set Ranges
Dim Cr_1, CR1_range, _
Cr_2, CR2_range, _
Cr_3, CR3_range, _
Cr_4, CR4_range, _
Cr_5, CR5_range _
As Range

'// Set Integers
Dim CR1, V1, CR1_Result, _
CR2, V2, CR2_Result, _
CR3, V3, CR3_Result, _
CR4, V4, CR4_Result, _
CR5, V5, CR5_Result, _
total_result, _
total_result2, _
total_result3, _
total_result4, _
total_result5 _
As Integer

'Set Strings
Dim V_1, V_2, V_3, V_4, V_5 As String

Dim ws As Worksheet



Set ws = Worksheets("database")

'// Get Criteria From Form And Search Database Headers
Set Cr_1 = ws.Cells.Find(What:=Me.Count_Criteria_1.Value, After:=Cells(1, 1), MatchCase:=False)

If Not Cr_1 Is Nothing Then

CR1 = Cr_1.Column '//Set CR1 as the Column in which the Criteria Header was found

Else
    MsgBox "Criteria 1 Has Not Been Found In The Database. Report Has Failed To Generate"
    Exit Sub
End If

'// Get Variable Value From Form And Set Shortcode
V_1 = Me.Criteria_1_Variable.Value


Set CR1_range = ws.Range(Cells(5, CR1), Cells(Rows.count, CR1))
CR1_Result = Application.CountIf(CR1_range, V_1)

If Me.Count_Criteria_2 = "Any" Then

Me.Count_Result.visible = True

Me.Count_Result.Value = "Based On Your Search Criteria Of:" & vbNewLine & _
"How many occurences of [" & Me.Criteria_1_Variable.Value & "] in the category [" & Me.Count_Criteria_1.Value & _
"] have occured between the dates..." & vbNewLine & vbNewLine & "The Results Are: " & CR1_Result

Exit Sub

代码在此行失败:

Set CR1_range = ws.Range(Cells(5, CR1), Cells(Rows.count, CR1))

谢谢提前人们

推荐答案

细胞从活动表中引用,而不是数据库。

The Cells are being referenced from the activesheet, not "database".

Set CR1_range = ws.Range(ws.Cells(5, CR1), ws.Cells(ws.Rows.count, CR1))

这篇关于运行代码时,VBA方法的对象范围_Worksheet失败了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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