Solved

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

Posted on 2011-02-23
3
200 Views
Last Modified: 2012-05-11
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
Comment
Question by:thutchinson
  • 2
3 Comments
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 500 total points
ID: 34965373
Use:

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

Kevin
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 34965392
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
 

Author Closing Comment

by:thutchinson
ID: 34965589
Thanks for helping a rookie Kevin!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now