Solved

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

Posted on 2014-03-22
4
692 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
  • 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 21

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

919 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now