简体   繁体   English

试图在 sheet1 的最后一行的 b 列中查找单元格的值

[英]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您的代码的问题是

  • using rangefound before it had a value, ie the order of the commands在有值之前使用rangefound ,即命令的顺序
  • using "rangefound" as a text instead of the variable使用"rangefound"作为文本而不是变量
  • wrong syntax to assign a value to rangefoundrangefound赋值的语法错误
  • not qualifying which sheet should be searched不限定应该搜索哪个工作表

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.

相关问题 在Sheet1中找到与Sheet2中的行数相同的行,并将文本添加到单元格值 - Find the row in Sheet1 with the same number of row in Sheet2 and add text to cell value 将行值从 sheet1 复制到 sheet2 列 - Copy row value from sheet1 to sheet2 column 将 sheet2 第一行的最后一列与 sheet1 的 F2 单元格进行比较,如果匹配,则显示 msgbox,否则将 F2 范围粘贴到 sheet2 - Comparing last column of first row in sheet2 with F2 cell of sheet1 if it matches then show msgbox or else copy F2 range paste to sheet2 如果工作表sheet2中的值与工作表sheet1中的标头的值匹配的Vba代码,从工作表sheet1检索整列 - Vba code to retrieve an entire column from sheet1 if the value in a cell of sheet2 matches the value of a header in sheet1 在Sheet1中找到一个单元格,然后将其所在的整个行复制到Sheet2中的第一个空行 - Find A Cell In Sheet1 Then Copy The Entire Row That It Is In To The First Empty Row in Sheet2 我想在sheet2上找到最后一行,然后将每个单元格移到sheet1上的定义位置 - I would like to find last line on sheet2 and then be able to move each cell to a defined location on sheet1 在行中查找最后一个条目,然后在该列的顶部行中返回单元格的值 - Find Last Entry in Row, then Return Value of Cell in Top Row of That Column 确定Sheet1的A列中的任何值是否与Sheet2的每一行中的任何值匹配 - Determine if any value in Sheet1, Column A, matches any value in each row of Sheet2 将数据从 Sheet1 复制到 Sheet2,当列中的值更改时插入行 - Copy Data From Sheet1 to Sheet2, Inserting Row When Value in Column Changes Excel宏查找列B中的最后一行,选择另一个单元格A1,将公式从A1拖动到B中的最后一行 - Excel Macro Find Last Row In Column B, Select another cell A1, drag formula from A1 to last row in B
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM