+ Reply to Thread
Results 1 to 4 of 4

add string in random cells of column for each same id

Hybrid View

fatoom91 add string in random cells of... 01-15-2020, 04:44 AM
WideBoyDixon Re: add string in random... 01-15-2020, 06:09 AM
fatoom91 Re: add string in random... 01-15-2020, 06:21 AM
Richard Buttrey Re: add string in random... 01-15-2020, 06:17 AM
  1. #1
    Registered User
    Join Date
    09-20-2019
    Location
    lebanon
    MS-Off Ver
    2016
    Posts
    36

    add string in random cells of column for each same id

    Hi I'm trying to add string = "bid canceled" in a column in range 2 to 6 time for each same Auction-ID if the bid status is empty:

    Private Sub Bidcanceled_Click()
     Application.ScreenUpdating = False
    
        Dim Bidstatus As Range, rng As Range, rng1 As Range, x As Integer, N As Long
        Dim str As String
         N = Cells(Rows.Count, "A").End(xlUp).Row
         Set rng = Range("H2:H" & N)
         str = "bid canceled"
           For Each Bidstatus In rng
            Select Case Int(Rnd * 10) Mod 2
            Case 1: lb = 2: ub = 6
            End Select
            x =
           ' Do While Application.WorksheetFunction.CountIf(rng, x) >= 1
               Do While Application.WorksheetFunction.CountIfs(rng.Offset(, -1), Bidstatus.Offset(, -1), rng, x) >= 1
                x =
            Loop
            Bidstatus = x
        Next
       Application.ScreenUpdating = True
    
    End Sub
    the attachment in sheet one is the output that I except, I didnt know how to insert string 2 times or 3 or 4 ...6 times in random cells for each auction id if the cells are empty,any idea for that?
    Attached Files Attached Files

  2. #2
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: add string in random cells of column for each same id

    Give this a try:

    Public Sub RandomCancel()
    
    Dim lastRow As Long
    Dim thisRow As Long
    Dim lastID As Variant
    Dim blankRows As Collection
    Dim randomCancels As Long
    Dim randomRow As Long
    
    Randomize Now
    lastID = ""
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row + 1
    For thisRow = 2 To lastRow
        If Cells(thisRow, "A").Value <> lastID Then
            If lastID <> "" Then
                randomCancels = Int(Rnd * 5) + 2
                If randomCancels > blankRows.Count Then randomCancels = blankRows.Count
                Do While randomCancels > 0
                    randomRow = Int(Rnd * blankRows.Count) + 1
                    Cells(blankRows(randomRow), "H").Value = "bid canceled"
                    blankRows.Remove randomRow
                    randomCancels = randomCancels - 1
                Loop
            End If
            
            lastID = Cells(thisRow, "A").Value
            Set blankRows = New Collection
        End If
        
        If Cells(thisRow, "H").Value = "" Then blankRows.Add thisRow
    Next thisRow
    
    End Sub
    WBD
    Office 365 on Windows 11, looking for rep!

  3. #3
    Registered User
    Join Date
    09-20-2019
    Location
    lebanon
    MS-Off Ver
    2016
    Posts
    36

    Re: add string in random cells of column for each same id

    Quote Originally Posted by WideBoyDixon View Post
    Give this a try:

    Public Sub RandomCancel()
    
    Dim lastRow As Long
    Dim thisRow As Long
    Dim lastID As Variant
    Dim blankRows As Collection
    Dim randomCancels As Long
    Dim randomRow As Long
    
    Randomize Now
    lastID = ""
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row + 1
    For thisRow = 2 To lastRow
        If Cells(thisRow, "A").Value <> lastID Then
            If lastID <> "" Then
                randomCancels = Int(Rnd * 5) + 2
                If randomCancels > blankRows.Count Then randomCancels = blankRows.Count
                Do While randomCancels > 0
                    randomRow = Int(Rnd * blankRows.Count) + 1
                    Cells(blankRows(randomRow), "H").Value = "bid canceled"
                    blankRows.Remove randomRow
                    randomCancels = randomCancels - 1
                Loop
            End If
            
            lastID = Cells(thisRow, "A").Value
            Set blankRows = New Collection
        End If
        
        If Cells(thisRow, "H").Value = "" Then blankRows.Add thisRow
    Next thisRow
    
    End Sub
    WBD
    thanks so much

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: add string in random cells of column for each same id

    Your upload was a .xlsx file and therefore didn't contain the macro. Please upload the .xlsm file

    In addition please indicate in a narrative form exactly what you want to do, the rules you apply and for a few of your results explain how you arrived at them.

    It seems there's an element of randomness in your system, but rather than reverse engineer your macro, which presumably isn't giving the results you want since you've raised the problem here, I'd rather know the overall aim.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Merge random cells in a column in sequenced order in another column
    By Chris1972 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 10-10-2018, 04:49 PM
  2. Replies: 6
    Last Post: 08-07-2018, 09:25 AM
  3. Generating random number string based on Alphanumeric String
    By ridemeve in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-15-2014, 04:56 PM
  4. [SOLVED] Generating Random String from Text in Cells
    By abbeycrombie in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 12-18-2013, 12:09 PM
  5. [SOLVED] selecting random cells from a column and paste to another.
    By dxblade in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-01-2013, 09:04 AM
  6. Only two values (numbers) for 150 random cells in a column of 300 cells
    By andrugrasu in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-20-2011, 03:17 AM
  7. Replies: 7
    Last Post: 09-01-2007, 04:29 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1