Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I select a row using a range variable?

Status
Not open for further replies.

evild

Technical User
Nov 25, 2002
71
GB
I have the following code:

I have the question at the end of code. It is well commented so should be eay to follow.

Code:
Dim cellrng As Range
For Each cellrng In Range("A:A")
  checkdata = cellrng.Value
  If Not IsEmpty(checkdata) Then
    'Check that the data is not already in sheet1
    If presence(checkdata, sheet1, sheet2b) = False Then
       'Copy Row from sheet1 to sheet2b with defaults
       Call AddData(cellrng, sheet1, sheet2b)
       MsgBox ("New Data Added!")
    End If
  End If
Next cellrng

...
...

    Public Sub AddData(cellrange As Range, sprdsheet As String, sprdsheet2 As String)
    ' Copies an entire row in spredsheet2 and pastes it into the next available row in sprdsheet
         
         'Move to sheet where data resides
         Windows(sprdsheet2).Activate
         'Select current row and copy
         cellrange.Select
         Selection.Copy
         'Move to sheet where row is to be copied to
         Windows(sprdsheet).Activate
         'Move to the next available empty row
         Range("A1").Activate
         'Going to the last populated row
         Selection.End(xlDown).Select
         'and then down one
         Range("A" & ActiveCell.Row + 1).Activate
         'Paste the row daqta into current selected row
         ActiveSheet.Paste
         
         'Set default values of newly added row data
         'AddDefaults(blah blah)
         'MsgBox ("Defaults added")
    

    End Sub


As you can see, I am copying a cell in sheet2b into sheet1 using the AddData procedure.
However, I would now like to copy the entire row instead of a single cell. How do I do this?

The AddData procedure takes the cellrng variable from the loop that goes throug column A in sheet2.

Any help would be great.


Note: My VB skills are quite novice.
 
you can use this to select a full row

Rows("1").Select

this selects the whole of row 1
then selection.copy
 
I know of this method, but I would like to use the row that the loop is currently on [defined by cellrng here]
I could add a counter in the loop i suppose.
 
I had assumed that there would be a way to select the entire row using the cell that is currently selected. Maybe this is not the case.
 
use cellrng.row
so rows(cellrng.row).entirerow.select
should work nicely for you

Also:
'Move to the next available empty row
Range("A1").Activate
'Going to the last populated row
Selection.End(xlDown).Select
'and then down one
Range("A" & ActiveCell.Row + 1).Activate
could be changed to:
Range("A1").Offset(Range("A1").End(xlDown).Row, 0).Select

Rgds, Geoff
Quantum materiae materietur marmota monax si marmota monax materiam possit materiari?
Want the best answers to your questions ? faq222-2244
 
Thanks!! That has made things work very well now. I would never have got that by myself.

 
Even simpler:

cellrng.entirerow.select
and
range("A1").end(xldown).offset(1,0).select


Rob
[flowerface]
 
woo yay - Rob's back and correcting me already ;-)

Rgds, Geoff
Quantum materiae materietur marmota monax si marmota monax materiam possit materiari?
Want the best answers to your questions ? faq222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top