PDA

View Full Version : [SOLVED:] Creating Sheets from Cell Value IF Only!



whatsapro
05-20-2016, 01:54 PM
I'm trying to create sheets from cell values only if they are not blank or contain "#N/A".

For example, I have Q3:Q38 that contain values I want to turn into sheets automatically. However, some of "#N/A" and others are blank. I do not wan tot create sheets for those ones.


Column Q
31114A
36516A



#N/A
22492A

22334A
#N/A

So the end result would be 4 sheets created, "31114A", "36516A", "22492A" and "22334A".

Here is some code I found online that does this except it creates the blanks and #N/A sheets as well.



Sub AddSheets()
Dim cell As Excel.Range
Dim wsWithSheetNames As Excel.Worksheet
Dim wbToAddSheetsTo As Excel.Workbook
Set wsWithSheetNames = ActiveSheet
Set wbToAddSheetsTo = ActiveWorkbook
For Each cell In wsWithSheetNames.Range("Q3:Q38")
With wbToAddSheetsTo
.Sheets.Add after:=.Sheets(.Sheets.Count)
On Error Resume Next
ActiveSheet.Name = cell.Value
If Err.Number = 1004 Then
Debug.Print cell.Value & " already used as a sheet name"
End If
On Error GoTo 0
End With
Next cell
End Sub

Any help would be appreciated.

Thanks.

p45cal
05-20-2016, 03:32 PM
try:
Sub AddSheets()
Dim cll As Excel.Range
Dim wsWithSheetNames As Excel.Worksheet
Dim wbToAddSheetsTo As Excel.Workbook
Set wsWithSheetNames = ActiveSheet
Set wbToAddSheetsTo = ActiveWorkbook
For Each cll In wsWithSheetNames.Range("Q3:Q38")
If Not (Application.IsNA(cll) Or Len(cll.Text) = 0) Then
With wbToAddSheetsTo
.Sheets.Add after:=.Sheets(.Sheets.Count)
On Error Resume Next
ActiveSheet.Name = cll.Value
If Err.Number = 1004 Then
Debug.Print cll.Value & " already used as a sheet name"
End If
On Error GoTo 0
End With
End If
Next cll
End Sub

whatsapro
05-22-2016, 03:39 PM
Worked flawlessly! Thank you so much. I'm sure I will have some more questions soon. I am pretty familiar with C++ programming concepts (academic version anyways) so I hope I can pick this stuff up fast. The problem I have is I was trying to get this implemented ASAP. That worked great.

Thank you again.