I am trying to take from a range of random words and output a sentence onto sheet 2 but I am stuck, any thoughts would be appreciated.
Option Explicit
Sub main()
Dim rng As Range
Dim sentenceNumber As Long
Dim sheet As Long
Set rng = Range(cells(1, 1), cells(10, 10))
sentenceNumber = InputBox("How many sentences?")
sentence rng, "Sheet2"
End Sub
Sub sentence(ByVal rng As Range, ByVal sheetname As String)
rng.Value = article & " " & noun & " " & verb & " " & preposition
End Sub
Function article()
Const ARTICLE_COL As Long = 1
Dim lastRow As Long
Dim row As Long
Dim startRow As Long
startRow = 1
lastRow = findLastRow(ARTICLE_COL)
Dim rng As Range
Set rng = Range(cells(startRow, ARTICLE_COL), cells(lastRow, ARTICLE_COL))
For row = startRow To lastRow
Dim randomCell As String
Dim word As String
randomCell = Application.WorksheetFunction.RandBetween(1, lastRow)
word = cells(randomCell, ARTICLE_COL).Value
Next row
article = word
End Function
Function noun()
Const NOUN_COL As Long = 2
Dim lastRow As Long
Dim startRow As Long
startRow = 1
lastRow = findLastRow(NOUN_COL)
Dim rng As Range
Set rng = Range(cells(startRow, NOUN_COL), cells(lastRow, NOUN_COL))
End Function
Function verb()
Const Verb_COL As Long = 3
Dim lastRow As Long
Dim startRow As Long
startRow = 1
lastRow = findLastRow(Verb_COL)
Dim rng As Range
Set rng = Range(cells(startRow, Verb_COL), cells(lastRow, Verb_COL))
End Function
Function preposition()
Const PREP_COL As Long = 4
Dim lastRow As Long
Dim startRow As Long
startRow = 1
lastRow = findLastRow(PREP_COL)
Dim rng As Range
Set rng = Range(cells(startRow, PREP_COL), cells(lastRow, PREP_COL))
End Function
Function findLastRow(ByVal col As Integer) As Long
'find the last row with data in a given column
findLastRow = cells(Rows.count, col).End(xlUp).row
End Function
Bookmarks