I'm trying to create VBA code to run through a worksheet (and eventually a workbook) and generate comma separated lists for each cell which has validation. I was able to get achieve my goal on a defined range using the the code below:
Sub ValidationPrintOut2()
Dim cell As Range
Dim oldstr As String
Dim newstr As String
Dim usedcell As Range
For Each usedcell In ActiveSheet.Range("N1:N3")
For Each cell In Range(usedcell.Validation.Formula1)
oldstr = ActiveSheet.Cells(usedcell.Row, usedcell.Column + 2)
newstr = cell.Value
ActiveSheet.Cells(usedcell.Row, usedcell.Column + 2) = oldstr + ", " + newstr
Next cell
Next usedcell
End Sub
But when I tried to expand it the code to the used range in a column (below) the code ends up breaking with a Method Error '1004': Method 'Range' of Object '_Global' failed.
Sub ValidationPrintOut2()
Dim cell As Range
Dim oldstr As String
Dim newstr As String
Dim usedcell As Range
For Each usedcell In ActiveSheet.UsedRange.Columns("N")
For Each cell In Range(usedcell.Validation.Formula1)
oldstr = ActiveSheet.Cells(usedcell.Row, usedcell.Column + 2)
newstr = cell.Value
ActiveSheet.Cells(usedcell.Row, usedcell.Column + 2) = oldstr + ", " + newstr
Next cell
Next usedcell
End Sub
Could someone explain why this is happening and how to fix the problem? Thanks!