PC Review


Reply
Thread Tools Rate Thread

Creating a variable range in Visual Basic

 
 
Deby
Guest
Posts: n/a
 
      10th Dec 2009
I've created a macro that will filter specific data from a sheet and copy
that filtered data to another sheet for a "watchlist". There are multiple
sheets to be filtered, and I'd like to be able to have the copied information
paste in at the end of the previous data on the "watchlist" sheet. I'm not
really good with Visual Basic, but can usually muddle my way through. Is
there code I can use to make the range variable, so the info will paste where
I need it to?
 
Reply With Quote
 
 
 
 
JLGWhiz
Guest
Posts: n/a
 
      10th Dec 2009
Add this near the beginning of your existing macro.

Dim lr As Long, sh2 As Worksheet
Set sh2 = Sheets("watchlist") 'Set the receiving sht to a variable
' The next line finds the last cell with data in col A and adds 1
lr = sh2.Cells(Rows.Count, 1).End(xlUp).Row + 1
'Your existing code here
Selection.Copy sh2.Range("A" & lr)

That last line can be modified. I used Selection because the actual code
being currently used was not posted, But that is the line that will put the
data on the "watchlist" sheet in the next blank cell of column A and will
post the entire copied range..


"Deby" <(E-Mail Removed)> wrote in message
news:40FEDE29-08A1-48D2-B02A-(E-Mail Removed)...
> I've created a macro that will filter specific data from a sheet and copy
> that filtered data to another sheet for a "watchlist". There are
> multiple
> sheets to be filtered, and I'd like to be able to have the copied
> information
> paste in at the end of the previous data on the "watchlist" sheet. I'm
> not
> really good with Visual Basic, but can usually muddle my way through. Is
> there code I can use to make the range variable, so the info will paste
> where
> I need it to?



 
Reply With Quote
 
Deby
Guest
Posts: n/a
 
      11th Dec 2009
This is the code that was created with the macro by using a criteria and
extract range (works better for the info requested):

Rows("21:3039").Select
Selection.Delete Shift:=xlUp
Range("A13").Select
Range("AR").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"A6:U8"), CopyToRange:=Range("A20:S20"), Unique:=False
Rows("21:21").Select
Selection.Insert Shift:=xlDown
Selection.ClearFormats
Range("A22").Select
Selection.CurrentRegion.Select
Selection.Copy
Sheets("Watchlist").Select
Range("A4").Select
Selection.End(xlDown).Select
Range("A6").Select
ActiveSheet.Paste
Sheets("Criteria-Extract").Select
Rows("21:21").Select
ActiveSheet.Next.Select
ActiveSheet.Next.Select
ActiveSheet.Next.Select
Sheets("Criteria-Extract").Select
Rows("21:4084").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Range("A13").Select
Range("MT").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"A6:U8"), CopyToRange:=Range("A20:S20"), Unique:=False
Rows("21:21").Select
Selection.Insert Shift:=xlDown
Selection.ClearFormats
Range("A22").Select
Selection.CurrentRegion.Select
Selection.Copy
Sheets("Watchlist").Select
Range("A4").Select
Selection.End(xlDown).Select
Range("A20").Select
ActiveSheet.Paste
Sheets("Criteria-Extract").Select

Thanks!!!

"JLGWhiz" wrote:

> Add this near the beginning of your existing macro.
>
> Dim lr As Long, sh2 As Worksheet
> Set sh2 = Sheets("watchlist") 'Set the receiving sht to a variable
> ' The next line finds the last cell with data in col A and adds 1
> lr = sh2.Cells(Rows.Count, 1).End(xlUp).Row + 1
> 'Your existing code here
> Selection.Copy sh2.Range("A" & lr)
>
> That last line can be modified. I used Selection because the actual code
> being currently used was not posted, But that is the line that will put the
> data on the "watchlist" sheet in the next blank cell of column A and will
> post the entire copied range..
>
>
> "Deby" <(E-Mail Removed)> wrote in message
> news:40FEDE29-08A1-48D2-B02A-(E-Mail Removed)...
> > I've created a macro that will filter specific data from a sheet and copy
> > that filtered data to another sheet for a "watchlist". There are
> > multiple
> > sheets to be filtered, and I'd like to be able to have the copied
> > information
> > paste in at the end of the previous data on the "watchlist" sheet. I'm
> > not
> > really good with Visual Basic, but can usually muddle my way through. Is
> > there code I can use to make the range variable, so the info will paste
> > where
> > I need it to?

>
>
> .
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Visual basic problem: Object variable or With block variable not set. angliaboy Webmaster / Programming 0 26th Nov 2009 08:20 PM
setting variable direction in visual basic blkane Microsoft Excel Programming 4 21st Jan 2008 06:38 PM
VBE (visual Basic Editor) variable Hellboy Microsoft Excel Programming 2 10th Jul 2005 11:39 PM
Visual Basic Variable DIM as Date Chris Microsoft Access VBA Modules 2 18th Sep 2003 02:48 AM
Visual Basic Range variable Andrew Johnson Microsoft Excel Programming 2 25th Aug 2003 05:18 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:50 PM.