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.
Related
I am trying to copy several data from a table on a different sheet, to a new table on another sheet. My structure is like:
These are the steps that me and Mr. #QHarr have tried:
Checked the objects and values exist
Tried running the codes line by line
Activate sheets and re-arranging the codes
None worked so far:
Here is my current codes:
Private Sub cmdedit_Click()
If MsgBox("Transfer selected asset to " & Me.ComboBox1.Text & "?", vbYesNo, "CONFIRMATION") = vbYes Then
Dim ws As Worksheet
Dim ws1 As Worksheet
Dim wsendRow As Range
Dim wsendRow1 As Range
Dim lo As ListObject
Dim lr As ListRow
Set ws = Sheets("FIELD OFFICE DATABASE")
Set ws1 = Sheets("Transferred Items")
Set lo = ws1.ListObjects("table3")
Set lr = lo.ListRows.Add
Set wsendRow = ws.Range("B" & Rows.Count).End(xlUp)
'Set wsendRow1 = ws1.Range("A" & Rows.Count).End(xlUp)
ws.Activate
Range("B2").Select
Do Until ActiveCell.Address = wsendRow.Address
If ActiveCell.Value = Me.cmbemn.Text Then
'ws1.Unprotect "321321"
'ws1.Activate
lr.Range(1, 1).Value = Me.cmbemn.Text 'error appears on this line. if I place a comment here, the error will just move on the next line.
lr.Range(1, 2).Value = Me.TextBox1.Text
lr.Range(1, 3).Value = Me.txttype.Text
lr.Range(1, 4).Value = Me.txtmodel.Text
lr.Range(1, 5).Value = ActiveCell.Offset(0, 4).Value
lr.Range(1, 6).Value = ActiveCell.Offset(0, 5).Value
lr.Range(1, 7).Value = Me.txtpurdate.Text
lr.Range(1, 8).Value = Me.txtprice.Text
lr.Range(1, 9).Value = Me.txtcon.Text
lr.Range(1, 10).Value = ActiveCell.Offset(0, 9).Value
lr.Range(1, 11).Value = ActiveCell.Offset(0, 11).Value
lr.Range(1, 12).Value = Me.ComboBox1.Text
lr.Range(1, 13).Value = ActiveCell.Offset(0, 13).Value
lr.Range(1, 14).Value = Date
lr.Range(1, 15).Value = ws.Range("A13").Value
lr.Range(1, 16).Value = Me.TextBox2.Text
Exit Do
Exit Sub
Else
ActiveCell.Offset(1, 0).Select
End If
Loop
End If
End Sub
Codes never worked. Also to note, this question is in reference to my other question, (which took a lot of comments from me and Mr. QHarr) until he suggested that I should ask another question instead.
I hope someone can help me figure this out.
Thank you so much in advance
Firstly, let me apologise if this question has already been answered somewhere else. I had a good look but couldn't find anything that would help me.
Secondly, I'm sure there is a far more simple way to do this but I'm very new the VBA and I'm just trying to teach myself as I go along.
Ok, so I have a sheet at the end of my workbook that compiles information from the previous sheet and I want to copy those values that are all in row 2 to another workbook that we have a network drive.
I've managed to get this to work on the same sheet but not to another workbook (without using a userform).
It comes back with the error 'Invalid Qualifier' for the line
Cells(emptyRow, 1.Value - DateRaised.Value
Here is my code below,
Sub CommandButton1_Click()
Dim emptyRow As Long
Dim DateRaised As Long
Dim CustomerName As String
Dim SiteAddress As String
Dim CallReason As String
Dim CustomerOrderNo As Long
Dim InvoiceNo As Long
Dim CovernoteNo As Long
Dim Findings As String
Dim ProductType As String
Dim Supplier As String
Dim Attempts As Long
Dim Condition As String
Dim DateClosed As Long
Dim CreditGiven As String
Dim CreditValue As Long
Dim IssueDays As Long
Dim Comments As String
DateRaised = Cells(2, "A").Value
CustomerName = Cells(2, "B").Value
SiteAddress = Cells(2, "C").Value
CallReason = Cells(2, "D").Value
CustomerOrderNo = Cells(2, "F").Value
InvoiceNo = Cells(2, "G").Value
CovernoteNo = Cells(2, "H").Value
Findings = Cells(2, "I").Value
ProductType = Cells(2, "J").Value
Supplier = Cells(2, "K").Value
Attempts = Cells(2, "L").Value
Condition = Cells(2, "M").Value
DateClosed = Cells(2, "N").Value
CreditGiven = Cells(2, "O").Value
CreditValue = Cells(2, "P").Value
IssueDays = Cells(2, "Q").Value
Comments = Cells(2, "R").Value
Dim WrkBk As Workbook
Dim WrkSht As Worksheet
Set WrkBk = Workbooks.Open("R:\6024 Onsite\COVER NOTE WORKFLOW\Database\Covernote Databse.xlsx")
Set WrkSht = WrkBk.Sheets("Covernote Database")
WrkSht.Activate
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
Cells(emptyRow, 1).Value = DateRaised.Value
Cells(emptyRow, 2).Value = CustomerName.Value
Cells(emptyRow, 3).Value = SiteAddress.Value
Cells(emptyRow, 4).Value = CallReason.Value
Cells(emptyRow, 5).Value = CustomerOrderNo.Value
Cells(emptyRow, 6).Value = InvoiceNo.Value
Cells(emptyRow, 7).Value = CovernoteNo.Value
Cells(emptyRow, 8).Value = Findings.Value
Cells(emptyRow, 9).Value = ProductType.Value
Cells(emptyRow, 10).Value = Supplier.Value
Cells(emptyRow, 11).Value = Attemps.Value
Cells(emptyRow, 12).Value = Condition.Value
Cells(emptyRow, 13).Value = DateClosed.Value
Cells(emptyRow, 14).Value = CreditGiven.Value
Cells(emptyRow, 15).Value = CreditValue.Value
Cells(emptyRow, 16).Value = IssueDays.Value
Cells(emptyRow, 17).Value = Comments.Value
WrkBk.Close (SaveChanges = False)
End Sub
If anyone can point me in the right direction I'd be a very happy man.
it's because you're attempting to treat value types (like String and Long) variables as if they were reference type (objects) ones calling their Value property:
Cells(emptyRow, 1).Value = DateRaised.Value
while you can't (unless you use User Defined Types): value type variables can be only accessed as they are:
Cells(emptyRow, 1).Value = DateRaised
but you can simply code like follows:
Option Explicit
Sub CommandButton1_Click()
Dim emptyRow As Long
Dim curSht As Worksheet
Set curSht = ActiveSheet
With Workbooks.Open("R:\6024 Onsite\COVER NOTE WORKFLOW\Database\Covernote Databse.xlsx").Sheets("Covernote Database")
emptyRow = WorksheetFunction.CountA(.Range("A:A")) + 1
.Cells(emptyRow, 1).Resize(, 17).value = curSht.Cells(2, 1).Resize(, 17).value '<-- paste values from originally opened sheet range A2:Q2
End With
ActiveWorkbook.Close SaveChanges:=False
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.
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