Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 76
Default Code to insert data from another workbook

In Workbook A, I have a macro that imports data from another source. This
data is added into the next available column. Up to now I have been going
into Workbook A and copying and pasting 2 cells from this new data into
pasting it into Workbook B. Prior to pasting this data into Workbook B, I
also manually insert 2 new cells so that the old data is moved one column to
the right. I'm looking for suggestions of code to automate this. Thank
you!!!!!!!!!!!
--
maryj
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Code to insert data from another workbook

Mary:
Assuming both workbooks are open when the macro runs, you could add
something like this to the macro code (where CopySheet and CopyRange give the
sheet name and range of cells you want to copy, PasteSheet and PasteRange
give the sheet name/column where you want to insert the cells, and using
"Workbook A and "Workbook B" as your workbook names - this really needs to be
the name as it appears in the title bar):

Workbooks("Workbook A").Sheets(CopySheet).Range(CopyRange).Copy
Workbooks("Workbook
B").Sheets(PasteSheet).Range(PasteRange).Insert(xl ShiftToRight)
Workbooks("Workbook
B").Sheets(PasteSheet).Range(PasteRange).PasteSpec ial(xlPasteValues)

Hope this does what you need.
K Dales


"maryj" wrote:

In Workbook A, I have a macro that imports data from another source. This
data is added into the next available column. Up to now I have been going
into Workbook A and copying and pasting 2 cells from this new data into
pasting it into Workbook B. Prior to pasting this data into Workbook B, I
also manually insert 2 new cells so that the old data is moved one column to
the right. I'm looking for suggestions of code to automate this. Thank
you!!!!!!!!!!!
--
maryj

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 76
Default Code to insert data from another workbook

Thanks! This looks close. My only question is that the data that is being
copied is always going to be one more column to the right than the last time
this was run. Is there a way to tell it to look at the next available column?
It would be in the same row each time.

Thanks

"K Dales" wrote:

Mary:
Assuming both workbooks are open when the macro runs, you could add
something like this to the macro code (where CopySheet and CopyRange give the
sheet name and range of cells you want to copy, PasteSheet and PasteRange
give the sheet name/column where you want to insert the cells, and using
"Workbook A and "Workbook B" as your workbook names - this really needs to be
the name as it appears in the title bar):

Workbooks("Workbook A").Sheets(CopySheet).Range(CopyRange).Copy
Workbooks("Workbook
B").Sheets(PasteSheet).Range(PasteRange).Insert(xl ShiftToRight)
Workbooks("Workbook
B").Sheets(PasteSheet).Range(PasteRange).PasteSpec ial(xlPasteValues)

Hope this does what you need.
K Dales


"maryj" wrote:

In Workbook A, I have a macro that imports data from another source. This
data is added into the next available column. Up to now I have been going
into Workbook A and copying and pasting 2 cells from this new data into
pasting it into Workbook B. Prior to pasting this data into Workbook B, I
also manually insert 2 new cells so that the old data is moved one column to
the right. I'm looking for suggestions of code to automate this. Thank
you!!!!!!!!!!!
--
maryj

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Code to insert data from another workbook

There are a few ways to do what you need, it depends on what else (if
anything) as adjacent to the cells you will be using.
Possibilities:
- The CurrentRegion property of the range will give a new range that
consists of all adjacent non-empty cells. Getting this to count your columns
will only work if there are no other adjacent cells that extend further, but
assuming that is OK you can count the columns in the CurrentRegion like this:
UsedColumns = Range(CopyRange).CurrentRegion.Columns.Count
- You could perhaps use the CountA worksheet function if there is nothing
else (ever) to the right of your column in that particular row:
UsedColumns = WorksheetFunction.CountA(CopyRange.EntireRow)
- You could use VBA to step to the right until it finds a blank:
ColNo = 1
While Not IsEmpty(CopyRange.Offset(0,ColNo)).Range("A1")
ColNo = ColNo + 1
Wend
....

Hopefully one of these methods can be adapted to your workbook.
"maryj" wrote:

Thanks! This looks close. My only question is that the data that is being
copied is always going to be one more column to the right than the last time
this was run. Is there a way to tell it to look at the next available column?
It would be in the same row each time.

Thanks

"K Dales" wrote:

Mary:
Assuming both workbooks are open when the macro runs, you could add
something like this to the macro code (where CopySheet and CopyRange give the
sheet name and range of cells you want to copy, PasteSheet and PasteRange
give the sheet name/column where you want to insert the cells, and using
"Workbook A and "Workbook B" as your workbook names - this really needs to be
the name as it appears in the title bar):

Workbooks("Workbook A").Sheets(CopySheet).Range(CopyRange).Copy
Workbooks("Workbook
B").Sheets(PasteSheet).Range(PasteRange).Insert(xl ShiftToRight)
Workbooks("Workbook
B").Sheets(PasteSheet).Range(PasteRange).PasteSpec ial(xlPasteValues)

Hope this does what you need.
K Dales


"maryj" wrote:

In Workbook A, I have a macro that imports data from another source. This
data is added into the next available column. Up to now I have been going
into Workbook A and copying and pasting 2 cells from this new data into
pasting it into Workbook B. Prior to pasting this data into Workbook B, I
also manually insert 2 new cells so that the old data is moved one column to
the right. I'm looking for suggestions of code to automate this. Thank
you!!!!!!!!!!!
--
maryj

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
HOW CAN I INSERT DATA BTW TWO WORKBOOK Kanmi Excel Worksheet Functions 1 July 2nd 09 05:33 AM
insert vba code to new workbook Jive Excel Worksheet Functions 2 March 5th 08 02:12 PM
How can I have data auto insert into another pg in workbook? beaker0103 Excel Worksheet Functions 0 August 17th 06 04:53 PM
Insert VBA code with a macro in a .xls file by workbook open event mihai[_3_] Excel Programming 8 July 29th 04 01:49 PM
insert data into macro code from text input field? john_t_h[_15_] Excel Programming 1 January 16th 04 03:14 AM


All times are GMT +1. The time now is 04:28 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"