简体   繁体   English

在Excel VBA中,如何找到一个子字符串,选择它下面的其余列,然后将所选区域移动到另一个工作簿?

[英]In Excel VBA, how do I find a substring, select the rest of the column below it, then move the selected area to another workbook?

UPDATE: I realized that A1 has a keyword I'm looking for, which I didn't see and caused the issue that came up. 更新:我意识到A1有一个我正在寻找的关键字,但我没有看到它,并导致出现问题。

I need to find a word - "P/position", "S/site", or "Residue," then copy everything in the column below this to a column in a sheet in a different workbook ("ExtractedColumns," "Sheet1"). 我需要找到一个单词-“ P / position”,“ S / site”或“ Residue”,然后将其下方的列中的所有内容复制到其他工作簿中工作表中的列中(“ ExtractedColumns”,“ Sheet1” )。 I was able to do all of this with the first two blocks of code in my macro - I was looking for different words and pasted to different columns, but it worked for those. 我可以使用宏中的前两个代码块来完成所有这些操作-我在寻找不同的单词并将其粘贴到不同的列中,但是它适用于那些。 And when I ran the following code on a different sheet, it worked. 当我在另一张纸上运行以下代码时,它起作用了。 However, the following code copies everything in column A starting from A2, but the substring it was supposed to locate was in column E- so it was supposed to copy from E5 and below. 但是,以下代码复制了A列中从A2开始的所有内容,但是它应该定位的子字符串位于E-列中,因此应该从E5及以下版本进行复制。 So why does this block of code select from a completely different column? 那么,为什么要从完全不同的列中选择此代码块呢? ("curr" is a range) (“ curr”是一个范围)

For Each curr In Range("A1:S10")
    If InStr(1, curr.Value, "residue") > 0 Or InStr(1, curr.Value, "Residue") > 0 Or InStr(curr.Value, "Position") > 0 Or InStr(curr.Value, "position") > 0 Or InStr(1, curr.Value, "Site") > 0 Or InStr(1, curr.Value, "site") > 0 Then
    Range(curr.Offset(1), Cells(Rows.Count, curr.Column).End(xlUp)).Copy Destination:=Workbooks("ExtractedColumns").Sheets("Sheet1").Range("G2")
        Exit For
    End If
    Set curr = curr.Offset(1)
Next

Just tested it, works fine for me. 刚刚测试过,对我来说效果很好。 Are you sure that A1 does not contain any of the strings you are searching for? 您确定A1不包含要搜索的任何字符串吗?

Two small things: if you want inStr to work case-insensitive, you can set the compare-parameter to vbTextCompare, eg 两件小事情:如果要让inStr不区分大小写,可以将compare-parameter设置为vbTextCompare,例如

InStr(1, curr.Value, "residue", vbTextCompare)

Secondly, what is the 6th line for? 第二,第六行是做什么用的? The for-loop iterates through all cells in the range, why do you manually change it to the next cell? for循环遍历该范围内的所有单元格,为什么您手动将其更改为下一个单元格?

暂无
暂无

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

相关问题 VBA我如何打开另一个工作簿? - VBA how do i open another workbook? 如何使用Excel VBA从使用文件浏览器选择的工作簿中复制工作表? - How do I use Excel VBA to copy a worksheet from a workbook selected using a file browser? Excel VBA - 如何清除另一个应用程序中另一个工作簿上的剪贴板? - Excel VBA - How do I clear the clipboard on another workbook in another application? 如何在Excel 2016的同一工作簿中将值从一个表移动到另一个表? - How do I move a value from one table to another in the same workbook in Excel 2016? 在 excel vba 中找到选中的列 - Find the selected column in the excel vba Excel VBA-如何在列中找到最大的子字符串值 - Excel VBA - how to find the largest substring value in a column 如何在excel中将范围从一个工作簿复制到另一个工作簿而不必在VBA中命名? - How do I copy a range from one workbook to another in excel WITHOUT having to name it in VBA? 如何使用 VBA 中的输入框从已打开的 Excel 工作簿中获取用户的范围并将其粘贴到另一个标准化工作簿中? - How do I use the Inputbox in VBA to obtain a range from a user from a already open Excel workbook and paste it into another, standardized workbook? 在 VBA 中查找选定区域的最后一行和一列 - Find last row and column in VBA for a selected area 按名称查找列标题并将所有数据移到列标题下方(Excel-VBA) - Find Column Header By Name And Move All Data Below Column Header (Excel-VBA)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM