我想运行循环,但每个实例更改2个变量

时间:2016-04-25 03:20:03

标签: vba excel-vba excel

我正在尝试编辑VBA代码。我根据自己的需要定制了它,但它目前重复了相同的过程超过20次,我基本上输入了20次代码并更改了变量。

我可能最多有100个实例,并且不想手动执行此操作。如何编辑此代码,以便在更改变量时运行100次?

如您所见,我已经将相同的代码包含两次,只需稍作修改即可。我需要更改每次迭代的自动收报机和范围,而且,目标范围每次需要移动1列。

顺便说一句,这是为了拉动雅虎财经股票信息。

Sub Data_Get()
'
' Data_Get Macro
'
Dim ticker1, ticker2, ticker3 As String, sday, smonth, syear, eday, emonth, eyear As Long

ticker1 = Range("L1")
ticker2 = Range("L2")
ticker3 = Range("L3")

sday = Day(Range("L4"))
smonth = Month(Range("L4")) - 1
syear = Year(Range("L4"))
eday = Day(Range("L5"))
emonth = Month(Range("L5")) - 1
eyear = Year(Range("L5"))

'
With ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;http://real-chart.finance.yahoo.com/table.csv?s=" & ticker1 & "&d=" & emonth & "&e=" & eday & "&f=" & eyear & "&g=d&a=" & smonth & "&b=" & sday & "&c=" & syear & "&ignore=.csv" _
    , Destination:=Range("$A$1"))
    .Name = "data"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 437
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = True
    .TextFileTabDelimiter = True
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = True
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(5, 9, 9, 9, 9, 9, 1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With

With ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;http://real-chart.finance.yahoo.com/table.csv?s=" & ticker2 & "&d=" & emonth & "&e=" & eday & "&f=" & eyear & "&g=d&a=" & smonth & "&b=" & sday & "&c=" & syear & "&ignore=.csv" _
    , Destination:=Range("$C$1"))
    .Name = "data2"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 437
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = True
    .TextFileTabDelimiter = True
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = True
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(9, 9, 9, 9, 9, 9, 1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With
End Sub

1 个答案:

答案 0 :(得分:0)

执行以下操作......

Dim i as long, nIter as long
' calculate the number iterations here, using your calcuation
nIter = 100

' do the same job repeatedly
for iLoop = 1 to nIter

' put the code you want to repeat in here
' if you want to move down in a range every iteration, one possible way is as follows...
ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;http://real-chart.finance.yahoo.com/table.csv?s=" & ticker1 & _
      "&d=" & emonth & "&e=" & eday & "&f=" & eyear & "&g=d&a=" & smonth & _
      "&b=" & sday & "&c=" & syear & "&ignore=.csv" _
      , Destination:=Range("$A$1").Offset(iLoop-1,0))

next iLoop