VBA: How to start 'emptyrow' from specific Column and Cell? - excel

Basically I am making a Userform and would like that the data start on the next emptyrow in Column B starting from Cell B4.
Here is the code I got from a userform template found online:
Private Sub OKButton_Click()
Dim emptyRow As Long
'Make Sheet1 active
Sheet1.Activate
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 2
'Transfer information
Cells(emptyRow, 1).Value = NameTextBox.Value
Cells(emptyRow, 2).Value = PositionTextBox.Value
Cells(emptyRow, 3).Value = EmployeeIDTextBox.Value
Thanks for the assistance.

Private Sub OKButton_Click()
' Declare/Set variable for referencing workbook
Dim wb As Workbook
Set wb = ThisWorkbook
' Declare/Set variable for referencing worksheet
Dim ws As Worksheet
Set ws = wb.Worksheets("Sheet1")
'Determine next empty Row
Dim emptyRow As Long
' Code below works like this:
' ws = the worksheet
' .Range("b65536") is the last cell in column b
' .End(xlUp) means go up from cell b65536 until we hit a non-empty cell
' .Row is the row number of that non-empty cell
' + 1 to get to the empty row below
emptyRow = ws.Range("b65536").End(xlUp).Row + 1
' Create/Set a variable for referencing the range we want to use
' The range will be set to start at column b of
' the empty row and go to column AF of the empty row
Dim rng As Range
Set rng = ws.Range("b" & emptyRow & ":AF" & emptyRow)
'Transfer information
' If we didn't use "With rng", below, you'd have to write each of these lines like:
' rng.Cells(...
' Since the range is only 1 row, you can replace the .Cells(emptyrow, 1), etc.
' like you had and just do .Cells(1,1), etc.
With rng
.Cells(1, 1).Value = NameTextBox.Value
.Cells(1, 2).Value = PositionTextBox.Value
.Cells(1, 3).Value = EmployeeIDTextBox.Value
.Cells(1, 4).Value = GenderComboBox.Value
.Cells(1, 5).Value = NationalityTextBox.Value
.Cells(1, 6).Value = DOBTextBox.Value
.Cells(1, 7).Value = PassportTextBox.Value
.Cells(1, 8).Value = PassportExpTextBox.Value
.Cells(1, 9).Value = MedicalTextBox.Value
.Cells(1, 10).Value = YFTextBox.Value
.Cells(1, 11).Value = Lic1TextBox.Value
.Cells(1, 12).Value = Lic1FlagTextBox.Value
.Cells(1, 13).Value = Lic1ExpTextBox.Value
.Cells(1, 14).Value = Lic2TextBox.Value
.Cells(1, 15).Value = Lic2FlagTextBox.Value
.Cells(1, 16).Value = Lic2ExpTextBox.Value
.Cells(1, 17).Value = DPComboBox.Value
.Cells(1, 18).Value = DPCertTextBox.Value
.Cells(1, 19).Value = DPCertExpTextBox.Value
.Cells(1, 20).Value = GMDSSTextBox.Value
.Cells(1, 21).Value = GMDSSCertTextBox.Value
.Cells(1, 22).Value = GMDSSExpTextBox.Value
If RadarCheckBox.Value = True Then .Cells(1, 23).Value = "Yes"
If ArpaCheckBox.Value = True Then .Cells(1, 24).Value = "Yes"
If EcdisCheckBox.Value = True Then .Cells(1, 25).Value = "Yes"
If BosietCheckBox.Value = True Then .Cells(1, 26).Value = "Yes"
If HuetCheckBox.Value = True Then .Cells(1, 27).Value = "Yes"
If HloCheckBox.Value = True Then .Cells(1, 28).Value = "Yes"
If OrbCheckBox.Value = True Then .Cells(1, 29).Value = "Yes"
If EACheckBox.Value = True Then .Cells(1, 30).Value = "Yes"
If VsoOptionButton1.Value = True Then
.Cells(1, 31).Value = "Yes"
Else
.Cells(1, 31).Value = "No"
End If
End With
End Sub

Related

Why ActiveX Command button does not run all codes in my VBA UserForm?

