简体   繁体   中英

VBA: Search for multiple possible values in row and then copy range

I am very new to VBA and would appreciate your help. I have a huge dynamic database (companies and their quarterly ratings), which gets updated every quarter. The database contains about 70 columns (ratings and other information) and more than 3000 rows (the companies). The column order and rows are subject to changes since I don't have any influence on the source file. My goal is to not only copy and paste the companies' names (according to the header "Company"), but also all the past ratings AND the new rating, which will always be in a new column between the most recent rating and other columns (with information I don't need).

Please see my example (with random numbers): https://www.dropbox.com/s/98n8ty5mrt7etwd/Screenshot2.PNG

There you can see

  • the company names (with the blue header)
  • the past ratings (in green) and the position of the new rating right next to them and
  • additional information (in orange) that is not needed

In order not to have to adapt the macro manually every quarter I would like to have a macro that searches for any header (with the format eg "Q1- 07") indicating the quarter and year ("Q1", "Q2", "Q3", or "Q4") and then copy these ratings to another worksheet (including the company names).

I know how to search for a heading with a specific name (like in the case of the "Company" heading) but I don't know how to integrate a Find function that takes multiple possible values (the 4 quarters) into account.

So far my manual solution would be like that but as I said I would prefer a more comfortable solution that recognizes the new quarters automatically:

Worksheets("UpdatedQuarterlyInput").Activate
Range("A1").Select
Worksheets("UpdatedQuarterlyInput").rows("1:1").Find(What:="Q1 - 07", _ 
After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
Set Rng = ActiveCell.Range(Cells(2, 1), Cells(LastRow_UpdatedQuarterlyInput, 1))
Rng.Select
Selection.Copy
Worksheets("Output").Activate
Worksheets("Output").Range("A2").PasteSpecial Paste:=xlPasteValues

Thanks a lot!!

Felix

I'm not sure I understand your problem fully- hopefully I'm not on the wrong track with this idea.

A quick and dirty solution would be to create a dropdown menu somewhere on your destination sheet using Data Validation on the Data tab which is populated by all the Quarter headings in the source document ( Data -> Data Validation ->choose List from the Allow field, and then select the source document Quarter headers for the Formula field). Have the formula go one column past the last Quarter column so it will automatically expand when you add a column in the middle.

Then add a button that will execute your Sub as written above, but populate the What: parameter of your Find method with the contents of your dropdown menu. Now all you have to do is select the quarter you want from the dropdown menu (it will already be there if you created the Data Validation correctly) and click your button. Done.

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