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
198 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

757 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

23 Experts available now in Live!

Get 1:1 Help Now