簡體   English   中英

在另一個單元格中顯示列表值/循環列表

[英]Display list values in another cell/ Cycle through list

我有一個單元格 A1:A75 中的值列表,以及一個隨機 select 一個按鈕並將其顯示在單元格 D3 中:

Sub BingoGen()
Dim ws As Worksheet
Dim stRow As Long, endRow As Long, dataCol As Long
Dim dispRow As Long, dispCol As Long

Set ws = Sheets("BingoHome")
stRow = 2
dataCol = 1
dispRow = 3
dispCol = 4

    With ws
        endRow = .Cells(.Rows.Count, dataCol).End(xlUp).Row
        .Cells(dispRow, dispCol).Value = _
          .Cells(Application.RandBetween(stRow, endRow), dataCol).Value
    End With

我想通過顯示 A1 中的值開始更改此設置,並在每次單擊按鈕時向下處理列表。

我有另一個宏按鈕,它將隨機化這個值列表。

我將使用static變量來存儲當前引用的單元格的位置。

Sub BingoGen()
    Dim ws As Worksheet
    Dim stRow As Long, endRow As Long, dataCol As Long
    Dim dispRow As Long, dispCol As Long
    Static dataRow As Integer

    Set ws = Sheets("BingoHome")
    stRow = 2
    dataCol = 1
    dispRow = 3
    dispCol = 4
    With ws
        'if you will always have 75 rows I would just use endRow = 75
        endRow = .Cells(.Rows.Count, dataCol).End(xlUp).Row
        'if the data row is undefined or larger than the end row
        If dataRow < 1 Or dataRow > endRow Then
            dataRow = 1
        'otherwise (1<= dataRow <= endRow) index dataRow by 1
        Else
            dataRow = dataRow + 1
        End If
        .Cells(dispRow, dispCol).Value = .Cells(dataRow, dataCol).Value
    End With

此外,出於好奇,您是否有理由將所有變量輸入為Long ,而它們很容易適合Integer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM