Solved

Run Macro

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

911 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

22 Experts available now in Live!

Get 1:1 Help Now