0

Here's my dilemma, I need to parse a series of 606 weeks worth of Billboard 200 charts for the position of 36 different albums. Here's what I've got so far...

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

Billboard just redid their website, so now Excel's webquery returns a very pretty and clean table. I created two formulas in columns A and B of my worksheet, A has the list of relevant dates (specifically every Saturday from 8/18/2001 until this week), and B makes hyperlinks to the charts based on the dates. The rest of the chart is color-coded for the benefit of my advisors, who will also be reviewing the sheet.

我还手动将日期为 2001-08-18 的第一个图表通过网络查询到了它自己的工作表中。此工作表尚未被触及 - 它正是 web 查询返回的内容。

如您所见,该表跨越到 G 列,每个条目占用 3 行。我的重点是 C 列和 D 列。每个条目的 D 列行是(从上到下)标题、艺术家和印记|标签。每个条目的 C 列的第一行包含该周从 1 到 200 的位置。出现的模式是每 3 行以 4 开头(所以 7、10、13...)包含一个位置和专辑标题(col C & D, resp.),并且每个以 5 开头的第 3 个单元格包含一个艺术家。

我将尝试用简单的英语解释我的想象,但请注意,这可能会惨遭失败。

So the macro would take two cells as input (can they even take input?), album title and corresponding artist. This input should tell the macro both the string stored in each cell and the location - e.g, E1, C2 - of said cell. The macro should loop through each URL in column A from row 3 to 608, querying the URL into a new sheet. This new sheet should be made active, and then every 3rd row starting with 4 should be searched sequentially for the album title string. Upon finding a match, the cell one row beneath the matching query cell should be compared to the artist name string. Should both strings match the content of their corresponding query cells, the number (from 1 to 200) in column C and the same row as the matching album title cell should be copied to the cell in the 'bb200' sheet corresponding to the URL queried and the album title searched. The loop should now recur on the next URL in the sequence. In the event no match is found (the album didn't chart that week, or BB returned a wonky table), the corresponding cell should be left blank. The macro should exit once the list of URLs is exhausted.

我的问题是双重的:首先,我对宏观的思考过程从根本上说是合理的吗?其次,但最重要的是,我什至不知道从哪里开始在 VBA 中编写它。我研究过 Java、C 和最近的 C++(特别是 OpenGL)。我对 VBA 语法和 API 完全不熟悉,坦率地说,我在这方面的时间太短了,无法坐下来正式学习这门语言。在此之后,我计划在短期内完成这项工作,但这项任务是在星期一完成的,我不知道它最终会有多大的规模。

为了记录,宏不是任务,但要收集的数据对于完成它是不可或缺的。对那些好奇的人来说,任务是在星期一之前完成我的毕业论文的完整草稿。这些数据将用于创建我的顾问指示我在写作中包含的几个图表。论文本身已经基于在网站外阅读每张专辑的销售业绩而编写。

你会帮助我超越,因为大多数其他应届毕业生都在提交一些严重不成熟的图形表示。唯一一个走到这一步的学生发明了一种仪器并提供了原理图和 AutoCAD 图纸。然而,我离题了。

在此先感谢您的帮助!!

4

1 回答 1

3

我认为这应该能让你获得大约 90% 的收益。唯一不会做的是网络查询。

For that part, I propose that you use the macro recorder to do a web query, and then post that code in a revision and we'll add it in and tailor it to your needs. You've gotta do some work on this :)

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

Good luck!

Edit this also assumes that there is only going to be one match in each web query. In the event there is more than one, it would only return the last match. That seems like a safe assumption given the nature of the data, but if that's not the case, let me know and I can tweak it.

于 2013-03-31T01:25:05.440 回答