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