Autofill Cells with Fixed Values

rhmkrmi

Active Member
Joined
Aug 17, 2012
Messages
348
Office Version
  1. 365
Platform
  1. Windows
Hello, Is there a formula or code that can be used to auto populate certain texts with certain values?

In rows 15 to 26, I am populating column B odd number cells with FROM and even number cells with TO. When users insert rows in this range, I want the FROM and TO to be auto filled instead of users typing them in.

Thank you.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Here's some change event code you can try. Assumes that col B starting at B15 contains only the two entries "FROM" and "TO", and allows the range occupied by those values to grow or shrink as the user inserts or deletes rows within it. Also ensures that the two values always appear in pairs so the occupied range always ends with a "TO".

To install sheet code:
1. Right-click the worksheet tab you want to apply it to and choose 'View Code'. This will open the VBE window.
2. Copy the code below from your browser window and paste it into the white space in the VBE window.
3. Close the VBE window and Save the workbook. If you are using Excel 2007 or a later version do a SaveAs and save it as a macro-enabled workbook (.xlsm file extension).
4. Make sure you have enabled macros whenever you open the file or the code will not run.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim R As Range
Set R = Range("B15:B" & Cells(Rows.Count, "B").End(xlUp).Row)
If Not Intersect(R, Target) Is Nothing Then
    Application.EnableEvents = False
    With R(1).Resize(2, 1)
        .Value = Application.Transpose(Array("FROM", "TO"))
        .AutoFill Destination:=R, Type:=xlFillDefault
    End With
End If
If R(R.Rows.Count).Value = "FROM" Then R(R.Rows.Count).Offset(1, 0).Value = "TO"
Application.EnableEvents = True
End Sub
 
Upvote 0
Here's some change event code you can try. Assumes that col B starting at B15 contains only the two entries "FROM" and "TO", and allows the range occupied by those values to grow or shrink as the user inserts or deletes rows within it. Also ensures that the two values always appear in pairs so the occupied range always ends with a "TO".

To install sheet code:
1. Right-click the worksheet tab you want to apply it to and choose 'View Code'. This will open the VBE window.
2. Copy the code below from your browser window and paste it into the white space in the VBE window.
3. Close the VBE window and Save the workbook. If you are using Excel 2007 or a later version do a SaveAs and save it as a macro-enabled workbook (.xlsm file extension).
4. Make sure you have enabled macros whenever you open the file or the code will not run.
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim R As Range
Set R = Range("B15:B" & Cells(Rows.Count, "B").End(xlUp).Row)
If Not Intersect(R, Target) Is Nothing Then
    Application.EnableEvents = False
    With R(1).Resize(2, 1)
        .Value = Application.Transpose(Array("FROM", "TO"))
        .AutoFill Destination:=R, Type:=xlFillDefault
    End With
End If
If R(R.Rows.Count).Value = "FROM" Then R(R.Rows.Count).Offset(1, 0).Value = "TO"
Application.EnableEvents = True
End Sub
You can replace what I highlighted in red with this to compact your code a little bit...

[{"FROM";"TO"}]

Also what I highlighted in blue could be replaced with this...

R(R.Rows.Count + 1)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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