简体   繁体   English

是否有VBA /宏代码用于在工作表中使用粗体文本查找和选择第一个单元格?

[英]Is there a VBA/macro code for locating and selecting the first cell in a worksheet with bold text?

I receive a report at work from one of our vendors. 我收到了一位供应商的工作报告。 The report is actually a series of reports that are all different formats that they put into excel vertically. 该报告实际上是一系列报告,它们都是垂直放入Excel的不同格式。 What I am looking to do is parse out each of these reports into a separate tab and clean up the excess rows/columns because in the current format it's just really ugly. 我要做的是将每个报告解析成一个单独的选项卡并清理多余的行/列,因为在当前格式中它真的很难看。

I've found bits and pieces that I've cobbled together to delete out excess rows and columns once I have the data on separate tabs but the part I'm struggling with is parsing out each report into it's own tab. 我已经发现了一些碎片,我拼凑起来删除多余的行和列,一旦我将数据放在单独的选项卡上,但我正在努力解决的部分是将每个报告解析为它自己的选项卡。 Below is what I tried which works great for reports with the same header but when you get part way through the overall report the headers change. 下面是我尝试过的,它适用于具有相同标题的报表,但是当您在整个报表中获得部分时,标题会发生变化。 The only thing that each report on this worksheet seems to have in common is that the title of the report is in bold text and then everything below, until the next report starts, is not bold. 这个工作表上每个报告的唯一共同点就是报告的标题是粗体文字,然后下面的所有内容,直到下一个报告开始,都不是粗体。 Is there any way to edit the below so that the range it finds goes from the first row with bold text to the next row with bold text, less one? 有没有办法编辑下面的内容,以便它找到的范围从带有粗体文本的第一行到带有粗体文本的下一行,少一个?

Dim findrow As Long, findrow2 As Long
findrow = Range("A:A").Find("Employee Updates", Range("A1")).Row
findrow2 = Range("A:A").Find("Employee Updates", Range("A" & 
findrow)).Row
Range("A" & findrow & ":BG" & findrow2 - 1).Select
'selects rows between employee updates header'

Selection.Cut 'cuts and pastes to a new tab'
Sheets.Add After:=ActiveSheet
ActiveSheet.Paste[enter image description here][1]

First of all, don't use "Selection" on your code. 首先,不要在代码上使用“选择”。 It is a bad practice and can give you a lot of different problems. 这是一种不好的做法,可以给你很多不同的问题。

What I would suggest for you: 我会建议你:

   Dim initialRow As Long, endRow As Long
   'starts on line 1 and goes on
   initialRow = 1
   'checks for bold in column 1
   Do While Cells(initialRow,1).Font.Bold = False
     initialRow = initialRow + 1
   Loop
   endRow = initialRow+1
   Do While Cells(endRow+1,1).Font.Bold = False
     endRow = endRowRow + 1
   Loop
   Range("A" & initialRow & ":BG" & endRow).Cut
   Sheets.Add After:=ActiveSheet
   ActiveSheet.Paste

This will get you the area between one bold and the next one, assuming they are on column one, otherwise you might have to change it. 这将为您提供一个粗体和下一个之间的区域,假设它们位于第一列,否则您可能需要更改它。 Note that this will need to be adapted in case you are looping through all the cells, specially for avoiding an error in the last report, where there won't be any bold cells to tell the program to stop looking for them 请注意,这将需要进行调整,以防您循环遍历所有单元格,特别是为了避免上一个报告中的错误,其中没有任何粗体单元格告诉程序停止查找它们

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

相关问题 Excel VBA宏:找到列中的第一个空单元格并自动填充它 - Excel VBA macro: Locating first empty cell in a column and automatically filling it Excel VBA 宏在单元格中用粗体文本替换 Html 粗体标记 - Excel VBA Macro Replace Html Bold Tag With Bolded Text In Cell VBA宏可在单元格范围内查找特定文本并将其设置为粗体 - VBA Macro to find specific text within cell range and style it bold VBA:激活/选择工作表/行/单元格 - VBA: activating/selecting a worksheet/row/cell 根据 Excel VBA 中的单元格值选择工作表 - Selecting a worksheet based on a cell value in Excel VBA 将单元格数据从一个工作表放到另一个包含文本的工作表的VBA代码是什么? - What is the VBA code for putting cell data from one worksheet to another worksheet that includes text? VBA合并单元格和第二个单元格的“粗体”文本 - VBA merge cells and **Bold** text of second cell 如何使用VBA代码将带有项目符号,粗体字符和换行符的文本粘贴到单个excel单元格中? - How to paste text with bullets, bold characters and break lines into a single excel cell using VBA code? VBA宏可以根据源工作表单元格范围更新工作表 - VBA macro that can update the worksheets based on a source worksheet cell range Excel VBA宏:将相对单元格复制到另一个工作表 - Excel VBA Macro: Copying relative cell to another worksheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM