简体   繁体   中英

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM