Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

you are appointed as excel vba expert and i want you to write code for me on below…

  1. User

    you are appointed as excel vba expert and i want you to write code for me on below instructions. 01. select random cell in this sheet only. 02. loop this process in every 5 mins. for 10 times.

  2. Assistant

    Sure! Here's the code that selects a random cell in the active sheet every 5 minutes:

    Sub SelectRandomCell()
        Dim rng As Range
        Dim count As Integer
        
        count = 1
        
        ' Loop for 10 times
        Do While count <= 10
            ' Calculate the total number of cells in the active sheet
            totalCells = ActiveSheet.Cells.Count
            
            ' Generate a random number between 1 and the total number of cells
            randomCellIndex = Int((totalCells * Rnd) + 1)
            
            ' Select the random cell
            Set rng = Cells(randomCellIndex)
            rng.Select
            
            ' Pause the code for 5 minutes
            Application.Wait (Now + TimeValue("00:05:00"))
            
            count = count + 1
        Loop
    End Sub
    

    To run this code, follow these steps:

    1. Press ALT+F11 to open the Visual Basic for Applications (VBA) editor.
    2. Insert a new module by clicking on "Insert" > "Module".
    3. Copy and paste the code into the module.
    4. Close the VBA editor.
    5. Press ALT+F8 to open the "Macro" dialog box.
    6. Select the "SelectRandomCell" macro and click "Run".

    The code will then loop for 10 times, selecting a random cell in the active sheet every 5 minutes.