samanthareed2012

New Member
Joined
May 2, 2014
Messages
9
I'm very new to VBA. I've done my best to copy and paste from forums like this one and then alter it for my purposes, but obviously that can only get you so far.
I am trying to get the following code to repeat adding the rows and the text for every time the word "Date" appears in column c. I keep getting the error message: Next without For, which I don't understand because I have both. Please help!


Code:
Dim GCell As Range
    Dim Txt As Range
    Dim Rng As Long
    Dim LastRow As Long
    Dim ColumnC As Range
    
'Find DATE fields and activate cells
For Each Txt In ColumnC
   With Txt
        If Txt = "Date" Then
        Set GCell = ActiveSheet.Cells.Find(Txt)
        Range("$A$1:$AG$28").Find(Txt).Activate

'Move one cell to the right and enter text
  ActiveCell.Offset(0, 1).Select
  ActiveCell.Value = "Enter valid date: 12/MAY/2014" & ActiveCell.Value
  ActiveCell.Offset(0, 1).Select
  ActiveCell.Value = "Accepts Input"
'Insert new row
    Rng = 1
    Range(ActiveCell.Offset(1), ActiveCell.Offset(Val(Rng), 0)).EntireRow.Insert
    
'Move one cell down and enter text
  ActiveCell.Offset(1, -1).Select
  ActiveCell.Value = "Enter year prior to the lower bound: 12/MAY/2012"
    
  ActiveCell.Offset(0, 1).Select
  ActiveCell.Value = "Receive message: The year given is outside the expected date range."
    
  Rng = 1
  Range(ActiveCell.Offset(1), ActiveCell.Offset(Val(Rng), 0)).EntireRow.Insert
    
'Next row
  ActiveCell.Offset(1, -1).Select
  ActiveCell.Value = "Enter year equal to the lower bound: 12/MAY/2014"
  
  ActiveCell.Offset(0, 1).Select
  ActiveCell.Value = "Accepts Input"
  
  Rng = 1
  Range(ActiveCell.Offset(1), ActiveCell.Offset(Val(Rng), 0)).EntireRow.Insert
    
'Next row
  ActiveCell.Offset(1, -1).Select
  ActiveCell.Value = "Enter year after to the upper bound: 12/MAY/2016"
   
  ActiveCell.Offset(0, 1).Select
  ActiveCell.Value = "Receive message: The year given is outside the expected date range."
   
  Rng = 1
  Range(ActiveCell.Offset(1), ActiveCell.Offset(Val(Rng), 0)).EntireRow.Insert

  Next Txt
 
Last edited by a moderator:

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Welcome the Board!

It looks like you haven't closed your With statement.

HTH,

EDIT: missed the If, but bbott didn't ;)
 
Last edited:
Upvote 0
Welcome to the board.

You need to end your IF and WITH statements first since they fall within the NEXT statement.
 
Upvote 0

Forum statistics

Threads
1,216,064
Messages
6,128,562
Members
449,458
Latest member
gillmit

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