Excel 将行的分组样式从一张工作表复制到另一张工作表时出现错误424“需要对象”
我创建了一个userform,它有一个列表框(Listbox1),列出了工作簿中的所有工作表名称。我有一个选择框(选择框1)。 我已经设置了Excel 将行的分组样式从一张工作表复制到另一张工作表时出现错误424“需要对象”,excel,vba,Excel,Vba,我创建了一个userform,它有一个列表框(Listbox1),列出了工作簿中的所有工作表名称。我有一个选择框(选择框1)。 我已经设置了选项框1。value=ActiveWorkbook.ActiveSheet 因此,我基本上希望从列表框中选择任何工作表,该工作表的分组行格式应复制到我的activesheet中 我在以下行中得到错误424: lastrow = Me.ListBox1.List(i).UsedRange.Row + Me.ListBox1.List(i).UsedRange.
选项框1。value=ActiveWorkbook.ActiveSheet
因此,我基本上希望从列表框中选择任何工作表,该工作表的分组行格式应复制到我的activesheet中
我在以下行中得到错误424:
lastrow = Me.ListBox1.List(i).UsedRange.Row + Me.ListBox1.List(i).UsedRange.Rows.Count - 1
请在下面找到我的代码:
Dim sh As Variant
Dim ws As Worksheet
Dim j As Long
Dim i As Long
Dim lastrow As Long
Private Sub CommandButton1_Click()
If Me.OptionButton1.Value = True Then
Set ws = ActiveWorkbook.ActiveSheet
Me.OptionButton1.Value = ws
For i = 0 To Me.ListBox1.ListCount - 1
If Me.ListBox1.Selected(i) Then
lastrow = Me.ListBox1.List(i).UsedRange.Row + Me.ListBox1.List(i).UsedRange.Rows.Count - 1
For j = Me.ListBox1.List(i).UsedRange.Row To lastrow
ws.Rows(j).OutlineLevel = Me.ListBox1.List(i).Rows(j).OutlineLevel
Next
End If
Next
End If
End Sub
Private Sub UserForm_Initialize()
'for each loop the add visible sheets
For Each sh In ActiveWorkbook.Sheets
'add sheets to the listbox
Me.ListBox1.AddItem sh.Name
Next sh
End Sub
如果Me.ListBox1.List(i)
是工作表的名称,则需要使用工作表(Me.ListBox1.List(i)).UsedRange
而不是Me.ListBox1.List(i).UsedRange
您可能需要将代码更改为:
Dim sh As Variant
Dim ws As Worksheet
Dim j As Long
Dim i As Long
Dim lastrow As Long
Private Sub CommandButton1_Click()
If Me.OptionButton1.Value = True Then
Set ws = ActiveWorkbook.ActiveSheet
Me.OptionButton1.Value = ws.Name ' Set the value to the NAME of the worksheet
For i = 0 To Me.ListBox1.ListCount - 1
If Me.ListBox1.Selected(i) Then
With Worksheets(Me.ListBox1.List(i))
lastrow = .UsedRange.Row + .UsedRange.Rows.Count - 1
For j = .UsedRange.Row To lastrow
ws.Rows(j).OutlineLevel = .Rows(j).OutlineLevel
Next
End With
End If
Next
End If
End Sub
Private Sub UserForm_Initialize()
'for each loop the add visible sheets
For Each sh In ActiveWorkbook.Sheets
'add sheets to the listbox
Me.ListBox1.AddItem sh.Name
Next sh
End Sub
它起作用了,谢谢。现在我将尝试在所有的表单中应用它,以防我被卡住,这将提出另一个问题。