ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Simple VBA Macro to import Data (https://www.excelbanter.com/excel-programming/402663-simple-vba-macro-import-data.html)

Eric

Simple VBA Macro to import Data
 
Hello,

I was looking for some help writing a simple macro. It would import data
from a .csv file and write it to my specific cells in my open worksheet.

For instance, I would click a button on my spreadsheet and it would then
import data from C:\Documents and Settings\Eric\Desktop\Import\info.csv
then it would copy the value only from B1 to a named cell "First_Name" the
process would look something like this

copy
B1="First_Name"
B2="Last_Name"

could anybody help me with this? I usually don't write macros but I have
been getting more and more work that involves simple ones like this. Thanks
for any help. Eric

[email protected]

Simple VBA Macro to import Data
 
On Dec 13, 9:28 am, Eric wrote:
Hello,

I was looking for some help writing a simple macro. It would import data
from a .csv file and write it to my specific cells in my open worksheet.

For instance, I would click a button on my spreadsheet and it would then
import data from C:\Documents and Settings\Eric\Desktop\Import\info.csv
then it would copy the value only from B1 to a named cell "First_Name" the
process would look something like this

copy
B1="First_Name"
B2="Last_Name"

could anybody help me with this? I usually don't write macros but I have
been getting more and more work that involves simple ones like this. Thanks
for any help. Eric


Hi Eric,
Try the code below.
Public Sub ImportData()
Dim wkb As Workbook
Dim csv As Workbook
Dim sData As String
Set csv = Workbooks("C:\Documents and Settings\Eric\Desktop\Import
\i-nfo.csv")
Set wkb = ThisWorkbook 'Destination workbook
sData = csv.Sheets("Sheet1").Range("B1:B1")
wkb.Sheets("Sheet1").Range("A2:A2").Value = sData 'cell
First_name
End Sub
Repeat for Last_name by defining a Dim statement and last 2 statements

Regards
trevosef


All times are GMT +1. The time now is 11:20 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com