[英]Trying to find the value of a cell in column b of the last row in sheet1
I need to find the value of the last cell in column b of sheet1.我需要在 sheet1 的 b 列中找到最后一个单元格的值。 This value will change weekly.
该值将每周更改。 I then want to take that value and find it in sheet2.
然后我想取那个值并在 sheet2 中找到它。 Then I want to copy and paste all data below this found value to sheet3.
然后我想将低于这个找到值的所有数据复制并粘贴到 sheet3。 I can't get past the first part with the following code:
我无法通过以下代码的第一部分:
Dim cell As Range
Dim rangefound As Integer
Dim lastRow As Long
lastRow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
Set cell = Range("B:B").Find("rangefound")
rangefound = lastRow = Cells(lastRow, 2).Value
I've been struggling with the syntax for a month and really don't know what I'm doing.我已经在语法上苦苦挣扎了一个月,真的不知道我在做什么。
try this尝试这个
Sub test()
Dim cell As Range
Dim rangefound As Integer
Dim lastRow As Long
lastRow = Sheet1.Range("B" & Rows.Count).End(xlUp).Row
rangefound = Sheet1.Cells(lastRow, 2).Value
Set cell = Sheet2.Range("B:B").Find(rangefound)
MsgBox "The value was found in Sheet2!" & cell.Address
End Sub
The issues with your code were您的代码的问题是
rangefound
before it had a value, ie the order of the commandsrangefound
,即命令的顺序"rangefound"
as a text instead of the variable"rangefound"
作为文本而不是变量rangefound
rangefound
赋值的语法错误Edit: To extend the code to copy the data below the found value to another sheet, use Offset to reference one row below.编辑:要扩展代码以将找到的值下方的数据复制到另一张表,请使用 Offset 引用下面的一行。 There are many different ways to do this, so using Offset is just one option.
有许多不同的方法可以做到这一点,所以使用 Offset 只是一种选择。
Here is the complete code这是完整的代码
Sub test()
Dim mycell As Range, RangeToCopy As Range
Dim rangefound As Integer
Dim lastRow As Long
lastRow = Sheet1.Range("B" & Rows.Count).End(xlUp).Row
rangefound = Sheet1.Cells(lastRow, 2).Value
' this is the cell with the found value
Set mycell = Sheet2.Range("B:B").Find(rangefound)
' now find the last row in Sheet2. We can use lastRow again,
' since it is no longer needed elsewhere
lastRow = Sheet2.Range("B" & Rows.Count).End(xlUp).Row
' set the range to copy to start one cell below rangefound
' to the end of the data in column B
Set RangeToCopy = Sheet2.Range(cell.Offset(1, 0), Sheet2.Cells(lastRow, "B"))
' copy the range and paste into Sheet3, starting at A1
RangeToCopy.Copy Sheet3.Range("A1")
End Sub
Note: I changed the variable name from "cell" to "mycell".注意:我将变量名从“cell”更改为“mycell”。 It's better to use variable names that cannot be mistaken for Excel artifacts.
最好使用不会被误认为 Excel 工件的变量名称。
Another edit: If you need to paste into the next free row in Sheet3, use the techniques already established.另一个编辑:如果您需要粘贴到 Sheet3 中的下一个空闲行,请使用已经建立的技术。
[...]
Set RangeToCopy = Sheet2.Range(cell.Offset(1, 0), Sheet2.Cells(lastRow, "B"))
' get the next available row in Sheet3
lastRow = Sheet3.Range("A" & Rows.Count).End(xlUp).Row + 1
' copy and paste
RangeToCopy.Copy Sheet3.Range("A" & lastRow)
[...]
Note that I'm using the same variable for three different purposes.请注意,我将相同的变量用于三种不同的目的。 If that is too confusing, create three distinct variables instead.
如果这太令人困惑,请创建三个不同的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.