Solved

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

Posted on 2014-03-22
4
699 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 22

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

752 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