I'm still a Noob when it comes to VBA, but I'm gradually picking it up as I go along. I need help trying to get my simple Flexitime input form to log flexi time "taken" as negative time (-01:00) on a spreadsheet, but I'm not sure how to go about doing it.
This is what I've got so far:
Private Sub submit_Click()
Dim wb As Workbook
Dim ws As Worksheet
Dim irow As Long
Set wb = FlexBook
Set ws = FlexBook.Worksheets("Flex Data")
irow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
If Trim(Me.employee.Value) = "" Then
Me.employee.SetFocus
MsgBox "Please select a name"
Exit Sub
End If
If Trim(Me.owta.Value) = "" Then
Me.owta.SetFocus
MsgBox "Please select whether it is time taken or time owed"
Exit Sub
End If
If Trim(Me.Time.Value) = "" Then
Me.Time.SetFocus
MsgBox "Please input the amount of time"
Exit Sub
End If
If Trim(Me.dateflex.Value) = "" Then
Me.dateflex.SetFocus
MsgBox "Please input the date the flex was owed or taken"
Exit Sub
End If
If Trim(Me.author.Value) = "" Then
Me.author.SetFocus
MsgBox "Please confirm who has authorised this"
Exit Sub
End If
If Trim(Me.owta.Value) = "Owed" Then
Time = Time
ElseIf Trim(Me.owta.Value) = "Taken" Then
Time = Time * -1
Exit Sub
End If
'Insert data in to the table
ws.Cells(irow, 1).Value = Me.employee.Value
ws.Cells(irow, 2).Value = Me.owta.Value
'ws.Cells(irow, 3).Value = ? <---cell to indicate positive or negative time
ws.Cells(irow, 4).Value = CDate(Me.dateflex.Value)
ws.Cells(irow, 5).Value = Me.author.Value
'clear the data
Me.employee.Value = ""
Me.owta.Value = ""
Me.Time.Value = ""
Me.dateflex.Value = ""
Me.author.Value = ""
Me.employee.SetFocus
End Sub
You could use an instant If, an If block, or a Select Case - your choice:
ws.Cells(irow, 3).Value = IIf(Trim(Me.owta.Value) = "Owed", "+", "-")
'// However I wouldn't advise this if you want to evaluate "Owed" and "Taken" seperately.
or
If Trim(Me.owta.Value) = "Owed" Then
ws.Cells(irow, 3).Value = "+"
ElseIf Trim(Me.owta.Value) = "Taken" Then
ws.Cells(irow, 3).Value = "-"
End If
or
Select Case Trim(Me.owta.Value)
Case "Owed": ws.Cells(irow, 3).Value = "+"
Case "Taken": ws.Cells(irow, 3).Value = "-"
End Select
All have their own pros and cons, but in the context in which you are using them will show little difference.
Related
After I click the command button, I want my excel to do:
Input what I type in text boxes / select in combo boxes in specific columns without deleting the one I previously entered
But at this moment, it does not work as I expected or enter any of input from text boxes and combo boxes.
The script I wrote is:
Private Sub
If TextBox1.Value = "" Or TextBox2.Value = "" Or TextBox3.Value = "" Then
If MsgBox ("There might one or more empty cells,
do you want to continue to proceed?", vbQuestion + vbYesNo) <> vbYes Then
Exit Sub
End If
End If
Dim invsheet As Worksheet
Dim pacsheet As Worksheet
Set invsheet = ThisWorkbook.Sheets("INV")
Set pacsheet = ThisWorkbook.Sheets("PAC")
invsheet.Range("A1").Value = TextBox6.Text
invsheet.Range("I5").Value = TextBox7.Text
invsheet.Range("A21").Value = TextBox5.Text
invsheet.Range("A25").Value = ComboBox1.Value
inv_nr = invsheet.Cells(Row.Count, 1).End(xlUp).Row +1
invsheet.Cells(inv_nr, 5).Value = Me.TextBox1
invsheet.Cells(inv_nr, 4).Value = Me.ComboBox2
pac_nr = pacsheet.Cells(Row.Count, 1).End(xlUp).Row +1
pacsheet.Cells(pac_nr, 5).Value = Me.TextBox2
pacsheet.Cells(pac_nr, 5).Value = Me.TextBox3
pacsheet.Cells(pac_nr, 5).Value = Me.TextBox4
Problem:
inv_nr = invsheet.Cells(Row.Count, 1).End(xlUp).Row +1
invsheet.Cells(inv_nr, 5).Value = Me.TextBox1
invsheet.Cells(inv_nr, 4).Value = Me.ComboBox2
pac_nr = pacsheet.Cells(Row.Count, 1).End(xlUp).Row +1
pacsheet.Cells(pac_nr, 5).Value = Me.TextBox2
pacsheet.Cells(pac_nr, 7).Value = Me.TextBox3 'mistyped it. supposed to be 7
pacsheet.Cells(pac_nr, 9).Value = Me.TextBox4 'mistyped it. supposed to be 9
This block of code does not work and create any output on the worksheet.
I will really appreciate your help.
Thank you!
You're not placing anything in column A (except A1, A21, and A25 of invsheet), so it's not a good idea to set your inv_nr and pac_nr variables based on the last used cell in column A.
Try basing it on one of the columns you are populating with data, e.g. column 5:
'Always qualify "Rows" (and don't mistype it as "Row")
inv_nr = invsheet.Cells(invsheet.Rows.Count, 5).End(xlUp).Row + 1
invsheet.Cells(inv_nr, 5).Value = Me.TextBox1
invsheet.Cells(inv_nr, 4).Value = Me.ComboBox2
'Always qualify "Rows" (and don't mistype it as "Row")
pac_nr = pacsheet.Cells(pacsheet.Rows.Count, 5).End(xlUp).Row + 1
pacsheet.Cells(pac_nr, 5).Value = Me.TextBox2 'Note: This is pointless because the next line overwrites it
pacsheet.Cells(pac_nr, 5).Value = Me.TextBox3 'Note: This is pointless because the next line overwrites it
pacsheet.Cells(pac_nr, 5).Value = Me.TextBox4
I'm currently creating a reporting tool I have a problem with regards in exporting the details from User form to my Database (Sheet1).
Scenario:
What if the user checked multiple checkboxes in the user form, how will it transfer the data from multiple checkboxes to 1 row "G2" without overlapping? I'm using a command button to transfer the data to the empty cell
Sample UserForm:
# Mike
Sample WorkSheet of Userform
Something that might work then is this:
Private Sub CommandButton4_Click()
RowCount = Worksheets("Sheet1").Range("A1").CurrentRegion.Rows.count
With Worksheets("Sheet1").Range("A1")
If CheckBox1.Value = True Then
.Offset(RowCount, 6).Value = .Offset(RowCount, 6).Value & vbCrLf & "Unable to remove footer"
Else:
.Offset(RowCount, 6).Value = ""
End If
If CheckBox2.Value = True Then
.Offset(RowCount, 6).Value = .Offset(RowCount, 6).Value & vbCrLf & "Unable to use PMSectionHead as First Level header/section"
Else:
.Offset(RowCount, 6).Value = ""
End If
End With
End Sub
I have it adding to the value that is already in the cell and putting a newline before each new value that is added. Then you will probably have to add an If for each checkbox since you are manually adding a string.
You need to check each one to see if more than one checkbox is checked. If you have them in an If ... ElseIf statement it will stop after the first true statement and not check all the rest.
EDIT
Private Sub CommandButton4_Click()
Dim RowCounter
RowCounter = 0
RowCount = Worksheets("Sheet1").Range("A1").CurrentRegion.Rows.count
With Worksheets("Sheet1").Range("A1")
If CheckBox1.Value = True Then
.Offset(RowCount + RowCounter, 6).Value = .Offset(RowCount + RowCounter, 6).Value & vbCrLf & "Unable to remove footer"
RowCounter = RowCounter + 1
Else:
.Offset(RowCount, 6).Value = ""
End If
If CheckBox2.Value = True Then
.Offset(RowCount + RowCounter, 6).Value = .Offset(RowCount + RowCounter, 6).Value & vbCrLf & "Unable to use PMSectionHead as First Level header/section"
RowCounter = RowCounter + 1
Else:
.Offset(RowCount, 6).Value = ""
End If
End With
End Sub
I added a counter to the sub that will count if any of the checkboxes have been checked and go that many rows down starting with 0 rows down for 1 box checked. Not entirely sure that is what you were looking for, but I think it will work for you.
I am working with a code to update the data in database through userform. Its first part i.e. search data is working fine but second part i.e. update sometime it works fine but sometime it gives runtime error 91
need help
Private Sub cmd_Update_Click()
Application.DisplayAlerts = False
Dim ws As Worksheet
'check for a Name number
If Trim(Me.TextBox_Search_Data.Value) = "" Then
Me.TextBox_Search_Data.SetFocus
MsgBox "Please fill the data in search box"
Exit Sub
End If
Set ws = Worksheets("Employee Data")
With ws
r.Value = Me.TextBox_Search_Data.Value
r.Offset(, 1).Value = Me.TextBox_EmployeeName.Value
r.Offset(, 2).Value = Me.TextBox_FatherHusbandName.Value
r.Offset(, 3).Value = Me.ComboBox_Designation.Value
r.Offset(, 4).Value = Me.ComboBox_Category.Value
Me.TextBox_Search_Data.SetFocus
MsgBox "Data Updated Sucessfully"
'clear the data
Me.TextBox_EmployeeNumber.Value = ""
Me.TextBox_EmployeeName.Value = ""
Me.TextBox_FatherHusbandName.Value = ""
Me.ComboBox_Designation.Value = ""
Me.ComboBox_Category.Value = ""
End With
End Sub
It looks like the sheet may not be getting set properly, and thus it can't use the object in order to update. See modified code below:
Private Sub cmd_Update_Click()
Application.DisplayAlerts = False
Dim ws As Worksheet
'check for a Name number
If Trim(Me.TextBox_Search_Data.Value) = "" Then
Me.TextBox_Search_Data.SetFocus
MsgBox "Please fill the data in search box"
Exit Sub
End If
' Change ThisWorkbook to a different workbook variable as needed.
Set ws = ThisWorkbook.Worksheets("Employee Data")
If Not ws Is Nothing Then
If not r is Nothing Then
With r
.Value = Me.TextBox_Search_Data.Value
.Offset(, 1).Value = Me.TextBox_EmployeeName.Value
.Offset(, 2).Value = Me.TextBox_FatherHusbandName.Value
.Offset(, 3).Value = Me.ComboBox_Designation.Value
.Offset(, 4).Value = Me.ComboBox_Category.Value
End With
Else
'This will run if r is not set to a range.
End If
Else
'This will occur if the sheet isn't set properly.
End If
Me.TextBox_Search_Data.SetFocus
MsgBox "Data Updated Sucessfully"
'clear the data
Me.TextBox_EmployeeNumber.Value = ""
Me.TextBox_EmployeeName.Value = ""
Me.TextBox_FatherHusbandName.Value = ""
Me.ComboBox_Designation.Value = ""
Me.ComboBox_Category.Value = ""
End Sub
I'm exporting my data from MS Project to MS Excel (single pre-defined file with a given name all the time, for e.g. XYZ.xlsx) and want to have different worksheet in the excel for every workstream in the project. And number of workstreams can increase in future, thus I've to keep it dynamic.
As of now my code does the export, but I also want it to check if the workstream already exists,
- if yes, delete all the data in that worksheet and paste the new data in XYZ file.
- if no, create a new worksheet in the XYZ file and paste the data into it.
Can anyone please help as I'm on a deadline to finish it.
Code that I'm using it,
Set tsks = ThisProject.Tasks
For Each t In tsks
If Not t Is Nothing Then
If t.OutlineLevel > 1 Then
If t.OutlineLevel = 2 Then
If ExcelRowCounter > 2 Then
'Finish formatting the sheet we just finished
For i = 1 To 7
xlSheet.Columns(i).AutoFit
Next i
End If
'Add Excel sheet, name it and define column headers
AppActivate ExcelAppTitle
Set xlSheet = xlBook.Worksheets.Add
ExcelSheetName = Left(Replace(t.Name, "&", "and"), 30)
xlSheet.Name = ExcelSheetName
xlSheet.Cells(1, 1).Value = "Task Name"
xlSheet.Cells(1, 2).Value = "Duration (days)"
xlSheet.Cells(1, 3).Value = "Start Date"
xlSheet.Cells(1, 4).Value = "Finish Date"
xlSheet.Cells(1, 5).Value = "Workstream Group"
xlSheet.Cells(1, 6).Value = "% Complete"
xlSheet.Cells(1, 7).Value = "Status"
xlSheet.Range(xlSheet.Cells(1, 1), xlSheet.Cells(1, 7)).Font.Bold = True
ExcelRowCounter = 2
End If
xlSheet.Cells(ExcelRowCounter, 1).Value = t.Name
xlSheet.Cells(ExcelRowCounter, 2).Value = t.Duration / (8 * 60)
xlSheet.Cells(ExcelRowCounter, 3).Value = Format(t.Start, "mm/dd/yyyy")
xlSheet.Cells(ExcelRowCounter, 4).Value = Format(t.Finish, "mm/dd/yyyy")
xlSheet.Cells(ExcelRowCounter, 5).Value = t.Text1
xlSheet.Cells(ExcelRowCounter, 6).Value = t.PercentComplete
xlSheet.Cells(ExcelRowCounter, 7).Value = t.Number1
xlSheet.Cells(ExcelRowCounter, 1).IndentLevel = 2 * (t.OutlineLevel - 2)
If t.Summary = "True" Then
xlSheet.Range(xlSheet.Cells(ExcelRowCounter, 1), xlSheet.Cells(ExcelRowCounter, 6)).Font.Bold = True
End If
ExcelRowCounter = ExcelRowCounter + 1
End If
End If
Next t
For i = 1 To 7
xlSheet.Columns(i).AutoFit
Next i
Here's as simple method:
Function AddOrGetWorksheet(withName As String) As Worksheet
Dim found As Boolean
found = False
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
If (LCase(ws.Name) = LCase(withName)) Then
found = True
Set AddOrGetWorksheet = ws
Exit For
End If
Next
If (Not found) Then
Set AddOrGetWorksheet = ActiveWorkbook.Sheets.Add()
AddOrGetWorksheet.Name = withName
End If
End Function
Can you help me to my problem , I want to happen in my program is that when you type a name to textbox in userform like this "Vincent" it will pass to the sheet in the same manner
Private Sub cmdAdd_Click()
If cmdAdd.Caption = "ADD" Then
txtName.Enabled = True: cboAge.Enabled = True:
cmdAdd.Caption = "SAVE": cmdClose.Caption = "CANCEL"
txtName.SetFocus
Else
If txtName.Text = "" Or cboAge.Text = "" Then
MsgBox "Required field(s) missing!", vbCritical, "Message"
Else
For i = 2 To Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
If LCase(txtName.Text) = Sheet1.Cells(i, 1).Value And _
cboAge.Text = Sheet1.Cells(i, 2).Value Then
MsgBox "Record already exist!", vbExclamation, "Message"
Call UserForm_Activate
Exit Sub
End If
Next i
r = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row + 1
Sheet1.Cells(r, 1).Value = (txtName.Text)
Sheet1.Cells(r, 2).Value = cboAge.Text
r = 0
MsgBox "One record saved!", vbInformation, "Message"
Call UserForm_Activate
End If
End If
End Sub
this is the code can you help me ..thank you
Use StrConv to convert to Propercase. See this MSDN Link
Debug.Print StrConv(Textbox1.Text, vbProperCase)
For example (From your code)
Sheet1.Cells(r, 1).Value = StrConv(txtName.Text, vbProperCase)