I am a complete novice at Excel VBA and I am currently attempting a project on Excel VBA. I have created a UserForm that would allow the user to enter data onto the Excel Sheet by completing the fields in the UserForm. I have tested all the codes individually and they have worked fine.
For the user to access the UserForm, I have added an ActiveX Command Button on a separate sheet on the same workbook. However, when accessing the UserForm from the ActiveX Command Button, some of the codes do not run (mainly the code that flags out the duplicate entry, as well as the code that generates serial numbers).
Where did I go wrong in my code?
This is my code to adding new data as well as the code to flag out duplicate entries. When opening the UserForm from the ActiveX Command Button, adding new data works fine but it does not flag out duplicate entries in the data. (However, testing the code itself in VBA works perfectly fine).
Private Sub cmdAddNewCustomer_Click()
Dim count As Long
Dim lastrow As Long
Dim lCustomerID As String
Dim ws As Worksheet
Set ws = Worksheets("Customer Data")
'find first empty row in database
lrow = ws.Cells.Find(what:="*", searchorder:=xlRows, _
Searchdirection:=xlPrevious, LookIn:=xlValues).Row + 1
lCustomerID = txtCustomerID
count = 0
With ws
For currentrow = 1 To lrow
If lCustomerID = Cells(currentrow, 1) Then
count = count + 1
End If
If count > 1 Then
.Cells(currentrow, 1).Value = ""
.Cells(currentrow, 2).Value = ""
.Cells(currentrow, 3).Value = ""
.Cells(currentrow, 4).Value = ""
.Cells(currentrow, 5).Value = ""
.Cells(currentrow, 6).Value = ""
.Cells(currentrow, 7).Value = ""
.Cells(currentrow, 8).Value = ""
.Cells(currentrow, 9).Value = ""
.Cells(currentrow, 10).Value = ""
.Cells(currentrow, 11).Value = ""
.Cells(currentrow, 12).Value = ""
.Cells(currentrow, 13).Value = ""
.Cells(currentrow, 14).Value = ""
MsgBox ("CustomerID already exists!")
End If
If count = 0 Then
.Cells(lrow, 1).Value = Me.txtCustomerID.Value
.Cells(lrow, 2).Value = Me.txtCustomerName.Value
.Cells(lrow, 3).Value = Me.cboCustomerStatus.Value
.Cells(lrow, 4).Value = Me.txtContactPerson.Value
.Cells(lrow, 5).Value = Me.cboDepartment.Value
.Cells(lrow, 6).Value = Me.txtPosition.Value
.Cells(lrow, 7).Value = Me.cboRoleType.Value
.Cells(lrow, 8).Value = Me.txtofficeHP1.Value
.Cells(lrow, 9).Value = Me.txtOfficeHP2.Value
.Cells(lrow, 10).Value = Me.txtMobileHP1.Value
.Cells(lrow, 11).Value = Me.txtMobileHP2.Value
.Cells(lrow, 12).Value = Me.txtEmail1.Value
.Cells(lrow, 13).Value = Me.txtEmail2.Value
.Cells(lrow, 14).Value = Me.txtEmail3.Value
End If
Next currentrow
End With
'clear the data
Me.txtCustomerName.Value = ""
Me.cboCustomerStatus.Value = ""
Me.txtContactPerson.Value = ""
Me.cboDepartment.Value = ""
Me.txtPosition.Value = ""
Me.cboRoleType.Value = ""
Me.txtofficeHP1.Value = ""
Me.txtOfficeHP2.Value = ""
Me.txtMobileHP1.Value = ""
Me.txtMobileHP2.Value = ""
Me.txtEmail1.Value = ""
Me.txtEmail2.Value = ""
Me.txtEmail3.Value = ""
End Sub
This is the code to generate serial numbers. (Same problem, does not work when accessed via ActiveX Command Button but works fine when tested individually in VBA).
Sub FindCustomerID()
Dim lastrow
Dim lastnum As Long
Dim ws As Worksheet
Set ws = Worksheets("Customer Data")
If Me.cboCountry = "" Or Me.txtCustomerName = "" Then
Exit Sub
End If
serialno = 1
lastrow = ws.Cells(Rows.count, 1).End(xlUp).Row
CountryCode = UCase(Left(Me.cboCountry, 3))
CustomerCode = UCase(Left(Me.txtCustomerName, 10))
'assemble them into CustomerID
CustomerID = CountryCode & CustomerCode & serialno
For currentrow = 2 To lastrow
If CustomerID = Cells(currentrow, 1) Then
'find last number that applies
serialno = serialno + 1
End If
're-assign customerID with new serial number
CustomerID = CountryCode & CustomerCode & serialno
Next currentrow
Me.lblCustomerID = CustomerID
End Sub
And lastly, this is the code from the ActiveX Command Button that brings out the UserForm.
Private Sub cmdNCustomerData_Click()
frmCustomerdata.Show
End Sub
The cause of the problem you described is a missing . to qualify Cells(currentrow, 1). Because you added the ActiveX button to a different sheet, the line
If lCustomerID = Cells(currentrow, 1) Then
accesses Cells(currentrow, 1) of that sheet. To fix this the range needs to be qualified with a . to become
If lCustomerID = .Cells(currentrow, 1) Then
I would also take
If count = 0 Then
.
.
.
End If
outside the loop. You are repeating these lines many times unnecessarily.
The first block of code then becomes:
Private Sub cmdAddNewCustomer_Click()
Dim count As Long
Dim lastrow As Long
Dim lCustomerID As String
Dim ws As Worksheet
Set ws = Worksheets("Customer Data")
'find first empty row in database
lrow = ws.Cells.Find(what:="*", searchorder:=xlRows, _
Searchdirection:=xlPrevious, LookIn:=xlValues).Row + 1
lCustomerID = txtCustomerID
count = 0
With ws
' Count backward to delete rows completely
For currentrow = lrow - 1 To 1 Step -1
If lCustomerID = .Cells(currentrow, 1) Then
count = count + 1
End If
If count > 1 Then
.Cells(currentrow, 1).Resize(1, 14).ClearContents
' Uncomment the following line to delete the whole row completely
'.Rows(currentrow).Delete
End If
Next currentrow
If count > 1 Then
MsgBox (count - 1 " duplicates of CustomerID found and cleared!")
ElseIf count = 0 Then
.Cells(lrow, 1).Value = Me.txtCustomerID.Value
.Cells(lrow, 2).Value = Me.txtCustomerName.Value
.Cells(lrow, 3).Value = Me.cboCustomerStatus.Value
.Cells(lrow, 4).Value = Me.txtContactPerson.Value
.Cells(lrow, 5).Value = Me.cboDepartment.Value
.Cells(lrow, 6).Value = Me.txtPosition.Value
.Cells(lrow, 7).Value = Me.cboRoleType.Value
.Cells(lrow, 8).Value = Me.txtofficeHP1.Value
.Cells(lrow, 9).Value = Me.txtOfficeHP2.Value
.Cells(lrow, 10).Value = Me.txtMobileHP1.Value
.Cells(lrow, 11).Value = Me.txtMobileHP2.Value
.Cells(lrow, 12).Value = Me.txtEmail1.Value
.Cells(lrow, 13).Value = Me.txtEmail2.Value
.Cells(lrow, 14).Value = Me.txtEmail3.Value
End If
End With
'clear the data
Me.txtCustomerName.Value = ""
Me.cboCustomerStatus.Value = ""
Me.txtContactPerson.Value = ""
Me.cboDepartment.Value = ""
Me.txtPosition.Value = ""
Me.cboRoleType.Value = ""
Me.txtofficeHP1.Value = ""
Me.txtOfficeHP2.Value = ""
Me.txtMobileHP1.Value = ""
Me.txtMobileHP2.Value = ""
Me.txtEmail1.Value = ""
Me.txtEmail2.Value = ""
Me.txtEmail3.Value = ""
End Sub
In the FindCustomerID subroutine you have exactly the same problem with the line
If CustomerID = Cells(currentrow, 1) Then
as Cells(currentrow, 1) is not qualified and therefore, should become
If CustomerID = ws.Cells(currentrow, 1) Then
You are also reassigning the CustomerID many times unnecessarily. I would take the reassignment inside the If statement and the loop will become
For currentrow = 2 To lastrow
If CustomerID = ws.Cells(currentrow, 1) Then
'find last number that applies
serialno = serialno + 1
're-assign customerID with new serial number
CustomerID = CountryCode & CustomerCode & serialno
End If
Next currentrow
This way CustomerID is only reassigned if and only if serialno changes.

