Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 78
Default Assign cell value to variable

I know this is very basic but I have two workbooks open and I declare (DIM)
and assign all workbook names and worksheet names. I simply want to take
values from specfic cells in workbookA.WorksheetA.Cell A1 and assign it to a
variable and then goto workbookB.etc. and put the value in cell A1.

How can I do this efficiently and then also how can I do this using variables?

Thanks....
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Assign cell value to variable

Dim shA as Worksheet
Dim shB as Worksheet
set shA = WorkBooks("WorkbookA.xls").Worksheets("WorksheetA" )
set shB = WorkBooks("WorkbookB.xls").Worksheets("WorksheetB" )

for i = 1 to 10
for j =1 1 to 5
shA.Cells(i,j).Value = shB.Cells(i,j).Value
Next j
Next i

or
shA.Range("A1:F10").Value = shB.Range("A1:F10").Value

to use a variable

for i = 1 to 10
for j =1 1 to 5
v = shB.Cells(i,j).Value
shA.Cells(i,j).Value = v
Next j
Next i

--
Regards,
Tom Ogilvy




"Rookie_User" wrote:

I know this is very basic but I have two workbooks open and I declare (DIM)
and assign all workbook names and worksheet names. I simply want to take
values from specfic cells in workbookA.WorksheetA.Cell A1 and assign it to a
variable and then goto workbookB.etc. and put the value in cell A1.

How can I do this efficiently and then also how can I do this using variables?

Thanks....

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Assign cell value to variable

The book where the code is running is always Thisworkbook so there really is
no need to declare an object for it. For this example I will do it anyway...

Dim wbkA As Workbook
Dim wbkB As Workbook
Dim wksA1 As Worksheet
Dim wksB1 As Worksheet
Dim var As Variant

Set wbkA = ThisWorkbook
On Error Resume Next
Set wbkB = Workbooks("BookB.xls")
On Error GoTo 0
If wbkB Is Nothing Then Set wbkB = Workbooks.Open("C:\BookB.xls")

Set wksA1 = wbkA.Sheets(1)
Set wksB1 = wbkB.Sheets(1)

'make BookB Sheet(1) cell A1 = BookA Sheet(1) cell A1
wksB1.Range("A1").Value = wksA1.Range("A1").Value
'make BookB Sheet(1) cell B1 = BookA Sheet(1) cell B1 with a variable
var = wksA1.Range("B1").Value
wksB1.Range("B1").Value = var

--
HTH...

Jim Thomlinson


"Rookie_User" wrote:

I know this is very basic but I have two workbooks open and I declare (DIM)
and assign all workbook names and worksheet names. I simply want to take
values from specfic cells in workbookA.WorksheetA.Cell A1 and assign it to a
variable and then goto workbookB.etc. and put the value in cell A1.

How can I do this efficiently and then also how can I do this using variables?

Thanks....

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
Assign value to variable rroach Excel Discussion (Misc queries) 1 July 13th 05 05:24 AM
How can I assign a range starting cell based on a variable locati. feman007 Excel Discussion (Misc queries) 1 March 9th 05 11:41 PM
How can I assign a range starting cell based on a variable locati. feman007 Excel Worksheet Functions 3 March 9th 05 11:40 PM
Change cell formula & assign to VBA variable... Kevin Lyons Excel Programming 0 February 5th 05 06:58 AM
Assign relative cell reference to variable Hardy[_7_] Excel Programming 0 September 7th 04 10:57 AM


All times are GMT +1. The time now is 01:46 PM.

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"