Solved

The Offset property in VBA

Posted on 2004-03-21
2
2,557 Views
Last Modified: 2012-06-21
I've working on a stock application and have written this code for a user form which adds new products to a sheet. The code I have for the form is as follows:

Private Sub Add_click()
Application.ScreenUpdating = False
StockList.Range("Newinfo").Select
Selection.EntireRow.Insert
StockList.Range("Newinfo").Offset(-1, 0).Value = Supplier.Value
StockList.Range("Newinfo").Offset(-1, 1).Value = Product_Name.Value
StockList.Range("Newinfo").Offset(-1, 2).Value = Order_Code.Value
StockList.Range("Newinfo").Offset(-1, 3).Value = Price.Value
StockList.Range("Newinfo").Offset(-1, 4).Value = Stock_Available.Value
Addprod.Hide
Application.ScreenUpdating = True
End Sub

I have five colomns on this sheet, and so I want data from each field on the form to go into one colomn respectively. The problem I'm having is that it enters the data right for all 5 (e.g. A20-E20), but then continues to insert data from the last field 'Stock_available' on the user form into F20-I20. Why Does it repeat the input just for the 'Stock_Available.Value'. Is there something wrong with this line:

StockList.Range("Newinfo").Offset(-1, 4).Value = Stock_Available.Value

Thanks in advance.
0
Comment
Question by:Bitemarx48
  • 2
2 Comments
 
LVL 80

Accepted Solution

by:
byundt earned 75 total points
ID: 10645351
If Range("NewInfo") is bigger than a single cell, each of the offset instructions is trying to populate a range equivalent in size with the value.
Range"A1:A5").Offset(0,2)=1     'Fills C1:C5 with 1

If I understand your intent and if Range("NewInfo") refers to cells A21:E21, you could try the following:
StockList.Range("Newinfo").Offset(-1, 0)=Array(Supplier,Product_Name,Order_Code,Price,Stock_Available)
0
 
LVL 80

Expert Comment

by:byundt
ID: 10645711
Bitemarx48,
Welcome to Experts Exchange!

This question probably would probably have gotten answered quicker in the Excel TA, but thanks for the grade anyway.
Brad
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

744 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

9 Experts available now in Live!

Get 1:1 Help Now