[英]find the first "To Date", select the cells under "To Date" paste the value in the previous cells then go to the next To Date
I need your help in order to fix this code.我需要您的帮助才能修复此代码。 Objective of this code is set a range.这段代码的目的是设置一个范围。 find the first "To Date", then select all the cells under "To Date" paste the value in the previous cells (for example the first "To Date" is in cell F4 contains value from F5:F"N"(N=last row) then paste F5:F"N" values in E5:E"N", then go to the next To Date.找到第一个“To Date”,然后选择“To Date”下的所有单元格,将值粘贴到前面的单元格中(例如,第一个“To Date”在单元格 F4 中包含来自 F5:F"N"(N=最后一行)然后将 F5:F"N" 值粘贴到 E5:E"N" 中,然后转到下一个 To Date。
The problem I am facing in this code is that我在这段代码中面临的问题是
the Code does not select the last row in under the "To Date" (except for the first time)代码不选择“To Date”下的最后一行(第一次除外)
the code runs in infinite loop does not stop after the last "To Date"代码在无限循环中运行不会在最后一个“To Date”之后停止
Sub FindAddressColumn() Dim twb As ThisWorkbook Dim ws As Worksheet Dim lastRow As Long Dim LastCol As Long Dim lr As Long Dim getLastCell As Range Dim firstAddress As String Dim rngAddress As Range Const strFindMe As String = "To Date" Set twb = ThisWorkbook For Each ws In twb.Worksheets If ws.Name = "QCR Summary" Then lastRow = ws.Cells.Find("*", ws.Cells(1, 1), xlFormulas, xlPart, xlByRows, _ xlPrevious).Row LastCol = ws.Cells.Find("*", ws.Cells(1, 1), xlFormulas, xlPart, xlByColumns, _ xlPrevious).Column Set getLastCell = ws.Cells(lastRow, LastCol) With ws.Range("A1", getLastCell) Set rngAddress = .Find(What:=strFindMe, LookIn:=xlValues) If rngAddress Is Nothing Then Exit Sub End If firstAddress = rngAddress.Address Do Set rngAddress = .FindNext(rngAddress) Range(rngAddress, rngAddress.End(xlDown)).Select 'MsgBox rngAddress.Address Loop While Not rngAddress Is Nothing And rngAddress <> firstAddress End With End If Next ws End Sub
Because you named your variable rngAddress
the names suggests that the variable contains an address string while it actually contains a Range
object.因为您将变量命名为rngAddress
所以名称表明该变量包含一个地址字符串,而它实际上包含一个Range
对象。
Then you compare rngAddress <> firstAddress
but if you have a look at your variable declarations然后你比较rngAddress <> firstAddress
但如果你看看你的变量声明
Dim firstAddress As String
Dim rngAddress As Range
You see that you compare a Range
object with a String
which cannot work properly.您会看到将Range
对象与无法正常工作的String
进行比较。 Because rngAddress
is a range object it defaults to rngAddress.Value
so you actually compare the value of the cell rngAddress
with an address string firstAddress
.因为rngAddress
是一个范围对象,它默认为rngAddress.Value
所以你实际上将单元格rngAddress
的值与地址字符串firstAddress
。
Replace代替
Loop While Not rngAddress Is Nothing And rngAddress <> firstAddress
with和
Loop While rngAddress.Address <> firstAddress
Note that you can omit the part Not rngAddress Is Nothing
here in the loop because that can never happen.请注意,您可以省略循环中的Not rngAddress Is Nothing
部分,因为这永远不会发生。 If it would be Nothing
then it would have already Exit Sub
in the step earlier where you checked If rngAddress Is Nothing Then
.如果它是Nothing
那么它就已经在之前检查If rngAddress Is Nothing Then
的步骤中Exit Sub
了。
Also Dim twb As ThisWorkbook
this should error because it must be Dim twb As Workbook
.另外Dim twb As ThisWorkbook
这应该会出错,因为它必须是Dim twb As Workbook
。
Finally your loop is a bit unnecessary, because you can access your sheet named QCR Summary
directly without looping throug all worksheets.最后,您的循环有点不必要,因为您可以直接访问名为QCR Summary
工作表,而无需循环遍历所有工作表。 Which would be much quicker:哪个会更快:
Option Explicit
Public Sub FindAddressColumn()
Const strFindMe As String = "To Date"
Dim twb As Workbook
Set twb = ThisWorkbook
Dim ws As Worksheet
On Error Resume Next
Set ws = twb.Worksheets("QCR Summary")
On Error GoTo 0
If ws Is Nothing Then
MsgBox "Worksheet 'QCR Summary' does not exist."
Exit Sub
End If
Dim lastRow As Long
lastRow = ws.Cells.Find("*", ws.Cells(1, 1), xlFormulas, xlPart, xlByRows, _
xlPrevious).Row
Dim LastCol As Long
LastCol = ws.Cells.Find("*", ws.Cells(1, 1), xlFormulas, xlPart, xlByColumns, _
xlPrevious).Column
Dim getLastCell As Range
Set getLastCell = ws.Cells(lastRow, LastCol)
With ws.Range("A1", getLastCell)
Dim rngAddress As Range
Set rngAddress = .Find(What:=strFindMe, LookIn:=xlValues)
If rngAddress Is Nothing Then
Exit Sub
End If
Dim firstAddress As String
firstAddress = rngAddress.Address
Do
Set rngAddress = .FindNext(rngAddress)
Range(rngAddress, rngAddress.End(xlDown)).Select
'MsgBox rngAddress.Address
Loop While rngAddress.Address <> firstAddress
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.