Consulting

Results 1 to 10 of 10

Thread: How to write a vba code

  1. #1

    How to write a vba code

    I have a value in A3 and B3. I want to put it on sheet 2 when I check or use a radio button in C3 sheet 1. If it is not checked remove it from sheet 2.
    Thanks Reddog.

  2. #2
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    I do not recommend using a radio button, I would use a checkbox

    Create a checkbox and add the following code to your checkbox control
    [VBA]
    Private Sub CheckBox1_Click()
    Dim WS1 As Worksheet
    Dim WS2 As Worksheet

    Set WS1 = Worksheets("Sheet1")
    Set WS2 = Worksheets("Sheet2")

    Select Case Me.CheckBox1.Value
    Case True
    WS2.Range("A3:B3").Value = WS1.Range("A3:B3").Value
    WS1.Range("A3:B3") = ""

    Case Else
    WS2.Range("A3:B3") = ""
    End Select
    End Sub
    [/VBA]

  3. #3
    Quote Originally Posted by JKwan
    I do not recommend using a radio button, I would use a checkbox

    Create a checkbox and add the following code to your checkbox control
    [VBA]
    Private Sub CheckBox1_Click()
    Dim WS1 As Worksheet
    Dim WS2 As Worksheet

    Set WS1 = Worksheets("Sheet1")
    Set WS2 = Worksheets("Sheet2")

    Select Case Me.CheckBox1.Value
    Case True
    WS2.Range("A3:B3").Value = WS1.Range("A3:B3").Value
    WS1.Range("A3:B3") = ""

    Case Else
    WS2.Range("A3:B3") = ""
    End Select
    End Sub
    [/VBA]
    I haven't had much time play with this. I'm problems with the ME part of the code. I will try again tonight and try to put the workbook on to.
    Thanks reddog

  4. #4
    VBAX Regular
    Joined
    Jul 2013
    Posts
    6
    Location
    Is your problem solved? If not please add some more details because i have not understood it clearly. But if its solved then that's fine.

  5. #5
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    The code goes in the "ThisWorkbook" code page.

    Put the cursor in the word "Me" and press F1 for help on "Me."
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    or


    [vba]Private Sub CheckBox1_Click()

    With Sheets("Sheet2").Range("A3:B3")
    .Clearcontents
    if CheckBox1.Value Then .Value = Sheets("Sheet1").Range("A3:B3").Value
    End With
    End Sub[/vba]

  7. #7
    I'm still having troubles. I really don't know what I'm doing. Sheet 1 has the field numbers and the gallons per minute in A3 B3 down to A47 B47. In column C I want a checkbox for each field. If the checkbox is checked I want to add it to sheet2. If it's unchecked remove it from sheet2. Column D is what crop that field has. The back ground color I would like that to go to sheet2 also from A3 to Q3. I don't know what I did in A11 where there is a drop down list but I don't want it. I will add my workbook again so you all can look at it. I'll try to post a picture of what sheet 2 will look like also. What's common sense to all of you isn't to me, so try make it so my 3 year old grand daughter can understand then I might have a chance.
    Thanks Stacey


    Looks like I will make another post of sheet2
    Attached Files Attached Files

  8. #8
    Here is what sheet2 will look like. I colored just some of the rows for an example. A1:A47 is all I want to change. The rest would stay the same.
    Hope this make sense.
    Thanks Stacey
    Attached Files Attached Files

  9. #9
    Before turning to your granddaughter try to stumble across 'VBA for dummies' or an equivalent book and swallow that.
    After that we are much better able to help you.

  10. #10
    Quote Originally Posted by snb View Post
    or


    [vba]Private Sub CheckBox1_Click()

    With Sheets("Sheet2").Range("A3:B3")
    .Clearcontents
    if CheckBox1.Value Then .Value = Sheets("Sheet1").Range("A3:B3").Value
    End With
    End Sub[/vba]
    I found out what I was doing wrong. I needed to assign this to a user form and it worked. I will need more help when time permits.
    Thanks Stacey

Posting Permissions

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