Check if sheet name exists, and insert sheet name and cell value into a table if true

Gregory72

New Member
Joined
Jun 25, 2014
Messages
12
Hi,

I'm havingconsiderable trouble writing a macro to do the following:

1) Select aparticular sheet in the workbook

2) Select a cell inthat worksheet

3) Check using a forloop if sheets exists and then insert data into a table if true
The sheets I'mchecking for existence (whether they are visible or not visible does not matterto me) are named "01", "02", "03", ... ,"99".
I want to insert thevalue of a particular cell within the sheet being checked into the active cell.I also want to insert the sheet name into the cell immediately left of theactive cell.

4) Select the cellimmediately below the active cell until the for loop is completed

5) Select a cell atthe top of the sheet


This is what Ihave:



SubFormChecklistTitles()

'1)
Sheets("FORMCHECKLIST").Select

'2)
Range("C4").Select

'3)
Dim i As Integer

For i = 1 To 99

If Sheets("0" &i).exist Or Sheets(i).exist = True Then

If i <= 9 Then
ActiveCell.Value ="='0" & i & "'!$M$1"
Else
ActiveCell.Value ="=" & i & "'!$M$S1"
End If

'4)
ActiveCell.Offset(0, -1).Select

Else
End If

Next
'5)
Range("C1").Select
End Sub



Thanks in advancefor any help.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi, nice explanation in your post!

Try this:

Code:
Sub FormChecklistTitles()

'1)
Sheets("FORMCHECKLIST").Select


'2)
Range("C4").Select


'3)
Dim ws As Worksheet
Dim i As Integer


For Each ws In ActiveWorkbook
    If IsNumeric(ws.Name) Then
        i = CInt(ws.Name)
            If i <= 9 Then
                ActiveCell.Value = "='0" & i & "'!$M$1"
            Else
                If i <= 99 Then
                    ActiveCell.Value = "=" & i & "'!$M$S1"
                End If
            End If
    End If


    '4)
    With ActiveCell
        .Offset(0, -1).Value = ws.Name
        .Offset(1).Select
    End With
Next ws


'5)
Range("C1").Select
End Sub
 
Upvote 0
Thanks for your help Ferdi24,

I'm getting the following error message:
"Object doesn't support this property or method"
while highlighting in the code:
"For Each ws In ActiveWorkbook"

I'm not sure how to fix this. Any suggestions?
 
Upvote 0
Nice! Works great! The only change I made was to put the With statement inside the If IsNumeric statement so only numbered forms were inserted.
Thanks for your assistance. Here's my final code:


Sub FormChecklistTitles()


'1)
Sheets("FORM CHECKLIST").Select




'2)
Range("C4").Select




'3)
Dim ws As Worksheet
Dim i As Integer




For Each ws In ActiveWorkbook.Sheets


If IsNumeric(ws.Name) Then
i = CInt(ws.Name)

If i <= 9 Then
ActiveCell.Value = "='0" & i & "'!$M$1"
Else
If i <= 99 Then
ActiveCell.Value = "='" & i & "'!$M$1"
End If
End If

'4)
With ActiveCell
.Offset(0, -1).Value = ws.Name
.Offset(1).Select
End With
End If
Next ws




'5)
Range("C1").Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,952
Messages
6,122,454
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top