Solved

The Offset property in VBA

Posted on 2004-03-21
2
2,561 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 81

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 81

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Macro which automatically sends attachment to Outlook 14 70
Add a task in Outlook from access 11 39
Advice in Xamarin 21 79
Send outlook email from VBS Script 2 30
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

809 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