• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member
  • Login

Random numbers incorrect when column hidden |VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Random numbers incorrect when column hidden |VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosRandom numbers incorrect when colum…
sp_PrintTopic sp_TopicIcon
Random numbers incorrect when column hidden
Avatar
Wim de Groot

New Member
Members

Excel for Finance
Level 0
Forum Posts: 2
Member Since:
September 20, 2019
sp_UserOfflineSmall Offline
1
September 16, 2021 - 5:47 pm
sp_Permalink sp_Print

I have a macro that creates numbers 1 through 10 in a random order, in cells D1:D10. The code goes as follows.

Sub Draw()
Dim i As Integer, Number As Integer

Range("D1:D10").ClearContents

For i = 1 To 10
Do
Number = Int(Rnd * 10) + 1
Loop Until Range("D1:D" & i).Find(What:=Number, LookAt:=xlWhole) Is Nothing
Range("D" & i) = Number
Next i

End Sub

This works fine. But when I hide that column, it fails. Some numbers appear twice. When I open that column, everything goes right.

Can anyone explain this behaviour? And, how to solve this?

Wim de Groot, the Netherlands

sp_AnswersTopicSeeAnswer See Answer
Avatar
Jessica Stewart
Northern USA
Member
Members


Trusted Members
Level 0
Forum Posts: 219
Member Since:
February 13, 2021
sp_UserOfflineSmall Offline
2
September 17, 2021 - 12:01 pm
sp_Permalink sp_Print

I am just learning, but my theory, and mind you it is only a theory, is this:

You are using a DO Loop until the number is nothing, well, you've hidden the cells so the display is effectively nothing. And now my question before I totally give horribly wrong advice and thoughts is, why are you combining a DO loop and a FOR loop? If you delete the DO loop and just use the FOR shouldn't that work just fine?

For i = 1 to 10
 Number = Int(Rnd * 10) + 1
 range("D" & i) = number
Next i

I hope if I'm totally off base, someone will correct me. Like I said - I'm still learning.

Avatar
Jessica Stewart
Northern USA
Member
Members


Trusted Members
Level 0
Forum Posts: 219
Member Since:
February 13, 2021
sp_UserOfflineSmall Offline
3
September 17, 2021 - 5:07 pm
sp_Permalink sp_Print

Or, to keep the format that obviously works with the one minor hiccup, do the loop until it comes across an empty string? 

Sub Draw()
Dim i As Integer, Number As Integer

Range("D1:D10").ClearContents

For i = 1 To 10
Do
Number = Int(Rnd * 10) + 1
Loop Until Range("D" & i) = ""
Range("D" & i) = Number
Next i

End Sub

I hope this helps, these are just ideas that I would start with if I had come across this issue. My knowledge is very basic, though, and I don't want to cause you more work. Someone check me, I welcome correction! Smile

Avatar
Velouria
London or thereabouts
Moderator
Members


Trusted Members

Moderators
Level 4
Forum Posts: 688
Member Since:
November 1, 2018
sp_UserOfflineSmall Offline
4
September 17, 2021 - 5:51 pm
sp_Permalink sp_Print

You could use something like COUNTIF instead of Find:

 

Sub Draw()
Dim i As Integer, Number As Integer

Dim DataRange As Range
Set DataRange = Range("D1:D10")
DataRange.ClearContents

For i = 1 To 10
Do
Number = Int(Rnd * 10) + 1
Loop Until WorksheetFunction.CountIf(DataRange, Number) = 0
DataRange.Cells(i) = Number
Next i

End Sub

sp_AnswersTopicAnswer
Answers Post
Avatar
Wim de Groot

New Member
Members

Excel for Finance
Level 0
Forum Posts: 2
Member Since:
September 20, 2019
sp_UserOfflineSmall Offline
5
September 21, 2021 - 10:23 pm
sp_Permalink sp_Print

@ Jessica, Tanks for your answer, but your suggestion will just add any number between 1 and 10, it does not give UNIQUE numbers.

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Chris Ritcey, QSolutions Group
Guest(s) 9
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 880
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 237
Jessica Stewart: 219
A.Maurizio: 213
Aye Mu: 201
jaryszek: 183
Newest Members:
Lisa Myers
David von Kleek
Ronald White
Ginette Guevremont
Taryn Ambrosi
Mark Davenport
Christy Nichols
Harald Endres
Ashley Hughes
Herbie Key
Forum Stats:
Groups: 3
Forums: 24
Topics: 6528
Posts: 28594

 

Member Stats:
Guest Posters: 49
Members: 32818
Moderators: 2
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel Office Scripts
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook

Sign up to our newsletter and join over 400,000
others who learn Excel and Power BI with us.

 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate
  • Sponsor Our Newsletter

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.