Add row to top of table after headers

I want to add new data to a table with a form. I have it adding data at the bottom of the sheet.
I want the new info at the top.
With my code, it sends the data to two sheets, the "home" sheet and the sheet that is selected in the first combo box.
Private Sub CommandButton1_Click()
TargetSheet = ComboBox1.Value
If TargetSheet = "" Then
Exit Sub
End If
Worksheets(TargetSheet).Activate
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
ActiveSheet.Cells(lastrow + 1, 1).Value = TextBox1.Value
ActiveSheet.Cells(lastrow + 1, 2).Value = TextBox2.Value
ActiveSheet.Cells(lastrow + 1, 3).Value = TextBox3.Value
ActiveSheet.Cells(lastrow + 1, 4).Value = TextBox4.Value
ActiveSheet.Cells(lastrow + 1, 5).Value = TextBox5.Value
ActiveSheet.Cells(lastrow + 1, 6).Value = TextBox6.Value
ActiveSheet.Cells(lastrow + 1, 7).Value = TextBox7.Value
ActiveSheet.Cells(lastrow + 1, 8).Value = TextBox8.Value
Worksheets("Home").Activate
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
ActiveSheet.Cells(lastrow + 1, 1).Value = ComboBox1.Value
ActiveSheet.Cells(lastrow + 1, 2).Value = TextBox1.Value
ActiveSheet.Cells(lastrow + 1, 3).Value = TextBox2.Value
ActiveSheet.Cells(lastrow + 1, 4).Value = TextBox3.Value
ActiveSheet.Cells(lastrow + 1, 5).Value = TextBox4.Value
ActiveSheet.Cells(lastrow + 1, 6).Value = TextBox5.Value
ActiveSheet.Cells(lastrow + 1, 7).Value = TextBox6.Value
ActiveSheet.Cells(lastrow + 1, 8).Value = TextBox7.Value
ActiveSheet.Cells(lastrow + 1, 9).Value = TextBox8.Value
ActiveSheet.Cells(lastrow + 1, 10).Value = Date
ActiveSheet.Cells(lastrow + 1, 11).Value = TimeValue(Now)
ActiveSheet.Cells(lastrow + 1, 12).Value = TextBox9.Value
MsgBox ("Item Added Successfully.")
TextBox1.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""
TextBox4.Value = ""
Worksheets("Home").Activate
Worksheets("Home").Cells(1, 1).Select
End Sub
How do I put the new data in the second row since I have headers on the sheet?
There are a lot of stuff to improve your my code, but I want to keep it simple
Some things to begin:
Use option explicit so you don't have unexpected behavior with undefined variables
Always indent your code (see www.rubberduckvba.com a free tool that helps you with that)
Try to separate your logic defining variables and the reusing them
Name your forms' controls
Check a great article about UserForms (when you feel you're ready to advance)
Check the code's comments, and adapt it to fit your needs
EDIT: No need for the EntireRow qualifier as we are already selecting the whole row, and added the copy format from below
Code:
Private Sub CommandButton1_Click()
' Define object variables
Dim targetSheet As Worksheet
Dim homeSheet As Worksheet
Dim targetSheetName As String
Dim homeSheetName As String
Dim targetSheetTopRow As Long
Dim homeSheetTopRow As Long
Dim textBox1Value As Variant
Dim textBox2Value As Variant
Dim textBox3Value As Variant
Dim textBox4Value As Variant
Dim textBox5Value As Variant
Dim textBox6Value As Variant
Dim textBox7Value As Variant
Dim textBox8Value As Variant
Dim textBox9Value As Variant
' Define parameters
targetSheetTopRow = 2
homeSheetTopRow = 2
homeSheetName = "Home"
' Validate if combobox has any value
If Me.ComboBox1.Value = vbNullString Then Exit Sub
' Get target sheet name
targetSheetName = Me.ComboBox1.Value
' Add a reference to sheets
Set targetSheet = ThisWorkbook.Worksheets(targetSheetName)
Set homeSheet = ThisWorkbook.Worksheets(homeSheetName)
' Store current controls values
textBox1Value = Me.TextBox1.Value
textBox2Value = Me.TextBox2.Value
textBox3Value = Me.TextBox3.Value
textBox4Value = Me.TextBox4.Value
textBox5Value = Me.TextBox5.Value
textBox6Value = Me.TextBox6.Value
textBox7Value = Me.TextBox7.Value
textBox8Value = Me.TextBox8.Value
' No need to activate stuff
With targetSheet
' Insert a row after row 2
.Range(targetSheetTopRow & ":" & targetSheetTopRow).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow
' Add cells values
.Cells(targetSheetTopRow, 1).Value = textBox1Value
.Cells(targetSheetTopRow, 2).Value = textBox2Value
.Cells(targetSheetTopRow, 3).Value = textBox3Value
.Cells(targetSheetTopRow, 4).Value = textBox4Value
.Cells(targetSheetTopRow, 5).Value = textBox5Value
.Cells(targetSheetTopRow, 6).Value = textBox6Value
.Cells(targetSheetTopRow, 7).Value = textBox7Value
.Cells(targetSheetTopRow, 8).Value = textBox8Value
End With
With homeSheet
' Insert a row after row 2
.Range(homeSheetTopRow & ":" & homeSheetTopRow).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow
' Add cells values
.Cells(homeSheetTopRow, 1).Value = textBox1Value
.Cells(homeSheetTopRow, 2).Value = textBox2Value
.Cells(homeSheetTopRow, 3).Value = textBox3Value
.Cells(homeSheetTopRow, 4).Value = textBox4Value
.Cells(homeSheetTopRow, 5).Value = textBox5Value
.Cells(homeSheetTopRow, 6).Value = textBox6Value
.Cells(homeSheetTopRow, 7).Value = textBox7Value
.Cells(homeSheetTopRow, 8).Value = textBox8Value
.Cells(homeSheetTopRow, 9).Value = Date
.Cells(homeSheetTopRow, 10).Value = TimeValue(Now)
.Cells(homeSheetTopRow, 11).Value = textBox9Value
End With
' Clear control's values
Me.TextBox1.Value = vbNullString
Me.TextBox2.Value = vbNullString
Me.TextBox3.Value = vbNullString
Me.TextBox4.Value = vbNullString
' Alert user
MsgBox ("Item Added Successfully.")
' Goto...
homeSheet.Activate
homeSheet.Cells(1, 1).Select
End Sub
Let me know if it works or you need more help

Error 381 populating listbox from a sheet table

I am getting all the time error 381.. What is there wrong? If i use only 1 column it works, if i add 2nd and more it stops working.
I try to populate my rows which compile with "if statement".
it stops each time to work at 2nd column.
UserForm + some Data:
https://drive.google.com/open?id=1hfCAu2m7C4kISSPJSvyjWc-TvxBr-fOO
2nd Version of code:
Sub PopulateList2()
Dim rngName As Range
Dim ws As Worksheet
Dim i As Integer
Dim LastRow As Long
Set ws = E1G
With ListBoxAbg
.Clear
.ColumnCount = 2
LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).row
For i = 1 To LastRow
If ws.Cells(i, 6).Value < Now() _
And ws.Cells(i, 6).Value <> vbNullString Then
.AddItem
.List(i - 1, 0) = ws.Cells(i, 1).Value
.List(i - 1, 1) = ws.Cells(i, 3).Value
End If
Next i
End With
End Sub
....
Sub PopulateList2()
Dim rngName As Range
Dim ws As Worksheet
Dim i As Integer
Dim LastRow As Long
Set ws = E1G
AbgeListField.Clear
AbgeListField.ColumnCount = 7
LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).row
For i = 1 To LastRow
If ws.Cells(i, 6).Value < Now() _
And ws.Cells(i, 6).Value <> vbNullString Then
AbgeListField.AddItem ws.Cells(i, 1).Value
AbgeListField.List(i - 1, 1) = ws.Cells(i, 2).Value
AbgeListField.List(i - 1, 2) = ws.Cells(i, 3).Value
AbgeListField.List(i - 1, 3) = ws.Cells(i, 4).Value
AbgeListField.List(i - 1, 4) = ws.Cells(i, 5).Value
AbgeListField.List(i - 1, 5) = ws.Cells(i, 6).Value
AbgeListField.List(i - 1, 6) = ws.Cells(i, 7).Value
End If
Next i
End Sub
i found the answer in that post:
https://social.msdn.microsoft.com/Forums/office/en-US/f5619db9-be72-41e3-a353-54ebb021f936/runtime-error-381-could-not-set-the-list-property-invalid-property-array-index?forum=exceldev
i added new dim nxtItme As Long. it works perfect now:
Sub PopulateList2()
Dim rngName As Range
Dim ws As Worksheet
Dim i As Integer
Dim LastRow As Long
Dim nxtItem As Long
Set ws = E1G
nxtItem = 0
With ListBoxAbg
.Clear
.ColumnCount = 6
LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).row
For i = 1 To LastRow
If ws.Cells(i, 6).Value < Now() _
And ws.Cells(i, 6).Value <> vbNullString Then
.AddItem
.List(nxtItem, 0) = ws.Cells(i, 1).Value
.List(nxtItem, 1) = ws.Cells(i, 3).Value
.List(nxtItem, 2) = ws.Cells(i, 4).Value
.List(nxtItem, 3) = ws.Cells(i, 5).Value
.List(nxtItem, 4) = ws.Cells(i, 6).Value
nxtItem = nxtItem + 1
End If
Next i
End With
End Sub

Excel vba : If Listbox selected value = 2 then show textbox4 else hide textbox

I have a listbox with values from a sheet called Database OUtypes. Values are:
Single Split
Multi Split
City Multi
I want a "MultiSplit"(textbox) that shows when value 2 is selected in listbox and hide when the other values are selected.
How can i do that ?
Option Explicit
Private Sub CommandButton3_Click()
Unload Me
Menu.Show
End Sub
Private Sub userform_activate()
Application.DisplayAlerts = False
Dim cell As Range
With Worksheets("Database Bedrijf")
For Each cell In .Range("B1:B1000" & .cells(Rows.Count, 3).End(xlUp).Row)
If Not IsEmpty(cell) Then Bedrijf.AddItem cell.Value
Next cell
End With
With Worksheets("Database Freon")
For Each cell In .Range("B1:B1000" & .cells(Rows.Count, 3).End(xlUp).Row)
If Not IsEmpty(cell) Then Freontype.AddItem cell.Value
Next cell
End With
With Worksheets("Database OUtypes")
For Each cell In .Range("B2:B1000" & .cells(Rows.Count, 3).End(xlUp).Row)
If Not IsEmpty(cell) Then OUtypes.AddItem cell.Value
Next cell
End With
'''This declares the data type of the variable "LstRw'
Dim LstRw As Long
'''This defines what the variable "LstRw' is to refer to. _
(The row number of the last used cell in column A.)
LstRw = cells(Rows.Count, "A").End(xlUp).Row
'''This tells the textbox named ID to equal the value of the last used cell in Col.A after adding 1 to it.
ID.caption = cells(LstRw, "A").Value + 1
End Sub
Private Sub Freontypes_Change()
gwp.Text = Application.VLookup(Freontype.Value, Worksheets("Database Freon").Range("B2:C1000"), 2, False)
End Sub
Private Sub Bedrijf_Change()
Dim RowMax As Integer
Dim wsh As Worksheet
Dim countExit As Integer
Dim CellCombo2 As String
Dim i As Integer
Set wsh = ThisWorkbook.Sheets("Database Klant")
RowMax = wsh.cells(Rows.Count, "B").End(xlUp).Row
'find last row of sheet in column A
Klant.Clear
'clear all value of comboBox2
With Klant
For i = 2 To RowMax
If wsh.cells(i, "B").Value = Bedrijf.Text Then
'Just show value of mapping with column A
.AddItem wsh.cells(i, "C").Value
Else
End If
Next i
End With
End Sub
Private Sub Freoninhoud_Change()
If Freoninhoud.Text = "" Then
MsgBox "Vul iets in"
Exit Sub
Else
Co2.Text = CDbl(Replace(Me.Freoninhoud.Text, ".", ",")) * gwp.Text
End If
End Sub
Private Sub Userform_Initialize()
Status.AddItem "Goed"
Status.AddItem "Slecht"
Status.AddItem "Defect"
Dim RowMax As Integer
Dim wsh As Worksheet
Dim countExit As Integer
Dim CellCombo1 As String
Dim i As Integer
Dim j As Integer
Set wsh = ThisWorkbook.Sheets("Database Bedrijf")
RowMax = wsh.cells(Rows.Count, "B").End(xlUp).Row
'find last row of sheet in column A
Bedrijf.Clear
'clear all value of comboBox1
With Bedrijf
For i = 2 To RowMax
'Run each row of column A
countExit = 0
CellCombo1 = wsh.cells(i, "B").Value
For j = i To 2 Step -1
'just show value not duplicate
If CellCombo1 = wsh.cells(j, "A").Value Then
countExit = countExit + 1
End If
Next j
If countExit = 0 Then
ElseIf countExit > 1 Then
Else
.AddItem CellCombo1
End If
Next i
End With
End Sub
Private Sub CommandButton1_Click()
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Database OU")
lRow = ws.cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
.cells(lRow, 1).Value = ID.caption
.cells(lRow, 2).Value = Bedrijf.Value
.cells(lRow, 3).Value = Klant.Value
.cells(lRow, 4).Value = Ruimte.Value
.cells(lRow, 5).Value = Merk.Value
.cells(lRow, 6).Value = Types.Value
.cells(lRow, 7).Value = Multisplit.Value
.cells(lRow, 8).Value = Model.Value
.cells(lRow, 9).Value = Serienummer.Value
.cells(lRow, 10).Value = Bouwjaar.Value
.cells(lRow, 11).Value = Afvoer.Value
.cells(lRow, 12).Value = Freontype.Value
.cells(lRow, 13).Value = Freoninhoud.Value
.cells(lRow, 14).Value = Co2.Text
.cells(lRow, 15).Value = Installatienummer.Value
.cells(lRow, 16).Value = Adres.Value
.cells(lRow, 17).Value = Status.Value
End With
Unload Me
Menu.Show
End Sub
Private Sub CommandButton2_Click()
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Database OU")
lRow = ws.cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
.cells(lRow, 1).Value = ID.caption
.cells(lRow, 2).Value = Bedrijf.Value
.cells(lRow, 3).Value = Klant.Value
.cells(lRow, 4).Value = Ruimte.Value
.cells(lRow, 5).Value = Merk.Value
.cells(lRow, 6).Value = Types.Value
.cells(lRow, 7).Value = Multisplit.Value
.cells(lRow, 8).Value = Model.Value
.cells(lRow, 9).Value = Serienummer.Value
.cells(lRow, 10).Value = Bouwjaar.Value
.cells(lRow, 11).Value = Afvoer.Value
.cells(lRow, 12).Value = Freontype.Value
.cells(lRow, 13).Value = Freoninhoud.Value
.cells(lRow, 14).Value = Co2.Text
.cells(lRow, 15).Value = Installatienummer.Value
.cells(lRow, 16).Value = Adres.Value
.cells(lRow, 17).Value = Status.Value
End With
Unload Me
Outoevoegen.Show
End Sub
Private Sub Userform_QueryClose(Cancel As Integer, closemode As Integer)
If closemode = vbFormControlMenu Then
MsgBox "Sorry gebruik de Sluiten knop"
Cancel = True
End If
End Sub
Found it
Private Sub OUtypes_Change()
If OUtypes = "Multi Split" Then
Label18.Visible = True
Multisplit.Visible = True
Else
Label18.Visible = False
Multisplit.Visible = False
End If
End Sub
Edit: This can be consolidated to be a bit more legible, by using booleans:
Private Sub OUtypes_Change()
visible = (OUtypes = "Multi Split") ' Boolean expression
Label18.Visible = visible
Multisplit.Visible = visible
End Sub

Row to Column in Excel

I have a set of data in this format:-
Note: It starts from Jan-17 to Dec-17. However, for this exercise I limit it to 3 months (Jan to Mar).
I wish to convert the data into this format:-
How can i achieve it using Excel?
Thanks in advance.
How about something like below, using a double For Loop to loop through rows and then columns and transfer data to Sheet2 in the desired format (this won't add the headers to Sheet2, but it will give you a some guidance as to how to go about it):
Sub Summarize()
Dim ws As Worksheet: Set ws = Sheets("Sheet1") 'Sheet with data
Dim ws2 As Worksheet: Set ws2 = Sheets("Sheet2") 'Summarised Sheet
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
LastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
For i = 2 To LastRow 'loop through rows
For col = 6 To 14 Step 4 'loop through columns
'replace 14 with (LastCol - 4) if you wish to do all the months instead of just the first 3
FreeRow = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row + 1 'get the next free row to transfer data to
ws.Range("A" & i & ":D" & i).Copy ws2.Range("A" & FreeRow) 'copy the first 4 columns into the free row
ws2.Cells(FreeRow, 5).Value = "20" & Mid(ws.Cells(1, col).Value, 5, 2) 'get the year from the header
ws2.Cells(FreeRow, 6).Value = Left(ws.Cells(1, col).Value, 3) ' get the month name from header
ws2.Cells(FreeRow, 7).Value = ws.Cells(i, col).Value 'transfer values
ws2.Cells(FreeRow, 8).Value = ws.Cells(i, col + 1).Value
ws2.Cells(FreeRow, 9).Value = ws.Cells(i, col + 2).Value
ws2.Cells(FreeRow, 10).Value = ws.Cells(i, col + 3).Value
Next col
Next i
End Sub
UPDATE:
I've added a couple of lines to the code to attempt to optimize the speed of it, also removed the Copy & Paste and altered it to pass the values without copying anything, please have a look below:
Sub Summarize()
Dim ws As Worksheet: Set ws = Sheets("Sheet1") 'Sheet with data
Dim ws2 As Worksheet: Set ws2 = Sheets("Sheet2") 'Summarised Sheet
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
LastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
'optimize code:
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
For i = 2 To LastRow 'loop through rows
For col = 6 To 14 Step 4 'loop through columns
'replace 14 with (LastCol - 4) if you wish to do all the months instead of just the first 3
FreeRow = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row + 1 'get the next free row to transfer data to
ws2.Cells(FreeRow, 1).Value = ws.Cells(i, 1).Value
ws2.Cells(FreeRow, 2).Value = ws.Cells(i, 2).Value
ws2.Cells(FreeRow, 3).Value = ws.Cells(i, 3).Value
ws2.Cells(FreeRow, 4).Value = ws.Cells(i, 4).Value
ws2.Cells(FreeRow, 5).Value = "20" & Mid(ws.Cells(1, col).Value, 5, 2) 'get the year from the header
ws2.Cells(FreeRow, 6).Value = Left(ws.Cells(1, col).Value, 3) ' get the month name from header
ws2.Cells(FreeRow, 7).Value = ws.Cells(i, col).Value 'transfer values
ws2.Cells(FreeRow, 8).Value = ws.Cells(i, col + 1).Value
ws2.Cells(FreeRow, 9).Value = ws.Cells(i, col + 2).Value
ws2.Cells(FreeRow, 10).Value = ws.Cells(i, col + 3).Value
Next col
Next i
'return to normal Excel status after macro has finished
Application.EnableEvents = True
Application.DisplayStatusBar = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub

Resources