473,785 Members | 3,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retrieving data in certain cells in Excel to MS Access usign VBA

48 New Member
hello,

been searching for a solution on this but all i get are advices for importing data from excel directly to MS Access DB.

my case here is different...the sheet in Excel file is not ready for importing...
the data are scattered from different cells from different sheets...
this is because these sheets are automated for their own role...

i just need to get data from a certain cell using VBA den save the data to MS Access DB.

Example:
the data are in Sheet 1 in A1, A2, C5, F5...
others are in Sheet 2 in B4, B5, B6, G7...

you see, i need to get each data from them...
then save them to MS Access DB...
the sheet is not ready for db import...

does anyone knows how to retrieve data in each cell?
then save data one-by-one to MS Access DB...
or are there other better ways?
(must not change the format of the excel)


hoping to hear good advices from the experts and the experienced,
keirnus
Aug 11 '08 #1
3 2543
NeoPa
32,578 Recognized Expert Moderator MVP
Using Application Automation it's relatively straightforward to get the data from Excel as long as you know where the data is.

Saving "it" into Access though, is just plain meaningless. Where and how do you want these disparate items of data to be saved?
Aug 11 '08 #2
keirnus
48 New Member
Using Application Automation it's relatively straightforward to get the data from Excel as long as you know where the data is.

Saving "it" into Access though, is just plain meaningless. Where and how do you want these disparate items of data to be saved?
Thanks NeoPa for the reply.
But the data in Excel are scattered in different cells.

Example:
I have one table in Access called "tblTable".
The columns are Col1, Col2, Col3, Col4, Col5, Col6.
The data to be inserted in this table are in different cells in Excel.
The locations are Sheet1 in A1, A2, C5 then Sheet2 in B4, B5, G7.

Let's say:
[tblTable].Col1 = Sheet1!A1
[tblTable].Col2 = Sheet1!A2
[tblTable].Col3 = Sheet1!C5
[tblTable].Col4 = Sheet2!B4
[tblTable].Col5 = Sheet2!B5
[tblTable].Col6 = Sheet2!G7

How do i get the data in Excel one by one?

