简体   繁体   English

查找不在工作表中的范围内的最后一个空单元格

[英]Find last empty cell in a range not in the sheet

I need to loop through a range of rows and end loop at last empty cell in that range.我需要遍历一系列行并在该范围内的最后一个空单元格处结束循环。 I know im not supposed to use End(xlUp) as it goes tot the end of the sheet I need to find the last full cell after a certain cell and stop there (in the picture it is cell c4)我知道我不应该使用 End(xlUp) 因为它走到了工作表的末尾我需要在某个单元格之后找到最后一个完整的单元格并停在那里(在图片中它是单元格 c4)

在此处输入图像描述

Below is my code, i need help defining the "Last Cell"下面是我的代码,我需要帮助定义“最后一个单元格”

Sub GS()
dim LastCell as Long
LastCell
ThisWorkbook.Worksheets("Service ID - Details").Visible = True

Dim myRange As Range: Set myRange = Worksheets(1).Range("F8:G11")

For i = 5 To LastCell
newname = ThisWorkbook.Worksheets("3. Functional Scope - 2").Cells(i, 3).Value
servicename = ThisWorkbook.Worksheets("3. Functional Scope - 2").Cells(i, 4).Value
c = 0
For Each ws In Worksheets
If ws.Name = newname Then
c = 1
End If
Next ws
If c = 1 Then
GoTo Last
End If

Sheets("Service ID - Details").Select
Sheets("Service ID - Details").Copy After:=Sheets(Sheets.Count)
Sheets("Service ID - Details (2)").Select
Sheets("Service ID - Details (2)").Name = newname
ActiveSheet.Range("D1").Value = newname
ActiveSheet.Range("D2").Value = servicename
Last:
Next
ThisWorkbook.Worksheets("Service ID - Details").Visible = False
ThisWorkbook.Worksheets("3. Functional Scope - 2").Activate
ActiveSheet.Range("C5").Select

End Sub

I would suggest just looping through all cells in the range and conditioning the loop on if the cell is empty or not ie我建议只循环遍历范围内的所有单元格,并根据单元格是否为空来调节循环,即

Dim rng As Range: Set rng = 'your range, also just set it as the range you saved from before
Dim cel As Range
For Each cel In rng.Cells
If cel.Value = vbNullString then
GoTo EndLoop
End If
With cel
    'your operation you want to loop
End With
EndLoop:
Next cel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM