0
我正在尝试编辑VBA代码。我为我的需要定制了它,但它目前重复同样的过程超过20次,我基本上已经输入了20次代码并更改了变量。我想运行一个循环,但每个实例更改2个变量
我可能有多达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行中列出了每个列表中的信息,但是当我运行这个时,它会创建新列,并将第一行中的内容转移。我如何防止这种情况? – Namelessandroid
看一下[MS文档](https://msdn.microsoft.com/EN-US/library/office/ff839455.aspx)。我想你应该使用'xlInsertEntireRows'而不是'xlInsertDeleteCells' – OldUgly