我正在处理一个宏,用于将不同位置的行复制到特定于主工作表中位置的工作表。
除了在我检查的单元格中包含“0
”并且显示为空字符串匹配时查找最后一行,我才能正常工作。我需要找到一种更好的方法来粘贴到第一个空行,或者找出被检查的单元格是否真的为空。
这是宏代码:
Sub MoveDataToSheets()
'
' MoveDataToSheets Macro
' Macro written 2/25/2011 by Jim Snyder
'
Dim rowCount As Integer, sheetIndex As Integer, LastRow As Integer
Dim ExcelLastCell As Range
' Prevent screen updates from slowing execution
Application.ScreenUpdating = False
rowCount = ActiveCell.CurrentRegion.Rows.Count
' Process each row once copying row to matching location tab
For currentRow = 1 To rowCount
' Determine which sheet the row goes to
Select Case (Cells(currentRow, "B").Value)
Case "ALTAVISTA"
sheetIndex = 2
Case "AN"
sheetIndex = 3
Case "Ballytivnan"
sheetIndex = 4
Case "Casa Grande"
sheetIndex = 5
Case "Columbus - Devices (DE)"
sheetIndex = 6
Case "Columbus - Nutrition"
sheetIndex = 7
Case "Fairfield"
sheetIndex = 8
Case "Granada"
sheetIndex = 9
Case "Guangzhou"
sheetIndex = 10
Case "NOLA"
sheetIndex = 11
Case "Process Research Operations (PRO)"
sheetIndex = 12
Case "Richmond"
sheetIndex = 13
Case "Singapore"
sheetIndex = 14
Case "Sturgis"
sheetIndex = 15
Case "Zwolle"
sheetIndex = 16
Case Else
sheetIndex = 1
End Select
' Only if the row cotains a valid location, copy it to location sheet
If (sheetIndex > 1) Then
Sheets(1).Activate ' Activate the sheet being copied from
ActiveSheet.Rows(currentRow).Copy ' Copy from master sheet
Set sheet = Worksheets(sheetIndex) ' Designate target sheet
Set ExcelLastCell = sheet.Cells.SpecialCells(xlLastCell) ' Find the last used row
LastRow = ExcelLastCell.Row
If (sheet.Rows(LastRow).Cells(LastRow, 5).Value = "") Then
sheet.Paste Destination:=sheet.Cells(LastRow, 1) ' Paste into first row
Else
sheet.Paste Destination:=sheet.Cells(LastRow + 1, 1) ' Paste in first empty row
End If
Sheets(1).Activate ' Activate the sheet being copied from
End If
Next
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:0)
Chip Pearson的网站有一个你需要的例子。您可以在粘贴之前在每个页面上执行此操作。 Finding The Last Used Cell In A Range
答案 1 :(得分:0)
假设您使用Excel 2003和/或您在任何工作表上都不会超过65,536:
LastRow = Sheet.Range("A65536").End(xlUp).Row
答案 2 :(得分:0)
仅查找列中具有值的最后一个单元格:
Dim LastRow As Long
LastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
然后,您可以使用LastRow
作为标准行引用。我经常使用它来找到最后一行然后加1,这实际上给了我该列中的第一个空单元格。例如:
Cells(LastRow + 1, 6).Value = Cells(10, 3).Value 'Or whatever value you want
如果您正在寻找更复杂的解决方案,上面由datatoo链接的CPearson网站会列出更多查找“最后一个单元格”的方法。
编辑:刚测试确定:此 检测列中的0或nulls(=“”)。