Consulting

Results 1 to 10 of 10

Thread: Solved: VBA: Values on Sheet

  1. #1

    Solved: VBA: Values on Sheet

    Hi All,

    To introduce myself, my name is Sam and I am from the UK. Currently progressing with a foundation degree whilst working for British Telecom.

    Was wondering if anybody can help.

    The code as follows does the obvious which is set some variables, write some values to the spreadsheet and bold the headings.

    I need to:

    - Make sure each time the code is run that the inputted values are written to the next row down to the previous so that it doesnt overwrite the last line. (EDIT: just looked at the code and it is obvious that each time it will always write to the same cell. Can somebody suggest another way to do this so that the above is possible)

    Also, im pretty sure the code can be cut down. I would greatly appreciate any help on this.

    The code is as follows:

    [vba]
    Option Explicit
    Sub Cylinder()
    Const PI = 3.14
    Dim UserRadius As Double
    Dim UserHeight As Double
    Dim Volume As Double
    UserRadius = InputBox("Enter Radius", "Cylinder Radius")
    UserHeight = InputBox("Enter Height", "Cylinder Height")
    Volume = PI * (UserRadius * UserRadius) * UserHeight
    MsgBox "The Volume of the Cylinder = " & Format(Volume, "0.0")

    Worksheets("Sheet1").Select
    Range("A1").Value = "Radius"
    Range("B1").Value = "Height"
    Range("C1").Value = "Volume(Cubed)"
    Range("A1:C1").Select
    Selection.Font.Bold = True

    Range("A2").Value = UserRadius
    Range("B2").Value = UserHeight
    Range("C2").Value = Volume
    ActiveCell.Offset(1, 0).Select


    End Sub
    [/vba]

    Thankyou in advance,
    Sam
    Last edited by sammclean23; 09-17-2011 at 09:17 AM.

  2. #2
    Okay iv tried this but still doesnt get the required effect:
    [VBA]UserRadius = InputBox("Enter Radius", "Cylinder Radius")
    UserHeight = InputBox("Enter Height", "Cylinder Height")
    Volume = PI * (UserRadius * UserRadius) * UserHeight
    MsgBox "The Volume of the Cylinder = " & Format(Volume, "0.0")

    Worksheets("Sheet1").Select
    Range("A1").Value = "Radius"
    Range("B1").Value = "Height"
    Range("C1").Value = "Volume(Cubed)"
    Range("A1:C1").Select
    Selection.Font.Bold = True

    ActiveCell.Value = Range("A2")
    ActiveCell.Offset(0, 1).Value = UserRadius
    ActiveCell.Offset(0, 2).Value = UserHeight
    ActiveCell.Offset(0, 3).Value = Volume
    ActiveCell.Offset(1, 0).Activate[/VBA]

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Hello Sam in the UK.

    You already have headings, so

    [vba]

    Sub Cylinder()
    Const PI = 3.14
    Dim UserRadius As Double
    Dim UserHeight As Double
    Dim Volume As Double
    Dim Lastrow As Long

    UserRadius = InputBox("Enter Radius", "Cylinder Radius")
    UserHeight = InputBox("Enter Height", "Cylinder Height")
    Volume = PI * (UserRadius * UserRadius) * UserHeight
    MsgBox "The Volume of the Cylinder = " & Format(Volume, "0.0")

    With Worksheets("Sheet1")

    Lastrow = .Cells(.Rows.Count, "a").End(xlUp).Row

    .Cells(Lastrow + 1, "A").Value = UserRadius
    .Cells(Lastrow + 1, "B").Value = UserHeight
    .Cells(Lastrow + 1, "C").Value = Volume
    End With
    End Sub[/vba]
    ____________________________________________
    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
    Brilliant thanks a lot!

    Works exactly as required.

    One thing though, because I wish to learn how this works, can you quickly just explain how the following bit of code works:

    [VBA]Lastrow = .Cells(.Rows.Count, "a").End(xlUp).Row
    [/VBA]

    Thanks again,
    Sam

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What that does is to start from the last row in the spreadsheet in column A (Rows.Count, "A") and does the equivalent of Ctrl-UpArrow, which is the End(xlUp) part. This finds the last occupied cell in that column, and we extract the row number.
    ____________________________________________
    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

  6. #6
    Quote Originally Posted by xld
    What that does is to start from the last row in the spreadsheet in column A (Rows.Count, "A") and does the equivalent of Ctrl-UpArrow, which is the End(xlUp) part. This finds the last occupied cell in that column, and we extract the row number.
    Okay thank you!

    Sorry but can i also ask, if i were to do this using offset and active cell, how could I achieve the same result?

    I know the above does work, but just want to see the same result using offsets too.

    Thanks again in advance, I appreciate the help as can be frustrating at times.
    Sam

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That is a VERY bad idea. Every time you reset the activecell, Excel will repaint the screen, which takes a lot of resources and time.
    ____________________________________________
    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

  8. #8
    Quote Originally Posted by xld
    That is a VERY bad idea. Every time you reset the activecell, Excel will repaint the screen, which takes a lot of resources and time.
    I understand it is yes

    However, just for understanding purposes, would you be able to explain how to achieve the same result using offset and activecell.

    I know by all means that this is not the most effective, im just trying to get a grasp for offest

    Thanks!

    Sam

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Not tested

    [vba]

    Sub Cylinder()
    Const PI = 3.14
    Dim UserRadius As Double
    Dim UserHeight As Double
    Dim Volume As Double
    Dim i As Long

    UserRadius = InputBox("Enter Radius", "Cylinder Radius")
    UserHeight = InputBox("Enter Height", "Cylinder Height")
    Volume = PI * (UserRadius * UserRadius) * UserHeight
    MsgBox "The Volume of the Cylinder = " & Format(Volume, "0.0")

    With Worksheets("Sheet1")

    With Activecell.Offset(0, -.Column + 1)

    Do While .Offset(i, 0).Value <> ""

    .Offset(i, 0).Value = UserRadius
    .Offset(i, 1).Value = UserHeight
    .Offset(i, 2).Value = Volume

    i = i + 1
    Loop
    End With
    End With
    End Sub [/vba]
    ____________________________________________
    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

  10. #10
    Brilliant thank you. Thanks a lot for your help

Posting Permissions

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