Solved

VBA to add a value to an excel cell at a defined row and column position

Posted on 2014-03-22
4
700 Views
Last Modified: 2014-03-22
I am building a lookup list of temporary sheet names (in colA), and this code aims to add a new sheet name to bottomRow+1 (ie to append the sheet name to the list in colA).
In colB: False means the data in the respective temp sheet has not been changed; True means it has been changed.

Since the length of the list is dynamic, I chose to add sheet names to the list using a .Range(Cells) syntax. But I cant get it to function.

Here is my code, with a set of failed lines, using a dynamic cell range. The only thing that worked was the static .Range("A3"):

Dim bottomRow as integer
Dim rn_bottomRow as range

Set rn_bottomRow =    
            ThisWorkbook.Worksheets("TempList_Shts_VisioData").Range("A1").End(xlDown)
            bottomRow = rn_bottomRow.row  ' this identifies the last occupied row ok
 
            With Worksheets("TempList_Shts_VisioData")
                .Activate 'sheet IS activated
                .Range(Cells(3, 2)) = "hello"                              'this line failed      
                .Range(.Cells(bottomRow + 1, 2)) = "hello"       'this line failed  
                .Range(.Cells(3, 2)) = "hello"                             'this line failed  
                .Range(.Cells(rn_bottomRow.row + 1, 3)) = "False"     'this line failed  
                .Range(.Cells(3, 2)) = "hello"                             'this line failed                      
                 .Range("B3") = "False" 'this line worked but I need a dynamic cell reference!!!
            End With

Evidence that .with syntax is working:
 .Activate 'sheet IS activated:  TempList_Shts_VisioData becomes the active sheet
 .Name returned the correct sheet name

I cant fathom my error...
What is the correct syntax for Range using Cells property - for active and inactive sheets, respectively?

This code is written within a class module; does that matter?
An explanation of the principles behind a solution would be appreciated.

Thanks
Kelvin
0
Comment
Question by:Kelvin4
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 7

Assisted Solution

by:COACHMAN99
COACHMAN99 earned 100 total points
ID: 39948007
try using .cells(3,2) = "hello"    (drop .range)
0
 
LVL 23

Accepted Solution

by:
Ejgil Hedegaard earned 400 total points
ID: 39948045
If you define a variable for the sheet
Dim ws as Worksheet
And assign the sheet to that
Set ws = ThisWorkbook.Worksheets("TempList_Shts_VisioData")
When used, ws is identical to writing ThisWorkbook.Worksheets("TempList_Shts_VisioData") every time, and you get help with what is possible to do with a worksheet when pressing . (dot) after ws.

To get the last row in column A, you don’t have to find the cell first, use
bottomRow = ThisWorkbook.Worksheets("TempList_Shts_VisioData").Range("A1").End(xlDown).Row
or
bottomRow = ws.Range("A1").End(xlDown).Row

Writing to a cell can be done without activating or selecting the sheet, you can even write to a hidden sheet, but to select a range on the sheet, copy or format cells, the sheet has to be active.

Range(Cell) syntax is Range(Cells(3,2), Cells(5,2)), which means StartCell, EndCell in the range
For a single cell it has to be Range(Cells(3,2), Cells(3,2))
Writing a value to a cell can be
ws.Cells(3,2) = "hello"
or
ws.Range(Cells(3,2), Cells(3,2)) = "hello"
The Range(Cell) syntax is good when you do something with the cell (needed with a range) like this.
ws.Range(Cells(3,2), Cells(3,2)).Interior.Color = 255
ws.Cells(3,2).Interior.Color = 255 does the same for a single cell, but no help when adding .Interior.Color
0
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 39948101
it works fine as:
 .cells(3,2) = "hello"    (drop .range)
0
 

Author Closing Comment

by:Kelvin4
ID: 39948272
Thanks to both.

hgholt: I appreciated your explanation, which enabled me to make a functional xl demo file of exercises.

Cheers
Kelvin
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question