te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>excel - VBA How to create an array formula - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

excel - VBA How to create an array formula - Stack Overflow

programmeradmin3浏览0评论

I'm trying to create a custom function to fill selected cells with unique random numbers. My problem is to get the range of selected cells eg.

=randm()

My problem is that I don't know the cells range to fill

I need to get the range address H16:J24

Is it possible ?

Regards.

I'm trying to create a custom function to fill selected cells with unique random numbers. My problem is to get the range of selected cells eg.

=randm()

My problem is that I don't know the cells range to fill

I need to get the range address H16:J24

Is it possible ?

Regards.

Share Improve this question edited 2 days ago BigBen 50k7 gold badges28 silver badges44 bronze badges asked 2 days ago danielBLRdanielBLR 52 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You are facing several issues when you want to use a UDF (User defined formula) to fill the selected range:

  • You can't pass the selection (the current selected cells) as parameter
  • A UDF can't write to the sheet. It can only calculate something and Excel writes the result into the cell where the formula is used.
  • You cannot create a spill function with VBA (Update: I was wrong, you can.)
  • If it would be possible to create such a function, it would be triggered whenever Excel thinks that it needs to recalculate the sheet. As the function creates random numbers, the range would be filled newly with every sheet recalculation - probably not what you want.

So instead, write a Subroutine that fills the Range:

Sub randM(Optional r As Range = Nothing)
    If r Is Nothing Then Set r = Selection
    
    Dim valueList As Collection, i As Long
    Set valueList = GetRandomValueList
    
    Dim cell As Range
    For Each cell In r
        Dim randomIndex As Long, randVal As Double
        randomIndex = Int((Rnd * valueList.Count) + 1)
        cell.Value = valueList(randomIndex)
        valueList.Remove randomIndex

        If valueList.Count = 0 Then Exit Sub  ' No more possible values.
    Next
End Sub

Now you need a simple routine that fills a list (I use a Collection) with possible values. The following example creates a deck of cards:

Private Function GetRandomValueList() As Collection
    Dim valueList As New Collection
    Dim i, j
    For Each i In Array(2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King", "Ace")
        For Each j In Array("Spade", "Hearts", "Diamonds", "Clubs")
            valueList.Add i & " of " & j
        Next
    Next
    Set GetRandomValueList = valueList
End Function

And as you can only call parameterless Subroutines from Excel, just create a wrapper that runs on the current Selection

Sub randMOnSelection
    randM Selection
End Sub

Now just call this routine whenever you need a range filled with random numbers. You can do this from the Macro-Menu (Developer->Macros or View->Macros). You can also assign the Macro to a Key (eg Ctrl+Shift+R) in the Macro Menu under [Options...]. Or you place a button or shape on your sheet that calls this routine.

发布评论

评论列表(0)

  1. 暂无评论