Solved

vba autofill range selection and fill

Posted on 2014-04-15
11
464 Views
Last Modified: 2014-04-16
All,

I keep getting errors when trying to execute the below code. Can anyone see my problem?

With shtTo

Dim from As Range
        Dim too As Range
        
        'specifying addresses
        copXrow = xRows - 1
        copxRows = shtTo.Cells(1).CurrentRegion.Columns(1).SpecialCells(xlCellTypeVisible).Count
        
        'setting from range
        Set from = Range("M" & copXrow & ":" & "EA" & copXrow)
        
        'setting to range
        Set too = Range("M" & lastrow & ":" & "EA" & lastrow)
        
        'autofill from too
        Range("from").AutoFill Destination:=shtTo.Range("too")

End With

Open in new window

0
Comment
Question by:discogs
  • 5
  • 4
  • 2
11 Comments
 
LVL 23

Expert Comment

by:Michael74
ID: 40003178
What error are you getting?

Michael
0
 

Author Comment

by:discogs
ID: 40003188
On line 17. The error "Method 'Range' of object'_Worksheet failed

Does that help?

It seems so stupid to me...
0
 

Author Comment

by:discogs
ID: 40003189
I even changed line 17 to read:

Range("from").AutoFill Destination:=Range("too")

Open in new window

An I got Method Range of Object Global failed error as well...
0
 
LVL 23

Expert Comment

by:Michael74
ID: 40003192
OK I see the error. when you declare "from" it is a range object not a named range


Change the line to

from.AutoFill Destination:=shtTo.too

Open in new window

0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 40003195
Change line 17 to

        from.AutoFill Destination:=Range(from, too)

to get rid of the error but I do not think this is what you would want. Can you explain what you want to achieve?
0
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.

 

Author Comment

by:discogs
ID: 40003200
Okay.

I have not changed anything because you might be able to tell me how to do it better.

Essentially, I copy a large dynamic dataset into a spreadsheet within one workbook.

In doing so, I know the row number to paste to, I also know the number of records I am pasting. For example, rngTo = A150 and record count = 78.

After pasting the records, I then select all records including and out to the right of column(M). After this, I autofill the row I selected (in the above example it would be M149 to EA149) and copy the whole lot down to the bottom of my records as displayed in columns(A through L).

Is there a better way?
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 40003213
Can you make my life easier by uploading a few-row fake sample?...unless Michael74 or anyone else has understood it well and can set up his own example.
0
 

Author Comment

by:discogs
ID: 40003230
Done.

Please note that I have highlighted and inserted a formula in row 150 as an example.

All formulas from column(M) through to column(EA) require copying all the way to the bottom of my data located in columns(A ~ L) respectively.

Although column M is a static starting point, the end column (EA) is not. So it would be really beneficial if the solution could be dynamic enough to compensate for growth.

TA
Book1.xlsx
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 40003359
This would find the row 150 using the formula
Set strtSrc = Range("M1").End(xlDown)

Open in new window

and the last column using the formula
Set endSrc = Range("M1").End(xlDown).End(xlToRight)

Open in new window

If this is different from what you want then write back.

Sub copym2()
Dim strtSrc As Range
Dim endSrc As Range
Dim strtTgt As Range
Dim endTgt As Range
Set strtSrc = Range("M1").End(xlDown)
Set endSrc = Range("M1").End(xlDown).End(xlToRight)
Set strtTgt = Range("M1").End(xlDown).Offset(1)
Set endTgt = Range("A1").End(xlDown).Offset(, 12)
Range(strtSrc, endSrc).Copy Range(strtTgt, endTgt)
End Sub
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
ID: 40003362
Taking advantage of the used variables this can be reduced to

Sub a()
Dim strtSrc As Range
Dim endSrc As Range
Dim strtTgt As Range
Dim endTgt As Range
Set strtSrc = Range("M1").End(xlDown)
Set endSrc = strtSrc.End(xlToRight)
Set strtTgt = strtSrc.Offset(1)
Set endTgt = Range("A1").End(xlDown).Offset(, 12)
Range(strtSrc, endSrc).Copy Range(strtTgt, endTgt)
End Sub
0
 

Author Closing Comment

by:discogs
ID: 40003574
Outstanding assistance. Thanks heaps it does exactly what I expected. TA
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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

863 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

24 Experts available now in Live!

Get 1:1 Help Now