Excel 2007 VBA代码用于列表生成,但仅在某些情况下有效
我正在组装一个列表生成器,但在让代码正常工作(再次!)时遇到了问题 该代码适用于第一行数据,但忽略所有其他数据。 所有代码都一样,我有5个按钮。级别#存储在X列中,因此#24 我希望能够单击按钮并生成包含级别#的行列表,从PI表到列表表 将传输的数据是联系人信息和大小信息,与级别#位于同一行Excel 2007 VBA代码用于列表生成,但仅在某些情况下有效,excel,vba,excel-2007,Excel,Vba,Excel 2007,我正在组装一个列表生成器,但在让代码正常工作(再次!)时遇到了问题 该代码适用于第一行数据,但忽略所有其他数据。 所有代码都一样,我有5个按钮。级别#存储在X列中,因此#24 我希望能够单击按钮并生成包含级别#的行列表,从PI表到列表表 将传输的数据是联系人信息和大小信息,与级别#位于同一行 与其逐行循环,为什么不使用过滤器呢?我是个新手,我不知道如何:(赞成/反对?宏录制器是你的朋友。调用一个sub而不是重复代码5次。lastrow的值是多少?我希望能够选择具体的级别1、2、3、4或5,而不从
与其逐行循环,为什么不使用过滤器呢?我是个新手,我不知道如何:(赞成/反对?宏录制器是你的朋友。调用一个sub而不是重复代码5次。lastrow的值是多少?我希望能够选择具体的级别1、2、3、4或5,而不从其他级别获得结果。将它们组合在一个sub下不意味着它们都完成了吗?lastrow是为了确保它粘贴在最后一行的下方用过。
Private Sub CommandButton2_Click() 'level 1 button
Dim LastRow As Long
Dim i As Long, j As Long
'Find the last used row in a Column: column A in this example
With Worksheets("PI")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
'first row number where you need to paste values in Sheet1'
With Worksheets("Lists")
j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
End With
For i = 1 To LastRow
With Worksheets("PI")
If .Cells(i, 24).Value = "1" Then
.Rows(i).Copy Destination:=Worksheets("Lists").Range("A" & j)
j = j + 1
End If
End With
Next i
End Sub
Private Sub Level5_Click()
Dim LastRow As Long
Dim i As Long, j As Long
'Find the last used row in a Column: column A in this example
With Worksheets("PI")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
'first row number where you need to paste values in Sheet1'
With Worksheets("Lists")
j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
End With
For i = 1 To LastRow
With Worksheets("PI")
If .Cells(i, 24).Value = "5" Then
.Rows(i).Copy Destination:=Worksheets("Lists").Range("A" & j)
j = j + 1
End If
End With
Next i
End Sub
Private Sub Lvl2_Click()
Dim LastRow As Long
Dim i As Long, j As Long
'Find the last used row in a Column: column A in this example
With Worksheets("PI")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
'first row number where you need to paste values in Sheet1'
With Worksheets("Lists")
j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
End With
For i = 1 To LastRow
With Worksheets("PI")
If .Cells(i, 24).Value = "2" Then
.Rows(i).Copy Destination:=Worksheets("Lists").Range("A" & j)
j = j + 1
End If
End With
Next i
End Sub
Private Sub Lvl3_Click()
Dim LastRow As Long
Dim i As Long, j As Long
'Find the last used row in a Column: column A in this example
With Worksheets("PI")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
'first row number where you need to paste values in Sheet1'
With Worksheets("Lists")
j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
End With
For i = 1 To LastRow
With Worksheets("PI")
If .Cells(i, 24).Value = "3" Then
.Rows(i).Copy Destination:=Worksheets("Lists").Range("A" & j)
j = j + 1
End If
End With
Next i
End Sub
Private Sub Lvl4_Click()
Dim LastRow As Long
Dim i As Long, j As Long
'Find the last used row in a Column: column A in this example
With Worksheets("PI")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
'first row number where you need to paste values in Sheet1'
With Worksheets("Lists")
j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
End With
For i = 1 To LastRow
With Worksheets("PI")
If .Cells(i, 24).Value = "4" Then
.Rows(i).Copy Destination:=Worksheets("Lists").Range("A" & j)
j = j + 1
End If
End With
Next i
End Sub