返回列表 上一主題 發帖

[發問] 有關使用事件程序取得資料(有二個問題)

回復 1# icestormer
VBAProject
主工作表
  1. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  2.   If Application.Intersect(Target, Range("F1:G1")) Is Nothing Then Exit Sub
  3.   UpdatePrice
  4. End Sub
複製代碼
一般模組
  1. Sub UpdatePrice()
  2.   Dim d, rng As Range
  3.   Dim sName As String, sSell As String, sBuy As String
  4.   Dim r As Long, c As Long, x
  5.   
  6.   Const sMark = " - Market Browser"
  7.   Const sMarkSell = "Sell Orders (Buy Orders)"
  8.   Const sMarkBuy = "Buy Orders"
  9.   
  10.   Set d = CreateObject("scripting.dictionary")
  11.   
  12.   With Sheets("資料區")
  13.     For c = 1 To .UsedRange.Columns.Count Step 7  '6欄加上空白欄 = 7
  14.       For r = 1 To .Cells(.Rows.Count, c).End(xlUp).Row Step 300 '固定300列
  15.         sName = "": sSell = "": sBuy = ""
  16.         With .Cells(r, c).Resize(300)
  17.           Set rngName = .Find(sMark, LookIn:=xlValues, lookat:=xlPart)
  18.           If rngName Is Nothing Then GoTo NEXT_BLOCK Else sName = Left(rngName.Value, Len(rngName.Value) - Len(sMark))
  19.          
  20.           Set rng = .Find(sMarkSell, LookIn:=xlValues, lookat:=xlWhole)
  21.           If Not rng Is Nothing Then sSell = rng.Offset(3, 2).Value
  22.          
  23.           Set rng = .Find(sMarkBuy, LookIn:=xlValues, lookat:=xlWhole)
  24.           If Not rng Is Nothing Then sBuy = rng.Offset(3, 2).Value
  25.          
  26.           d(sName) = Array(sSell, sBuy)
  27.         End With
  28. NEXT_BLOCK:
  29.       Next
  30.     Next
  31.   End With
  32.   
  33.   '貼上價位
  34.   With Sheets("主工作表")
  35.     For Each x In .Range(.Range("A2"), .Cells(.Rows.Count, "A").End(xlUp))
  36.       If d.exists(x.Value) Then x.Offset(, 1).Resize(, 2).Value = d(x.Value)
  37.     Next
  38.   End With

  39. End Sub
複製代碼

TOP

回復 4# icestormer
按上方[更新價位]字樣(F1 G1儲存格)

TOP

回復 6# icestormer
所以你資料表是有很多很多外部資料連線組成,全部都更新太慢所以是要找到特定的外部資料更新連線就好,
然後再取回買賣價到主工作表,對嗎?

修改主工作表
  1. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  2.   If Application.Intersect(Target, Range("F1:G1")) Is Nothing Then Exit Sub
  3.   UpdateQueryTable
  4.   UpdatePrice
  5. End Sub
複製代碼
原本一般模組再加上:
  1. Sub UpdateQueryTable()
  2.   Dim ar, rng As Range, x
  3.   Dim sMsg As String, bNotFound As Boolean
  4.   
  5.   Const sMark = " - Market Browser"
  6.   sMsg = "以下找不到 : "
  7.   
  8.   With Sheets("主工作表")
  9.     ar = Application.Transpose(.Range(.Range("A2"), .Cells(.Rows.Count, "A").End(xlUp)).Value)
  10.   End With
  11.   
  12.   With Sheets("資料區")
  13.     For Each x In ar
  14.       Set rng = .Cells.Find(x & sMark, LookIn:=xlValues, lookat:=xlWhole)
  15.       If rng Is Nothing Then
  16.         sMsg = sMsg & vbCrLf & x & sMark
  17.         bNotFound = True
  18.       Else
  19.         rng.QueryTable.Refresh BackgroundQuery:=False
  20.       End If
  21.     Next
  22.   End With
  23.   
  24.   If bNotFound Then MsgBox sMsg Else MsgBox "Finish"
  25. End Sub
複製代碼

TOP

本帖最後由 stillfish00 於 2014-1-16 10:20 編輯

回復 9# icestormer
改上面
  With Sheets("主工作表")
    ar = Application.Transpose(.Range(.Range("A2"), .Cells(.Rows.Count, "A").End(xlUp)).Value)
    If Not IsArray(ar) Then ar = Array(ar)
  End With


去掉 updateprice   要跑時有錯誤
不懂,不可能取消呼叫函數反而產生錯誤

TOP

回復 13# icestormer
1. FOR迴圈結束就全部完成阿..
2. 沒遇到,迴圈內加個DOEVENTS看看

TOP

        靜思自在 : 看別人不順眼,是自己修養不夠。
返回列表 上一主題