Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

Using VBA in Excel, how can I copy a numeric text value down a column without entering as a series?

I am trying to copy a number as text down a column but my VBA code is writing the values in a series.  In the attached example, I want to copy the string "301" down a formatted text column but Excel writes "301", "302", "303" etc.  How do I do it? InsertOfficeNo.xlsm
Sub I_InsertOfficeNo()
'
' I_InsertOfficeNo Macro
' Inserts OfficeNo in COL D
'
Dim lastrow As Integer
Dim officeNo As String

lastrow = Cells(Rows.Count, "H").End(xlUp).Row
officeNo = 301

Sheets("Sheet1").Activate
Columns("D:D").Insert
    Range("D2").Value = officeNo
    Range("D2").AutoFill Destination:=Range("D2:D" & lastrow), Type:=xlFillDefault
    Range("D1").Value = "Office"
   
End Sub

Open in new window

0
thutchinson
Asked:
thutchinson
  • 2
1 Solution
 
zorvek (Kevin Jones)ConsultantCommented:
Use:

    Range("D2").AutoFill Destination:=Range("D2:D" & lastrow), Type:=xlFillCopy

Kevin
0
 
zorvek (Kevin Jones)ConsultantCommented:
More specifically, just set the value:

Sub I_InsertOfficeNo()
'
' I_InsertOfficeNo Macro
' Inserts OfficeNo in COL D
'
Dim lastrow As Integer
Dim officeNo As String

lastrow = Cells(Rows.Count, "H").End(xlUp).Row
officeNo = 301

Sheets("Sheet1").Activate
Columns("D:D").Insert
    Range("D2:D" & lastrow).Value = officeNo
    Range("D1").Value = "Office"
   
End Sub

Kevin
0
 
thutchinsonAuthor Commented:
Thanks for helping a rookie Kevin!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now