Excelmacros在特定安排中search值可能吗?

这是我的困境,我需要为36张不同的专辑parsing一系列价值606周的Billboard 200排行榜。 这是我到目前为止…

https://docs.google.com/file/d/0B_tgNfDq0kXAakR5eHZ3bzJQVkk/edit?usp=sharing

广告牌只是redid他们的网站,所以现在Excel的webquery返回一个非常漂亮和干净的表。 我在工作表的A和B列中创build了两个公式,A有相关date的列表(特别是从2001年8月18日到本周的每个星期六),B根据date制作超链接到图表。 为了我的顾问的利益,图表的其余部分是颜色编码的,他们也将审查工作表。

我还手动将第一张图表(2001年8月18日)发布到自己的工作表中。 这个工作表没有被触及 – 它正是web查询返回的内容。

正如你所看到的,表格跨越到G列,每个条目占用3行。 我的重点在C列和D列。每个条目的D行都是(从上到下)标题,艺术家和印记|标签。 C列中的第一行包含了从1到200的那一周的位置。出现的模式是以4开头的每第三行(所以7,10,13 …)包含位置和专辑标题(col C &D,),每个以5开头的第三个单元格都包含一个艺术家。

我会尽量用简单的英文来解释我想象的,但要预先警告说这可能会失败。

所以这个macros将需要两个单元格作为input(他们甚至可以input?),专辑标题和相应的艺术家。 这个input应该告诉macros存储在每个单元中的string和所述单元的位置,例如E1,C2。 该macros应该循环遍历从列3到列608中的列A中的每个URL,将该URL查询成新的表格。 这个新的表格应该被激活,然后每个以4开始的第3行应该顺序search相册标题string。 find匹配项后,匹配的查询单元格下面一行的单元格应与艺术家名称string进行比较。 如果两个string都匹配其对应的查询单元格的内容,那么C列中的数字(从1到200)和与匹配的专辑标题单元格相同的行应被复制到与查询的URL对应的“bb200”表格中的单元格并search了专辑标题。 循环现在应该在序列中的下一个URL上重复出现。 在没有find匹配的情况下(该专辑没有在当周图表,或者BB返回了一张不起眼的表格),相应的单元格应该留空。 一旦URL列表被用尽,macros应该退出。

我的问题是双重的:首先,关于macros观的思维过程是否从根本上说是合理的? 第二 – 但是最重要的是,我并不知道哪里可以开始写VBA。 我学习了Java,C和最近的C ++(特别是OpenGL)。 我完全不熟悉VBA的语法和API,坦率地说,我的时间太短,无法正确地学习语言。 在此之后,我打算在短期内这样做,但是这个任务是在星期一到期的,我不知道这个任务最终会有多大的规模。

对于logging来说,macros不是分配,但要收集的数据是完成它的不可或缺的部分。 对那些好奇的人来说,这个任务是在星期一之前写出一份完整的高级论文草稿。 这些数据将被用来创build几个图表,我的顾问已经指示我包括我的写作。 这篇论文本身就是基于简单阅读每个专辑的销售业绩而写的。

你会帮助我走得更远,因为其他gradle生中的大多数人正在转向一些严重的半成品graphics表示。 唯一一个这样做的学生发明了一种仪器,并提供了原理图和autoCAD图纸。 但是,我离题了。

先谢谢您的帮助!!

我认为这应该会让你大概90%。 唯一不能做的就是networking查询。

对于这一部分,我build议你使用macroslogging器来做一个网页查询,然后在一个修订版本中发布这个代码,然后我们将它添加进来,并根据你的需要进行定制。 你必须做一些这方面的工作:)

Option Explicit Sub TestMacro() Dim inputVal As String Dim artistCell As Range Dim artistName As String Dim albumCell As Range Dim albumName As String Dim ws As Worksheet: Set ws = Sheets("thesisData") Dim r As Long 'this will be our row iterator variable Dim hLink As String 'string for each hyperlink in the iteration Dim wsNew As Worksheet 'this will be used when we create new worksheets Dim foundRange As Range 'this is how we will locate the album Dim weekRank As Long 'weekly rank from column C On Error GoTo InvalidRange 'This error handling is for the input box, to trap invalid arguments.' 'Use an input box to capture the cell address' inputVal = InputBox("Please enter the cell location containing the ARTIST name", "Input Range") Set artistCell = Range(inputVal) 'set a Range variable for the artist' artistName = artistCell.Value 'string variable for artist name' inputVal = vbNullString 'clear out the inputVal' 'Use an input box again...' inputVal = InputBox("Please enter the cell location containing the ALBUM name", "Input Range") Set albumCell = Range(inputVal) 'set a Range variable for the song cell' albumName = albumCell.Value 'string for song name' On Error GoTo 0 For r = 3 To 608 'iterate over rows 3 to 608 hLink = ws.Cells(1, r).Value 'Add a new sheet after the last sheet in this file' Set wsNew = Sheets.Add(After:=Sheets(ThisWorkbook.Sheets.Count)) wsNew.Name = Format(ws.Cells(r, 2).Value, "YYYY-MM-DD") '''' add VBA for web query, here.' '''' '''' try using the macro recorder and we can tweak it to your needs.' '''' '''' '''' 'Rather than looping over all the cells in web query...' Do 'Use the FIND method to look for matching album title in column D.' ' this uses exact text match, non-case-sensitive. Dim fnd Set foundRange = wsNew.Columns(4).Find(What:=albumName, After:=ActiveCell, LookIn:= _ xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _ xlNext, MatchCase:=True, SearchFormat:=False) If Not foundRange Is Nothing Then 'if we've found a match, then just offset by 1 row and check against artist name' If foundRange.Offset(1, 0) = artistName Then 'likewise, just offset the foundRange cell by -1 columns to get the weekly rank' weekRank = foundRange.Offset(0, -1) 'At this point I'm not sure what cell you want to put this value in, ' ' but I think you want row designated by "r" and the column of the ' ' album name, so we can do that like this: ws.Cells(r, albumCell.Column).Value = weekRank End If End If Loop While Not foundRange Is Nothing Next Exit Sub 'before error handling InvalidRange: 'error handling MsgBox inputVal & " is not a valid range", vbCritical, "Error!" End Sub 

祝你好运!

编辑这也假定每个networking查询只会有一个匹配。 如果不止一个,那只会返回最后一场比赛。 考虑到数据的性质,这似乎是一个安全的假设,但如果情况并非如此,请告诉我,我可以调整它。