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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB Excel: How do you paste a variable at a location?

Status
Not open for further replies.

SMich011

Programmer
Jun 17, 2009
1
US
Good afternoon,

I just started using VB in Excel to make a sheet to tabulate hours toward projects. Using a button I want to populate a set of cells with data from the sheet which I am doing with the following code:
ActiveCell = Range("B1")
ActiveCell.Offset(0, 1).Activate
ActiveCell = Range("F17") + Range("G17")
ActiveCell.Offset(0, 1).Activate
ActiveCell = Range("K1")
ActiveCell.Offset(0, 1).Activate
ActiveCell = Range("K2")
ActiveCell.Offset(0, 1).Activate
ActiveCell = Range("K3")

What I want to do is set variable 1= B1 , Variable2 =F17, variable3= G17, variable 4= var2+var3 etc. and then later paste variable1,2,3,4 to specific locations, say d1,d2,d3,d4.

How do I execute the paste portion of the command?
 



Hi,

You do not seem to be COPYING and PASTING anything. However, you are assigning values, which is not PASTING.

What I want to do is set variable 1= B1 , Variable2 =F17, variable3= G17, variable 4= var2+var3 etc. and then later paste variable1,2,3,4 to specific locations, say d1,d2,d3,d4.
Assign to an array.

I NEVER use ActiveCell for this kind of process.

Define the RANGE that you want to process. I'm going to assume that that range is A10:E10 (adjust accordingly...
Code:
Sub test()
    Dim r As Range, ary(4), i As Integer
    
    ary(0) = [B1]
    ary(1) = [F17] + [G17]
    ary(2) = [K1]
    ary(3) = [K2]
    ary(4) = [K3]
    
    i = 0
    For Each r In Range("A10:E10")
        r.Value = ary(i)
        i = i + 1
    Next
    
    i = 0
    For Each r In Range("D1:D5")
        r.Value = ary(i)
        i = i + 1
    Next

End Sub

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top