I'm sorry but i'm kinda new on this stuff.
But I kinda liking doing this already. =)
Just got bumped on this though. =(

Hope you could help give advices.
Aug 11 '08 #3
NeoPa
32,578 Recognized Expert Moderator MVP
Assuming then, that you want to add a new record into the table every time this process is run, you need to include the following logic.

NB. This is not the whole program or code, but it does provide the building blocks you will need.

Firstly, using the Application Automation linked in the earlier post, retrieve the specific cells that you need into six variables that will not lose scope. These variables (or the data in them) will be required later on to insert into the table.

I will assume, for the moment, that the first three items are numeric, string and date data respectively. I doubt your actual data is, but it illustrates how each should correctly be handled. ==> Col1=Numeric; Col2=String; Col3=Date. The remaining three columns are all Numeric. Numeric data should all be processed in a similar way, but which data type you use to store the data in may depend on the precise nature of the data you expect. I will use Double variables for all numeric items in the example.

At the top of a module (where the code to add the data into the table is to be found) you will need to define a constant which is the SQL string used to add in the data. I also include in here the Dim statements for the six variables as I've described.
Expand|Select|Wrap|Line Numbers
  1. Private Const conUpdate As String = _
  2.                   "INSERT INTO tblTable" & _
  3.                   "(Col1,Col2,Col3,Col4,Col5,Col6) " & _
  4.                   "VALUES(%A,%B,%C,%D,%E,%F)"
  5. Private dblC1 As Double
  6. Private strC2 As String
  7. Private datC3 As Date
  8. Private dblC4 As Double
  9. Private dblC5 As Double
  10. Private dblC6 As Double
You will then have some code to get all the data from Excel. We can dig into this a little deeper if you find you need to, but for now I will leave you with the link and expect you to work out the basics of how this is done. I believe all the info you need is there.

After that there will be code to populate a new record with this data. Note very well, the difference in how the different fields are handled. SQL is particularly complicated where dates are concerned. This code is designed for SQL. It does NOT depend in the least on which country the code is run in. SQL date format is global and independent of local formats (See Literal DateTimes and Their Delimiters (#)).
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String, strWork As String
  2.  
  3. 'Insert your code to populate the vales in dblC1 through dblC6
  4.  
  5. strSQL = conUpdate
  6. 'Formate for numeric values
  7. strSQL = Replace(strSQL, "%A", dblC1)
  8. 'Formate for string values
  9. strSQL = Replace(strSQL, "%B", "'" & strC2 & "'")
  10. 'Formate for numeric values
  11. strSQL = Replace(strSQL, "%C", Format(datC3, '\#m/d/yyyy\#'))
  12. strSQL = Replace(strSQL, "%D", dblC4)
  13. strSQL = Replace(strSQL, "%E", dblC5)
  14. strSQL = Replace(strSQL, "%F", dblC6)
  15.  
  16. Call DoCmd.RunSQL(SQLStatement:=strSQL, UseTransaction:=False)
Aug 12 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
13468
by: Martin | last post by:
There is an Access table on the network. 15 users who do not have Access are connected to the network. Is there a way for each user to be able to enter one or more rows containing 3 or 4 columns to Excel on his machine and then press a button or something and append that data to the Access table on the network? Any suggestions on what the code would be? Thank you very much! Martin
11
2274
by: Heather | last post by:
I need suggestions on possible ways to implement the following: 10 users on a network need to infrequently add data to a backend database on the network. The data would be a couple of records containing maybe a half dozen fields. The users do not have Access. Would adding a form to the backend database for these users be a viable option considering they would need to open the form over the network? Thanks for all anticipated help!
5
4087
by: Jonny | last post by:
Hello, I have created a button on my form which when pressed does the following : 1) Run a pre-defined Macro, which is picking up a query and running my data to excel. However, I need the data to export into Excel in a certain format, i.e it needs to begin importing at cell A4, and in truth it would be great
2
46478
by: Chris Bellini | last post by:
Greetings! I'm developing a C# application that needs to read some data from a selected XLS file. I've used VB in the past to automate Excel but this is the first time I've used C#. Back in VB, I was able to retrieve a cell's contents via something like this: Set objExcellApp = CreateObject("Excel.Application") Set objWorkbook = objExcel.Workbooks.Open(strPathToXLS) ' retrive the contents of A1 strData = objExcel.Cells(1, 1).Value
2
2094
by: Hans Wien | last post by:
I have to write a lot of data to excel for analysts who use it further from there but would like to use managed C++ and not C# since a lot of code is involved which already exists now in managed C++. In C# I could handle it easily as you can see in this sample: using Excel = Microsoft.Office.Interop.Excel; .... private Excel.Application thisApplication; private Excel.Workbook myWorkbook1;
0
14423
by: Grip | last post by:
Hi, I have gone throught the group and Microsoft's online help and have seen many suggestions but I am still seeking clarity: 1. I have an excel spreadsheet. Column A contains text that may be greater than 255 characters. 2. I have an access database. I link (not import) to the contents of the excel spreadsheet. In the design view in access, Column A has the data type "memo".
0
1903
by: coony | last post by:
Hi everyone, I got an annoying thing going on. I've got an MSSQL db which is filled with different data, used by another program (T-Plan). I should read some table to import in another DB. The DB I have to read, contains als BLOB files. In Excel I created a connection string, can read all fields & values as shown in the DB, but I can't manage to convert the Blobs to Strings (which is the data in the BLOBs). I tried all the next, but...
7
5770
by: semijoyful | last post by:
OS: Win XP SP2 Access version: 2003 Excel version: 2003 I am new at this, as I am sure you have gathered from this post title:) I am working on a form where users can input data in Access and at the end of the day be able to press a button on the form that would update that data to a single exel spreadsheet. The following is the code for the module. When I click on the button in the form, It takes me to VBS Debugger. I then type...
1
1830
by: rishiyo | last post by:
Good ones!! I want to retrieve and add datas with excel. I've tried one by adding two textboxes and two command buttons. THE code is: 'do declare these variables you need to add a reference'to the microsoft excel 'xx' object library. 'you need two text boxes and two command buttons'on the form, an excel file in c:\book1.xls Dim xl As New Excel.Application Dim xlsheet As Excel.Worksheet Dim xlwbook As Excel.Workbook Private Sub...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.