Solved

Run Macro

Posted on 2011-02-19
3
272 Views
Last Modified: 2012-05-11
Hello trying to figure this out i have a macro see below.  I need to run the macro until strData does not have a value.  but also need a way to change the cell location of strData each time run, for example the first run strData will be the value at 55,2, the next run is 56,2, the next 57,2 until the last data in that column, then end macro.

Dim strURL, strURL1, strURL2, strURL3, strData
    On Error Resume Next
    'called from Sheet1 where A12 contains - pafb
   
    strData = Sheets("Control").Cells(55, 2)
   
    'change sheets
    Sheets("Sheet1").Activate   'sheet named WebData to hold returned data
   
    'clear last web query
    'Cells.Select
    'Selection.Clear
    'this line will fail if there isn't an existing web query - comment out the first time around...
    'Selection.QueryTable.Delete

    'return to top
    'Cells(1, 1).Select
   
    'get new web data
    strURL = "URL;http://68.226.77.253/text/NAMSFC/NAM_" & strData & ".txt"
    strURL1 = "URL;http://charlie.wxcaster.com/text/GFSSFC/GFS_" & strData & ".txt"
    strURL2 = "URL;http://www.nws.noaa.gov/cgi-bin/lamp/getlav.pl?sta=" & strData
    strURL3 = "URL;http://weather.uwyo.edu/cgi-bin/wyowx.fcgi?TYPE=metar&DATE=current&HOUR=current&UNITS=A&STATION=" & strData
    Range("A1").Select
    With Selection.QueryTable
        .Connection = strURL
        .WebSelectionType = xlAllTables
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = False
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
0
Comment
Question by:sandramac
  • 2
3 Comments
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
ID: 34934236
Is this what you are trying?

Sub Sample()
    Dim strURL As String, strURL1 As String, strURL2 As String
    Dim strURL3 As String, strData As String
    
    LastRow = Sheets("Control").Range("B" & Rows.Count).End(xlUp).Row
    
    For i = 55 To LastRow
        strData = Sheets("Control").Cells(i, 2)
   
        strURL = "URL;http://68.226.77.253/text/NAMSFC/NAM_" & strData & ".txt"
        strURL1 = "URL;http://charlie.wxcaster.com/text/GFSSFC/GFS_" & strData & ".txt"
        strURL2 = "URL;http://www.nws.noaa.gov/cgi-bin/lamp/getlav.pl?sta=" & strData
        strURL3 = "URL;http://weather.uwyo.edu/cgi-bin/wyowx.fcgi?TYPE=metar&DATE=current&HOUR=current&UNITS=A&STATION=" & strData
        
        With Sheets("Sheet1").Range("A1").QueryTable
            .Connection = strURL
            .WebSelectionType = xlAllTables
            .WebFormatting = xlWebFormattingNone
            .WebPreFormattedTextToColumns = False
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .WebDisableRedirections = False
            .Refresh BackgroundQuery:=False
        End With
    Next
End Sub

Open in new window


Sid
0
 
LVL 41

Expert Comment

by:dlmille
ID: 34934478
Another solution  - sets public variable on open and increments from there...  You might want a routine to zero the RowIncr if you're testing in the middle of all this:

Public sub zeroIncr()
  RowIncr = 0
end sub


And your code would be:

Public Const InitialRow As Integer = 55 'initial row to set
Public RowIncr As Integer
'initially is zero at compile/bind

     Dim strURL, strURL1, strURL2, strURL3, strData
    On Error Resume Next
    'called from Sheet1 where A12 contains - pafb

strData = Sheets("Control").Cells(InitialRow + RowIncr, 2)  

    'change sheets
    Sheets("Sheet1").Activate   'sheet named WebData to hold returned data
   
    'clear last web query
    'Cells.Select
    'Selection.Clear
    'this line will fail if there isn't an existing web query - comment out the first time around...
    'Selection.QueryTable.Delete

    'return to top
    'Cells(1, 1).Select
   
    'get new web data
    strURL = "URL;http://68.226.77.253/text/NAMSFC/NAM_" & strData & ".txt"
    strURL1 = "URL;http://charlie.wxcaster.com/text/GFSSFC/GFS_" & strData & ".txt"
    strURL2 = "URL;http://www.nws.noaa.gov/cgi-bin/lamp/getlav.pl?sta=" & strData
    strURL3 = "URL;http://weather.uwyo.edu/cgi-bin/wyowx.fcgi?TYPE=metar&DATE=current&HOUR=current&UNITS=A&STATION=" & strData
    Range("A1").Select
    With Selection.QueryTable
        .Connection = strURL
        .WebSelectionType = xlAllTables
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = False
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With

 RowIncr = RowIncr + 1

Dave
0
 
LVL 41

Expert Comment

by:dlmille
ID: 34934483
Of course, this assumes the initial row is ALWAYS 55.  If it needs to be determined by the existing range, Sid's approach is the way to go.

Dave
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question