Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-03-22
4
Medium Priority
?
709 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 400 total points
ID: 39948007
try using .cells(3,2) = "hello"    (drop .range)
0
 
LVL 23

Accepted Solution

by:
Ejgil Hedegaard earned 1600 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

916 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