[英]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.