'方法'价值'对象'范围'失败的错误

时间:2017-08-22 20:59:48

标签: excel-vba vba excel

我有一些vba代码,应该允许用户使用添加连续段ID(1,2,3,...)的表单将学习段添加到列表中。该表单还允许用户从列表框中选择那些添加的项目,并在必要时删除段。 VBA代码应该删除所选项目并从1开始重新编号列表。但是,我得到了 '方法'值'对象'范围'当我尝试将第一项的值重置为1而ws是工作表变量时失败 错误。我不是程序员,所以这不是最美丽的代码......希望有人可以告诉我为什么我会获得这种方法的价值'对象'范围'失败'下面代码中粗体点的错误。

这里是完整的代码:

Private Sub CommandButton_RemSegList_Click()   

Dim sFind As String
Dim rFound As Range
Dim ws As Worksheet
Dim ws2 As Worksheet
Dim irow As Long
Dim rn As Range
Dim rng As Range
Dim k As Long
Dim SegIDRng As Range


Set ws = ThisWorkbook.Sheets("Learning Segments")
Set ws2 = ThisWorkbook.Sheets("Course Summary")

'identify the segement ID ofs the selected segment in the list box on the Learning Segment tab
Select Case Me.ListBox_Segments.Value
Case Is <> vbNullString
    sFind = Me.ListBox_Segments.Value

MsgBox "Selected Seg ID is " + sFind

'find the selected Seg ID in the Learning Segments sheet and clear the contents if found
    With ws
        Set rFound = .Columns(1).Find(What:=sFind, After:=.Cells(1, 1))

 MsgBox rFound

        If Not rFound Is Nothing Then
            rFound.EntireRow.ClearContents 'shift:=xlUp
        End If
    End With
Case Else: Exit Sub
End Select

MsgBox "The Segment ID" + rFound + "has been cleared"

'find the first cell with a value in column A of Learning Segments sheet starting a row 1, col 1 and add 1 row to account for the header.

With ws
.Visible = True
irow = ws.Range("A:A").Find(What:="", After:=.Cells(1, 1), SearchOrder:=xlRows, _
SearchDirection:=xlNext, LookIn:=xlValues).row + 1

MsgBox irow

.Range(.Cells(irow, 1), .Cells(26, 8)).Copy _
Destination:=ws.Cells(irow - 1, 1)

.Range(.Cells(2, 2), .Cells(26, 8)).Copy _
Destination:=ws2.Cells(23, 1)

Set rn = ws.UsedRange
k = rn.Rows.Count + rn.row - 1

MsgBox k

End With

Set SegIDRng = ws.Range(ws.Cells(2, 1), ws.Cells(k, 1))

MsgBox "segIDRng has been set"

If k = 1 Then

Else
    If k = 2 Then
        **ws.Range("A2").Value = 1**

        MsgBox "k = 2"

    Else
        MsgBox " k does not equal 1 or 2, so set A2 in Learning segments sheet to 1"
        **ws.Range("A2").Value = 1**
        Set rng = ws.Range("A2")
        rng.AutoFill Destination:=SegIDRng, Type:=xlFillSeries
    End If
End If

ws.Visible = False

End Sub

0 个答案:

没有答案