搜索文本,复制偏移行 [英] Search for text, copy offset rows

查看:45
本文介绍了搜索文本,复制偏移行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个宏来读取.csv文件.它将数据放置在Sheet2中.

I wrote a macro to read a .csv file. It places the data in Sheet2.

我需要复制一个数据块,该数据块始于文本字符串"BP Error"之后的行.该字符串之前的数据的长度可以变化,但是我感兴趣的数据块的大小是固定的.我想将此数据块复制到Sheet1.

I need to copy a block of data that begins in the row that follows the text string "BP Error". The data that precedes this string can vary in length, but the block of data I'm interested in is of a fixed size. I'd like to copy this block of data into Sheet1.

我尝试执行一个子例程来搜索"BP错误",但尝试进行胶印复印时却一直陷入困境.我需要紧跟在"BP错误"字符串之后的接下来的18行数据(该错误始终在B列中).

I tried doing a sub-routine that would search for "BP Error" but I kept getting stuck trying to do Offset copying. I need the next 18 rows of data following that string of "BP Error" (which is always in Column B).

示例:在单元格B13中发现"BP错误",将单元格A14:G31复制到Sheet1

Example: "BP Error" is found in cell B13, copy cells A14:G31 to Sheet1

推荐答案

由于您只需查找第一个"BP错误",因此以下代码将为您完成工作(不要忘记更改工作表名称并检查范围以查看它们是否满足您的需求):

Since you have to find only the first "BP Error", the following code would do the job for you (Dont forget to change the worksheet names and check the ranges to see if they fit your needs):

Sub DoYourJob()

Dim readingRow As Long

Dim sourceSheet As Worksheet
Dim destinationSheet As Worksheet

Set sourceSheet = ThisWorkbook.Worksheets("YourSourceSheetName")
Set destinationSheet = ThisWorkbook.Worksheets("YourDestinationSheetName")

For readingRow = 1 To sourceSheet.Cells(sourceSheet.Rows.Count, 2).End(xlUp).Row

    If sourceSheet.Cells(readingRow, 2) = "BP Error" Then

        sourceSheet.Range(sourceSheet.Cells(readingRow + 1, 1), sourceSheet.Cells(readingRow + 18, 7)).Copy (destinationSheet.Cells(1, 1))
        Exit For

    End If

Next readingRow

End Sub

这篇关于搜索文本,复制偏移行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