Is it possible for VBA to meet my requirements? All instructions are included in the document. thanks
Is it possible for VBA to meet my requirements? All instructions are included in the document. thanks
yes. try this:
PHP Code:
Option Explicit
Sub Math()
Dim r&, c&, rng, symb
Const min = 10 ' adjust to actual min value
Const max = 20 ' adjust to actual max value
rng = Range("A2:H11").Value
symb = Array("+", "-", "*", "/")
Randomize
For c = 1 To UBound(rng, 2)
For r = 1 To UBound(rng)
If InStr(1, "-1-4-7-", "-" & c & "-") And InStr(1, "-2-6-10-", "-" & r & "-") Then
rng(r, c) = symb(Int(Rnd() * 4))
ElseIf InStr(1, "-2-5-8-", "-" & c & "-") And InStr(1, "-1-2-5-6-9-10-", "-" & r & "-") Then
rng(r, c) = min + Int(Rnd() * (max - min)) + 1
End If
Next
Next
Range("A2").Resize(UBound(rng), UBound(rng, 2)).Value = rng
End Sub
Quang PT
Dear Bebo021999, thank you for your answer. But how can I increase the number of randomly generated rows? If I need 10, 20, or 100 lines, how can I modify the code? The current code can only generate 3 fixed lines
Sorry for off-topic interjection:
Although there is no official rule regarding this behavior, we request that wherever possible both the question AND the answer be provided in substantive detail here within the thread. An attached workbook is an excellent aid for posing a question and offering a solution, but solely doing that with no in thread explanation makes it difficult for researchers to understand or consider the Q & A of this thread without downloading what may be a pointless doc to them, if they can do that at all. Doing that also hides the content from search engines so others may never benefit from this.
I'm sure you understand, and we look forward to seeing you post your formulas/macros in your posts for the searching benefit of all.
Thanks again for all your hard work here!
1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
2. If your question is resolved, mark it SOLVED using the thread tools
3. Click on the star if you think someone helped you
Regards
Ford
again:
PHP Code:
Option Explicit
Sub Math()
Dim r&, c&, rng, symb
Const min = 10 ' adjust to actual min value
Const max = 20 ' adjust to actual max value
Const setnum = 6 ' set numbers of math
rng = Range("A2:H" & setnum * 4 - 1).Value
symb = Array("+", "-", "*", "/")
Randomize
For r = 2 To UBound(rng) Step 4
rng(r, 1) = symb(Int(Rnd() * 4))
rng(r, 4) = symb(Int(Rnd() * 4))
rng(r, 7) = symb(Int(Rnd() * 4))
rng(r, 2) = min + Int(Rnd() * (max - min)) + 1
rng(r, 5) = min + Int(Rnd() * (max - min)) + 1
rng(r, 8) = min + Int(Rnd() * (max - min)) + 1
rng(r - 1, 2) = min + Int(Rnd() * (max - min)) + 1
rng(r - 1, 5) = min + Int(Rnd() * (max - min)) + 1
rng(r - 1, 8) = min + Int(Rnd() * (max - min)) + 1
Next
Range("A2:J10000").ClearContents
Range("A2").Resize(UBound(rng), UBound(rng, 2)).Value = rng
End Sub
WOW, bebo021999, you are amazing!!! Thanks very much .
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks