|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
怎么把宏1和宏2合并成一个工作簿,老师麻烦帮忙!
宏一代码如下:
Function FillOneRow(url As String, r As Integer) As Integer
With CreateObject("msxml2.xmlhttp")
.Open "GET", url, False
.send
sp = Split(.responsetext, "~")
If UBound(sp) > 3 Then
FillOneRow = 1
Cells(r, 2).Value = sp(1) '名称
Cells(r, 3).Value = sp(3) '当前价格
Cells(r, 4).Value = sp(4) '昨日收盘价
Dim zhangDie As Double
zhangDie = sp(32)
Cells(r, 5).Value = zhangDie
If zhangDie > 0 Then
'上涨使用红色
Cells(r, 5).Font.Color = vbRed
Cells(r, 3).Font.Color = vbRed
Else
'下跌使用绿色
Cells(r, 5).Font.Color = &H228B22
Cells(r, 3).Font.Color = &H228B22
End If
Else
FillOneRow = 0
End If
End With
End Function
Sub GetData()
Dim succeeded As Integer
Dim url As String
Dim row As Integer
Dim code As String
For row = 2 To Range("A1").CurrentRegion.Rows.Count '从第二行开始
code = Cells(row, 1).Value
If code <> "" Then
url = "http://qt.gtimg.cn/q=sh" & code '沪市
succeeded = FillOneRow(url, row)
If succeeded = 0 Then
url = "http://qt.gtimg.cn/q=sz" & code '深市
succeeded = FillOneRow(url, row)
End If
If succeeded = 0 Then
MsgBox ("获取失败")
End If
End If
Next
End Sub
宏2代码如下:
'Option Explicit
Sub test()
For r = 3 To Range("A1").CurrentRegion.Rows.Count
dm = Cells(r, 1).Value
If Val(dm) < 600000 Then
url = "http://qt.gtimg.cn/q=sz" & dm
Else
url = "http://qt.gtimg.cn/q=sh" & dm
End If
With CreateObject("msxml2.xmlhttp")
.Open "GET", url, False
.send
sp = Split(.responsetext, "~")
If UBound(sp) > 3 Then
Cells(r, 3).Value = sp(3)
Cells(r, 4).Value = Format(sp(30), "0000-00-00 00:00:00")
Else
Cells(r, 3).Value = "证券代码错啦!"
End If
End With
Next
End Sub
|
|