PDA

View Full Version : How to write a vba code



reddog2002
07-04-2013, 01:58 PM
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.

JKwan
07-04-2013, 02:40 PM
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

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

reddog2002
07-08-2013, 12:34 PM
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

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


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

JamesAl
07-09-2013, 02:01 AM
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.

SamT
07-09-2013, 05:24 AM
The code goes in the "ThisWorkbook" code page.

Put the cursor in the word "Me" and press F1 for help on "Me."

snb
07-09-2013, 06:21 AM
or


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

reddog2002
07-09-2013, 08:14 PM
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 A1:D1 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

reddog2002
07-09-2013, 08:31 PM
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

snb
07-10-2013, 02:08 AM
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.

reddog2002
08-03-2013, 04:33 PM
or


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



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