Macro for Inserting Lines and Adding Dates

ststern45

Well-known Member
Joined
Sep 17, 2005
Messages
926
Office Version
  1. 2010
Platform
  1. Windows
I have a worksheet that has 1o workbooks (TABS)

What I would like to do is insert a line in each of the Tabs and add the current date in cell A10.

The information on each line has a range from cell A10 through CO10.

I have certain tabs depending on the day that I do not want to insert a line or the current date. For example: I have 2 tabs that I do not want a Sunday date.

Can this be accomplished??

Thanks in advance!!

Steve
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try:

Code:
Sub test()
Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets
    With ws
        Select Case Weekday(Date)
            Case Is = 1 'if weekday is Sunday
                'if worksheet name is not one of these 2, insert row/date on all other sheets
                If .Name <> "No Sunday" And ws.Name <> "No Sunday2" Then
                    .Rows(10).EntireRow.Insert
                    .Range("A10").Value = Date
                End If
            Case Else 'if weekday is anything but Sunday
                'insert row/date in all sheets
                .Rows(10).EntireRow.Insert
                .Range("A10").Value = Date
        End Select
    End With
Next ws

End Sub
 
Upvote 0
Thank you!! Additional Question

Can you also add a line that will drag all the formulas from line 11 (previously line 10) cell ranges b10 through co10.

Thanks again,

Steve
 
Upvote 0
Try:
Code:
Sub test()
Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets
    With ws
        Select Case Weekday(Date)
            Case Is = 1
                If .Name <> "No Sunday" And ws.Name <> "No Sunday2" Then
                    .Rows(10).EntireRow.Insert
                    .Range("A10").Value = Date
                    .Range("B11:CO11").AutoFill Destination:=.Range("B10:CO11")
                End If
            Case Else
                .Rows(10).EntireRow.Insert
                .Range("A10").Value = Date
                .Range("B11:CO11").AutoFill Destination:=.Range("B10:CO11")
        End Select
    End With
Next ws

End Sub
 
Upvote 0
Error Message "cannot change part of an array"

Thanks for the code.

I get the above error message: Cannot change part of an array

Sub test()
Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets
With ws
Select Case Weekday(Date)
Case Is = 1
If .Name <> "No Sunday" And ws.Name <> "No Sunday2" Then
.Rows(10).EntireRow.Insert
.Range("A10").Value = Date
.Range("B11:CO11").AutoFill Destination:=.Range("B10:CO11")
End If
Case Else
.Rows(10).EntireRow.Insert 'This where the program stops'
.Range("A10").Value = Date
.Range("B11:CO11").AutoFill Destination:=.Range("B10:CO11")
End Select
End With
Next ws

End Sub

Thanks for all your help

Steve
 
Upvote 0
That code is working for me in both 2002 and 2003...

What line of the code is highlighted when the error occurs?
Are you using array formulas in the cells?
 
Upvote 0

Forum statistics

Threads
1,203,261
Messages
6,054,424
Members
444,724
Latest member
madhink

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