Private Sub CommandButton1_Click()
Dim ctrl As control
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
'Pass this CheckBox to the subroutine below:
TransferValues ctrl
End If
Next
End Sub
Sub TransferValues(cb As MSForms.CheckBox)
Dim ws As Worksheet
Dim emptyRow As Long
Dim ws1 As Worksheet
If cb Then
'Define the worksheet based on the CheckBox.Name property:
Set ws = Sheets(Left(cb.Name, 15))
emptyRow = WorksheetFunction.CountA(ws.range("A:A")) + 1
With ws
.Cells(emptyRow, 1).Value = surname.Value
.Cells(emptyRow, 2).Value = firstname.Value
.Cells(emptyRow, 3).Value = tod.Value
.Cells(emptyRow, 4).Value = program.Value
.Cells(emptyRow, 5).Value = email.Value
.Cells(emptyRow, 6).Value = officenumber.Value
.Cells(emptyRow, 7).Value = cellnumber.Value
End With
End If
'the master sheet needs to have a "Stakeholder" column with list of stakeholder the person belongs to
End Sub
Depending on which checkboxes are chekced, I want to compile the values of checkboxes into one cell in Master Tab and Master Tab only. Above code transfers the values of each textbox according to which stakeholder the person belongs to (and this is done through checkboxes)
For example, a person named John Doe belongs to 6/8 checkboxes, above code transfers all information to 6/8 checkboxed sheets. but i always want the information to be filled in the master tab with an additional column name stakeholder that'll transfer the names of the checked boxes. when i tried it, it made a seperate row for every checkboxes instead of compiling it into one cell. so i made 6 John Doe's with same information except each John Doe's had different stakeholder he belonged to.
We will do this using VBA and the below procedure illustrates how this was done.
Sub copyPasteData() Dim strSourceSheet As String Dim strDestinationSheet As String Dim lastRow As Long
strSourceSheet = "Data entry"
Sheets(strSourceSheet).Visible = True Sheets(strSourceSheet).Select
Range("C2").Select Do While ActiveCell.Value <> "" strDestinationSheet
= ActiveCell.Value ActiveCell.Offset(0, -2).Resize(1, ActiveCell.CurrentRegion.Columns.Count).Select Selection.Copy Sheets(strDestinationSheet).Visible = True Sheets(strDestinationSheet).Select lastRow = LastRowInOneColumn("A") Cells(lastRow + 1, 1).Select Selection.PasteSpecial xlPasteValues Application.CutCopyMode = False Sheets(strSourceSheet).Select ActiveCell.Offset(0, 2).Select ActiveCell.Offset(1, 0).Select Loop End Sub
Related
I have been searching for a solution for hours, Can anyone help me?
I have a cmd button to open a user form and create a new entry in a 'master enquiry log' worksheet. I would like the cmd button to autofill the next number to the master log in column 'A' and then return that number to the user form.
I think there may need to be a user form initialization sub added but im not sure what to enter.
Any help will be much appreciated
Private Sub cmdAdd_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Master Enq. Log")
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'copy the data to the database
'use protect and unprotect lines,
' with your password
' if worksheet is protected
With ws
' .Unprotect Password:="password"
.Cells(iRow, 2).Value = Me.txtCustName.Value
.Cells(iRow, 3).Value = Me.txtCustAddr.Value
.Cells(iRow, 4).Value = Date
.Cells(iRow, 5).Value = Me.cboProjEng.Value
.Cells(iRow, 6).Value = Me.cboDrawer.Value
.Cells(iRow, 11).Value = Me.cboSalesPers.Value
' .Protect Password:="password"
End With
'clear the data
Me.txtCustName.Value = ""
Me.txtCustAddr.Value = ""
Me.cboProjEng.Value = "Select"
Me.cboDrawer.Value = "Select"
Me.cboSalesPers.Value = "Select"
Me.txtCustName.SetFocus
End Sub
Can someone help me get my userform to submit into this table from cal worksheet?
Private Sub cmdbutton_submitform_Click()
Dim emptyRow As Long
'Make Sheet2 active
Sheet2.Activate
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
'Transfer information
Cells(emptyRow, 1).Value = txtbox_number.Value
Cells(emptyRow, 2).Value = txtbox_rank.Value
Cells(emptyRow, 3).Value = txtbox_Name.Value
Cells(emptyRow, 4).Value = txtbox_height.Value
Cells(emptyRow, 5).Value = txtbox_weight.Value
Cells(emptyRow, 6).Value = txtbox_right_rm.Value
Cells(emptyRow, 7).Value = txtbox_left_rm.Value
End Sub
I think you are getting confused with sheet codenames and sheet names (see this). Try
Private Sub cmdbutton_submitform_Click()
Dim emptyRow As Long
With Worksheets("Sheet2")
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(.Range("A:A")) + 1
'Transfer information
.Cells(emptyRow, 1).Value = txtbox_number.Value
.Cells(emptyRow, 2).Value = txtbox_rank.Value
.Cells(emptyRow, 3).Value = txtbox_Name.Value
.Cells(emptyRow, 4).Value = txtbox_height.Value
.Cells(emptyRow, 5).Value = txtbox_weight.Value
.Cells(emptyRow, 6).Value = txtbox_right_rm.Value
.Cells(emptyRow, 7).Value = txtbox_left_rm.Value
End With
End Sub
Using Worksheet.Activate method likely loses the parent form reference that is required to correctly get the text box data from the user form. Within this Private Sub you should be able to reference Sheet2 by its Worksheet .CodeName property and use Me to reference the user form as the parent of the text boxes.
Private Sub cmdbutton_submitform_Click()
Dim emptyRow As Long
'Reference Sheet2 by CodeName as the parent worksheet of the .Cells
With Sheet2
'Determine emptyRow
emptyRow = .Cells(Rows.Count, 1).End(xlUp).Row + 1
'Transfer information
.Cells(emptyRow, 1).Value = Me.txtbox_number.Value
.Cells(emptyRow, 2).Value = Me.txtbox_rank.Value
.Cells(emptyRow, 3).Value = Me.txtbox_Name.Value
.Cells(emptyRow, 4).Value = Me.txtbox_height.Value
.Cells(emptyRow, 5).Value = Me.txtbox_weight.Value
.Cells(emptyRow, 6).Value = Me.txtbox_right_rm.Value
.Cells(emptyRow, 7).Value = Me.txtbox_left_rm.Value
End With
End Sub
I found it a little odd that you were identifying the worksheet with a Worksheet .CodeName property rather than a Worksheet .Name property. I've included a couple of links to make sure you are using the naming conventions correctly. In any event, I've use a With ... End With statement to avoid repeatedly reidentifying the parent worksheet.
I am relatively new to the whole VBA so any help would be greatly appreciated...
I am having issues with getting my Combobox on my form (once you hit the submit button) to input the information to my excel spreadsheet in a specific column which would move down a row each time a new record is entered.
Please see my code below and If you need any further information please ask away :) Many Thanks in advance Paula
Option Explicit
Private Sub cmdAdd_Click()
Dim irow As Long
Dim EorP As String
Dim ComboStaus As ComboBox
Dim ws As Worksheet
Set ws = Worksheets("BS Personal Data")
'find first empty row in spreadsheet
irow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
EorP = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
Combo = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'copy the data to the spreadsheet
With ws
.Cells(irow, 1).Value = Me.txtmanager.Value
.Cells(irow, 2).Value = Me.txtdivision.Value
.Cells(irow, 3).Value = Me.txtlocation.Value
.Cells(irow, 4).Value = Me.txtsystemname.Value
.Cells(irow, 9).Value = Me.Txtpurpose.Value
.Cells(irow, 10).Value = Me.txtaccess.Value
.Cells(irow, 11).Value = Me.txtdatecompleted.Value
End With
'clear the data
Me.txtmanager.Value = ""
Me.txtdivision.Value = ""
Me.txtlocation.Value = ""
Me.txtsystemname.Value = ""
Me.Txtpurpose.Value = ""
Me.txtaccess.Value = ""
Me.txtdatecompleted.Value = ""
'set option button to input data based on type of information to column 5
With ws
If OptElectronic Then
.Cells(EorP, 5).Value = "Electronic"
Else
.Cells(EorP, 5).Value = "PaperBased"
End If
End With
'set option button to input data based on personal data into column 7
With ws
If PersonalYes Then
.Cells(EorP, 7).Value = "Yes"
Else
.Cells(EorP, 7).Value = "No"
End If
End With
'set option button to input data based on privacy notes into column 8
With ws
If PrivacyYes Then
.Cells(EorP, 8).Value = "Yes"
Else
.Cells(EorP, 8).Value = "No"
End If
End With
End Sub
'combo button setup
Private Sub UserForm_Activate()
ComboStatus.Clear
With ComboStatus 'this loads the combo
.AddItem ""
.AddItem "Live"
.AddItem "Archived"
.AddItem "zzz"
End With
End Sub
'close button on the form
Private Sub cmdClose_Click()
Unload Me
End Sub
I wrote a VBA Macro that tries to take values input from text boxes in a user form and copies them into cells on a specific worksheet. I also wrote a countA function that allows me to write to a new row every time I hit the input button. For a reason that I cannot understand, it will only write to the active worksheet no matter what sheet I reference. Please Help!
Private Sub inputlight_Click()
Dim emptyrow As Long
'Find the first empty row after row 47 on sheet "T5 Input Sheet"
emptyrow = 47 + WorksheetFunction.CountA(Sheets("T5 Input Sheet").Range("b48:b219")) + 1
'transfer data
Cells(emptyrow, 2).Value = esize.Value
Cells(emptyrow, 3).Value = etype.Value
Cells(emptyrow, 4).Value = ewatt.Value
Cells(emptyrow, 5).Value = elamps.Value
Cells(emptyrow, 6).Value = eusage.Value
Cells(emptyrow, 7).Value = efixtures.Value
End Sub
I've tried changing it to CountA(Worksheets("T5 Input Sheet"), (Sheets(2)), (Sheets("Sheet2") etc. but none of them print to anything but the active cell. What am I doing wrong?
You take the correct approach using the CountA function however you also need to qualify your cells in order to stick values to a specific sheet.
Private Sub inputlight_Click()
Dim emptyrow As Long
'Find the first empty row after row 47 on sheet "T5 Input Sheet"
emptyrow = 47 + WorksheetFunction.CountA(Sheets("T5 Input Sheet").Range("b48:b219")) + 1
'transfer data
With Sheets("SHEET_NAME")
.Cells(emptyrow, 2).Value = esize.Value
.Cells(emptyrow, 3).Value = etype.Value
.Cells(emptyrow, 4).Value = ewatt.Value
.Cells(emptyrow, 5).Value = elamps.Value
.Cells(emptyrow, 6).Value = eusage.Value
.Cells(emptyrow, 7).Value = efixtures.Value
End With
End Sub
So where it says SHEET_NAME that's where you stick the destination Sheet name.
Private Sub CommandButton1_Click()
Dim ctrl As control
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
'Pass this CheckBox to the subroutine below:
TransferValues ctrl
End If
Next
End Sub
Sub TransferValues(cb As MSForms.CheckBox)
Dim ws As Worksheet
Dim emptyRow As Long
Dim ws1 As Worksheet
If cb Then
'Define the worksheet based on the CheckBox.Name property:
Set ws = Sheets(Left(cb.Name, 15))
emptyRow = WorksheetFunction.CountA(ws.range("A:A")) + 1
With ws
.Cells(emptyRow, 1).Value = surname.Value
.Cells(emptyRow, 2).Value = firstname.Value
.Cells(emptyRow, 3).Value = tod.Value
.Cells(emptyRow, 4).Value = program.Value
.Cells(emptyRow, 5).Value = email.Value
.Cells(emptyRow, 6).Value = officenumber.Value
.Cells(emptyRow, 7).Value = cellnumber.Value
End With
Set ws1 = Sheets("Master")
emptyRow = WorksheetFunction.CountA(range("A:A")) + 1
With ws1
.Cells(emptyRow, 1).Value = surname.Value
.Cells(emptyRow, 2).Value = firstname.Value
.Cells(emptyRow, 3).Value = tod.Value
.Cells(emptyRow, 4).Value = program.Value
.Cells(emptyRow, 5).Value = email.Value
.Cells(emptyRow, 6).Value = officenumber.Value
.Cells(emptyRow, 7).Value = cellnumber.Value
.Cells(emptyRow, 8).Value = cb.Name
End With
End If
'the master sheet needs to have a "Stakeholder" column with list of stakeholder the person belongs to
The problem here is Cb.Name - I want the name checkboxes to appear in one single cell but right now it's making extra rows depending on the number of checked boxes. So instead of putting 6/8 checked boxes names into 1 single cell it makes 6 rows with each names which is not good. How do i transfer all cb.names into one single cell?
sorry if the code doesn't look properly formatted - for some reason it's not showing all the indents...
If I read you right your transfer function deals with individual checks but the master sheet needs to have a single line for the whole lot, right?
If so what you will need to do is work on the individual and collective levels separately. Delete all references to the master sheet from your basic sub and deal with the master sheet on it's own
Sub TransferMasterValue()
Dim allChecks As String
'Iterate through the checkboxes concatenating a string of all names
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
If ctrl Then
allChecks = allChecks & ctrl.Name & ","
End If
End If
Next
'If you have at least one transfer to the Master sheet
If Len(allChecks) > 0 Then
'Your code to transfer
Set ws1 = Sheets("Master")
emptyRow = WorksheetFunction.CountA(range("A:A")) + 1
With ws1
.Cells(emptyRow, 1).Value = surname.Value
...
'and post the concatenated value in the name position
.Cells(emptyRow, 8).Value = left(allChecks,len(allChecks)-1)
End With
End If
End Sub
The main button click function will then look like...
Private Sub CommandButton1_Click()
Dim ctrl As control
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
'Pass this CheckBox to the subroutine below:
TransferValues ctrl
End If
Next
TransferMasterValue
End Sub
Have you tried concatenate? If I have cells |1|2|3|4|5| I can just insert the function =CONCATENATE(A1,",",B1) and you can change to "," to " " for a space, or have no separation at all. If this isn't what you're looking for then I think I misinterpted the question.