View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
[email protected] trevosef@gmail.com is offline
external usenet poster
 
Posts: 14
Default 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