我正在尝试使用VBA处理工作表中的某些行。我想从一张纸上裁剪和粘贴一组行到另一张纸上,努力想出我需要的ID码并选择最后一行来确定整个范围。使用VBA在Excel中选择多行
我想要的行由列A中的值标识,可以说该值在该单元格中为'Dept1',那么我需要在该单元格中复制该行和任何其他具有该值的行。
这些值将被粘贴到另一个表格中,我已经与标题一起创建了这些表格。
我的第一次尝试在此涉及按列A对数据进行排序,然后遍历数据,直到找到具有'Dept1'的第一个单元格,然后将此单元格地址放入一个变量中,但我还需要最后'dept1'的值,所以我可以得到这个行号。
我到目前为止的代码是这样的,只把第一个单元格地址中的变量,但还需要在最后一个单元格地址,然后创建我想选择和削减范围:
With wb.ActiveSheet
'Call sortorder sub to sort Department field
SortOrder
For i = 1 To lastcol
'find department named column
If .Range(ConvertToLetter(i) & 1).Value = "department" Then
For j = 2 To MaxRowCount
If .Range(ConvertToLetter(i) & j).Value = "Dept1" Then
'Get first cell address here to build range for 'dept1' data
firstRangeNumber = ConvertToLetter(i) & j
RHSCRange = firstRangeNumber & ":"
' Create code to populate LastRangeNumber variable as explained below
' work out how to get last cell address number with 'dept1'
' and use lastcol variable value combined with last cell row number(j)
' to create last cell address for range. Finally combine first and
' last variables to create complete range, select and cut range to
' New sheet
End If
Next
End If
Next
End With