Solved

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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…

760 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

21 Experts available now in Live!

Get 1:1 Help Now