Solved

Run Macro

Posted on 2011-02-19
3
266 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to batch remove spreadsheet password 19 93
Convert .PDF 6 42
Out of stack space (Error 28) 5 24
TT Master To Fresh - Rework 4 25
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now