Consulting

Results 1 to 7 of 7

Thread: Function to add/write cells

  1. #1
    VBAX Regular
    Joined
    Jun 2007
    Posts
    58
    Location

    Function to add/write cells

    Ok I have three columns A, B, C. A1, B1, C1 are column headings.

    1st Problem:
    I need to perform division between A and B and put the result in C

    2nd Problem;
    I need to write to A and B.

    Can you use variables with Range?
    [VBA]Sheets("Sheet2").Range("B3").CopyFromRecordset RS[/VBA]
    How can I make this automated without typing B4,B5,... and so on? How do I throw a variable in there so I can use a loop of some sort?

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Range("C1").Value = Range("A1").Value / Range("B1").Value
    
    'and
    
    For i = 1 To 5
    Worksheets("Sheet2").Range("B" & i).CopyFromRecordSet RS
    Next i
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Regular
    Joined
    Jun 2007
    Posts
    58
    Location
    Thanks that worked. I'm new to this vba stuff.

  5. #5
    VBAX Regular
    Joined
    Jun 2007
    Posts
    58
    Location
    Ok a problem arose. Say I delete a2 and b2, a3 and b3 then move up to a2 and b2. How can I stop this?

  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    these are the same...second is relative:
    'Range("C1").Value = Range("A1").Value / Range("B1").Value
    Cells(1, 3).Value = Cells(1, 1).Value / Cells(1, 2).Value
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    For i = 1 To 5 
        Worksheets("Sheet2").Range("B" & i).CopyFromRecordSet RS 
    Next i
    In trying this, the whole of the recordset is copied to the range starting at B1. The looping is not repeating the "paste". Similarly, the following only copies for the first line
       Worksheets("Sheet1").Range("A1").CopyFromRecordset Rs
       Worksheets("Sheet1").Range("A2").CopyFromRecordset Rs
       Worksheets("Sheet1").Range("A3").CopyFromRecordset Rs
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •