2015-04-01 54 views
-1

我在Excel中包含大量文本的單元格M2。我試圖找出使這個文本從右向左連續滾動的方法。從右向左進行單元格滾動/選取框文本?

我在網上查了很多東西,但是我可以找到的是這樣的代碼,這對我沒有任何意義,我想盡量讓這個代碼儘可能簡單。有人能告訴我一個簡單的方法讓我做我想做的事。

Sub StartMarquee() 
Dim sMarquee As String 
Dim iPosition As Integer 

sMarquee = "This is a scrolling Marquee" 

With Me 
With .tbMarquee 
.Text = "" 

For iPosition = 1 To Len(sMarquee) 
.Text = .Text & Mid(sMarquee, iPosition, 1) 
Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 1) 
Next iPosition 
End With 
End With 

'Beep 
'Application.OnTime Now + TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 2), "StartMarquee" 
End Sub 

回答

0

雖然這可能的內部在子程序完成for循環,整個應用程序會同時進行循環執行,這將使它非常無益被關起來。

而是將一次子程序作爲單次迭代運行。當子運行時,您希望它檢測郵件中的哪個字幕已位於單元格M13中,然後再將該消息再推送一個字符。然後Application.OnTime交易將爲其下一次迭代安排子例程。

Sub DoMarquee() 
    Dim sMarquee As String 
    Dim iWidth As Integer 
    Dim iPosition As Integer 
    Dim rCell As Range 
    Dim iCurPos As Integer 

    'Set the message to be displayed in this cell 
    sMarquee = "This is a scrolling Marquee." 

    'Set the cell width (how many characters you want displayed at once 
    iWidth = 10 

    'Which cell are we doing this in? 
    Set rCell = Sheet1.Range("M2") 

    'determine where we are now with the message. 
    ' instr will return the position of the first 
    ' character where the current cell value is in 
    ' the marquee message 
    iCurPos = InStr(1, sMarquee, rCell.Value) 

    'If we are position 0, then there is no message, so start over 
    ' otherwise, bump the message to the next characterusing mid 
    If iCurPos = 0 Then 
     'Start it over 
     rCell.Value = Mid(sMarquee, 1, iWidth) 
    Else 
     'bump it 
     rCell.Value = Mid(sMarquee, iCurPos + 1, iWidth) 
    End If 

    'Set excel up to run this thing again in a second or two or whatever 
    Application.OnTime Now + TimeValue("00:00:01"), "DoMarquee" 
End Sub 

把這個放到一個新的模塊中,在你的工作簿的VBE中運行它。如果你希望它停止,那麼註釋掉最後一行Application.OnTime...

這個整個子程序將在閃存中運行,所以工作簿的用戶確實不應該看到它每秒運行一次,以將選取框碰到下一個字符。