I have been following a tutorial and playing with developing Fully Automated User Data Entry Form.
The Video I followed PSB:
https://www.youtube.com/watch?v=P53T6oxgUVA&ab_channel=TheDataLabs
After having downloaded his files and gone through the code step by step, I still get a yellow highlight over the particular section of the ELSE statement on the Sub Clear()? Additionally my Listbox does not update correctly it replaces the previous entry with the next entry saved.
Please view the code below
Sub Clear()
Dim iRow As Long
iRow = [Counta(Room Access 2!A:A)] '''Identify Last Row as per tutorial''
With Frm_Room_Access_2
.Surname_Txtbx.Value = ""
.Rank_Cmbobx.Value = ""
.Section_Txtbx.Value = ""
.Extention_Txtbx.Value = ""
.Service_Number_Txtbx.Value = ""
.Due_Time_Txtbx.Value = ""
.OpBtn_Time_as_Now.Value = False
.SerialNo_Txtbx.Value = ""
If iRow > 1 Then
.List_Database.RowSource = "Room Access 2!A2:G" & iRow
Else
.List_Database.RowSource = "Room Access 2!A2:G" '''' Keeps Highlighting Error ''''
End If
End With
End Sub
Sub Submit()
Dim sh As Worksheet
Dim iRow As Long
Set sh = ThisWorkbook.Sheets("Room Access 2")
If Frm_Room_Access_2.SerialNo_Txtbx.Value = "" Then
iRow = [Counta(Room Access 2!A:A)] + 1
Else
iRow = Frm_Room_Access_2.SerialNo_Txtbx.Value
End If
With sh
.Cells(iRow, 1) = iRow - 1
.Cells(iRow, 2) = Frm_Room_Access_2.Surname_Txtbx.Value
.Cells(iRow, 3) = Frm_Room_Access_2.Service_Number_Txtbx.Value
.Cells(iRow, 4) = Frm_Room_Access_2.Rank_Cmbobx.Value
.Cells(iRow, 5) = Frm_Room_Access_2.Section_Txtbx.Value
.Cells(iRow, 6) = Frm_Room_Access_2.Extention_Txtbx.Value
.Cells(iRow, 7) = IIf(Frm_Room_Access_2.OpBtn_Time_as_Now.Value = True And Frm_Room_Access_2.Due_Time_Txtbx.Value = "", Now(), Frm_Room_Access_2.Due_Time_Txtbx.Value)
End With
End Sub
The Issues that I want to resolve is firstly why is my list box not adding another entry under the first entry, it simply replaces the first entry upon saving. Do I require another method for this?
Secondly why is the code Highlighting that the Else Statement is incorrect despite following the tutorial and replicating the code correctly to my particular needs.
Any advice will be greatly appreciated.
The last cell with text in a column can be found with
Range("B" & 1).End(xlDown).Offset(1, 0).Select
The Offset(1, 0) selects the cell below in order for you to copy data to it.
To select a worksheet use:
Worksheets("Room Access 2").Select
No underlines are needed.
Related
Hey I have been writing some code to add a part ID to a spreadsheet off of a user form in Excel VBA. I have been reading through different documentation and can not figure out why no matter what type of method of inserting a row I try it inserts a row with a repeating value instead of a blank one. If anyone knows how to specify blank, other than writing the whole row to blank and then writing my numbers I want after, that would be appreciated.
I have tried both the following lines to add a row
Cells (x+1 ,column).EntireRow.Insert Shift:= xlDown
ws1.Rows(x+1).Insert Shift:=xlDown
This is the function it is used in:
Public Sub Add(IDRange As Range)
SearchCell = Cells(x, IDRange.Column)
Cells(x, IDRange.Column).Select
Do
If SearchCell = PartID Then
MsgBox " this Company Already uses this part"
Exit Sub
ElseIf x <> StopRow Then
x = x + 1
SearchCell = Cells(x, IDRange.Column)
End If
Loop While x <> StopRow And SearchCell <> PartID
Cells(x + 1, IDRange.Column).EntireRow.Insert Shift:=xlDown
Cells(x, IDRange.Column).Value = PartID
MsgBox PartID & " has been added to Adress " & Cells(x, IDRange.Column).Address
Cells(x, IDRange.Column).Select
End Sub
Bellow is the function that calls the Add Function and where I belive it may be getting the company name from
Private Sub AddPart_Click()
AddPartCounter = 0
Company = UserForm1.CompanyBox.Value
PartID = UserForm1.PartBox.Value
If Company = "" Then
MsgBox " Please put in the company you would like the part to go under"
ElseIf PartID = "" Then
MsgBox " Please put in the Part you would like entered"
ElseIf UserForm1.Studs.Value = False And UserForm1.Spreaders.Value = False And UserForm1.Blocks.Value = False And UserForm1.Imma.Value = False Then
MsgBox "Please select the type of part you are trying to add"
Else
Dim CurrentCell
Set CurrentCell = Cells.Find(What:=Company, LookAt:=xlWhole)
If CurrentCell Is Nothing Then
MsgBox " Company Not Found "
Exit Sub
End If
x = CurrentCell.Row
Do
Set CurrentCell = CurrentCell.Offset(1, 0)
Loop While CurrentCell.Offset(1, 0) = "" And Not CurrentCell Is Nothing And CurrentCell.Offset(1, 0).Row <> thisvar.Row + 1
StopRow = CurrentCell.Row
'If they are trying to add a nut
If UserForm1.Imma.Value = True Then
Call Add(Nut_ID_Rng)
'IF they are trying to add a stud
ElseIf UserForm1.Studs.Value = True Then
Call Add(Stud_ID_Rng)
'If they are trying to add a block
ElseIf UserForm1.Blocks.Value = True Then
Call Add(Block_ID_Rng)
'If they are trying to add a spreader
ElseIf UserForm1.Spreaders.Value = True Then
Call Add(Spreader_ID_Rng)
End If
End If
AddPartCounter = 1
End Sub
I know that the repeating pattern is coming from the insert line through debugging but I can not figure out why I have tried changing variables to numbers and it still did the same thing. This what it looks like with the repeating values.
enter image description here
The problem is that you most likely have a value still stored in your clipboard when you execute the Macro. To fix that, simply add this line of dode before running the insert line:
Applcation.CutCopyMode = False
That will clear your clipboard and allow the inserted rows to be blank.
I am trying to transfer number of content controls to excel spreadsheet. The issue is, I would only like to transfer words which were selected by used with bold. I would appreciate any help.
I have already tried to adopt one of your workflows but I am stuck.
Dim txtboxstring As Characters
Dim CharacterFont As Font
Dim counter As Integer
(...)
ElseIf C_C.Title = "Data_Sheet_Structural_Type" Then
Workbooks(DES_Bridges_WrkBook).Sheets(DES_Bridges_WrkSheet).Cells(i, 17).Value = C_C.Range.Text
'Test for bold text
ElseIf C_C.Title = "Data_Sheet_Material" Then
Set textboxstring = C_C.Range.Characters
For counter = 1 To textboxstring.Count
CharacterFont = textboxstring.Font
If CharacterFont.Bold Then
BoldFound = BoldFound & txtboxstring(counter)
End If
Next counter
Workbooks(DES_Bridges_WrkBook).Sheets(DES_Bridges_WrkSheet).Cells(i, 18).Value = BoldFound
BoldFound = ""
ElseIf C_C.Title = "Deck_Width" Then
Workbooks(DES_Bridges_WrkBook).Sheets(DES_Bridges_WrkSheet).Cells(i, 19).Value = C_C.Range.Text
(...)
I get following error:
Objec doesnt support Property or Method
in line
CharacterFont = textboxstring.Font
I would appreciate any help.
I have a excel spreadsheet where I have values in a form format, I need to convert them into tabular format. example -
Project ID/Name: 3001 Miscellaneous Improvements
Location: This is Project Location.
Description: This is the project description. This is the project description. This is the project description. This is the project description. This is the project description. This is the project description. This is the project description. This is the project description.
Justification: This is the project Justification. This is the project Justification. This is the project Justification. This is the project Justification. This is the project Justification. This is the project Justification. This is the project Justification.
Duration: Q1 2013 to Ongoing
Status: This is some status
Each block starts with Project ID/Name, however, Description and Justification can vary according to the size of text they have. All the headings are in Column A. If I use Find for ProjectID - and use offset at a fixed length it works but if Justification and description are bigger or smaller they don't fall in correct place. Please help.
You can use TextToColumns. Example:
'Split this cells when find ':" or <TABS>
[A1:A6].TextToColumns Destination:=[A1], DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, Tab:=True, OtherChar:=":", _
FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
From what I understand, you want to convert a vertical "form" into a table of data. I suggest adding that data to an existing table.
Here's the code.
You'll need to edit some variables (sheet/range names)
Public Sub test()
'insert your code to get each Worksheet and it's column range here
transferFormDataToTable Range("Sheet1!B1:B100"), Worksheets(2).ListObjects(1)
End Sub
Public Sub transferFormDataToTable(yourRangeB As Range, dbTable As ListObject)
' make a reference to the form data range
Dim formRange As Range
Set formRange = yourRangeB
'create a new ListRow in your target table
Dim listR As ListRow
Set listR = dbTable.ListRows.Add
'transfer the data from form to the new ListRow
Dim lastHeader As String
lastHeader = ""
Dim targetColumnOffset As Integer
targetColumnOffset = 0
Dim currentColumn As Integer
currentColumn = 0
Dim i As Integer
For i = 1 To formRange.Count
'if the row's header is not empty and different than previous row
'then we'll know we have a new column of different type of data
If lastHeader <> formRange(i).Offset(0, -1).Value And formRange(i).Offset(0, -1).Value <> "" Then
lastHeader = formRange(i).Offset(0, -1).Value
targetColumnOffset = 0
currentColumn = currentColumn + 1
End If
'this loop captures data that might have been placed in columns to the right of the input cell
Dim rowString As String
rowString = ""
Dim j As Integer
j = 0
Do While True
If formRange(i).Offset(0, j).Value <> "" Then
If rowString = "" And targetColumnOffset = 0 Then
rowString = formRange(i).Offset(0, j).Value
Else
rowString = rowString & "; " & formRange(i).Offset(0, j).Value
End If
j = j + 1
Else
Exit Do
End If
Loop
If targetColumnOffset = 0 Then
listR.Range(currentColumn).Value = rowString
Else
listR.Range(currentColumn).Value = listR.Range(currentColumn).Value & rowString
End If
targetColumnOffset = targetColumnOffset + 1
'Exit the loop if it seems to get the end
If formRange(i).Value = "" And formRange(i).Offset(0, -1).Value = "" Then _
Exit For
Next i
End Sub
Notes:
Excel has weird bugs occasionally when creating editing with VBA empty tables that have only 1 or 2 rows. I suggest using this macro only when your table has 3+ rows.
Send me a note if you want a much more complete version of this. Namely, a problem you might eventually have with this short version is that the code will screw up if a user switches columns around.
EDIT
I just adapted the code to your requirements. This is bound to get buggy eventually though. I'd really look into convincing the team about just how much they need to find a more appropriate tool. Good luck.
I am new to Excel macros..
Can anyone tell me what this macro does?
Sub People_Add_Document()
prow = ActiveCell.row
num = Cells(prow, 1).Value
wshet = ActiveSheet.Name
If (Val(num) > 0) _
And (Cells(4, 1).Value = "#") _
And (wsheet = People_wsheet) _
Then
people_select_link_to_Document process_wbook_path, prow
End If
End Sub
Sub people_select_link_to_Document(process_wbook_path, prow)
If Len(Cells(prow, DocumentFile).Value) = 0 Then
Fname = Application.GetOpenFilename("Document Files (*.doc;*.pdf),*.doc;*.pdf", 1, "Select the Document file..")
If Fname <> False Then
Cells(prow, DocumentFile).Value = Fname 'global path
End If
End If
End Sub
Get the row number of the active cell:
prow = ActiveCell.row
Get the value in column 1 of that row:
num = Cells(prow, 1).Value
Read the name of the active worksheet (there is an error here, should read wsheet rather than wshet):
wshet = ActiveSheet.Name
Test if num is greater than 0, and the cell A4 contains "#" and the active worksheet is equal to a variable or constant called People_wsheet. And if so, a subroutine called people_select_link_to_Document is called with parameters process_wbook_path and prow
If (Val(num) > 0) _
And (Cells(4, 1).Value = "#") _
And (wsheet = People_wsheet) _
Then
people_select_link_to_Document process_wbook_path, prow
End If
Now, that subroutine first of all checks to see if the DocumentFile column of the active row is empty. Actually it's a rather lame way to test emptyness, but it will probably do.
If Len(Cells(prow, DocumentFile).Value) = 0 Then
And if it is empty then we show a file dialog to obtain a file name:
Fname = Application.GetOpenFilename("Document Files (*.doc;*.pdf),*.doc;*.pdf", 1, "Select the Document file..")
If a filename has been selected (i.e. the dialog is not cancelled) then we save that file name in the DocumentFile column of the active row for future reference:
If Fname <> False Then
Cells(prow, DocumentFile).Value = Fname 'global path
End If
And that's it!
Good Day,
really need some help here, im bad at VBA.
Had created a spreadsheet and recorded a macro to record checkin of staff. However, im having difficulties checking out with the corresponding users based on the name.
Could anyone help me out over here?
Thanks. Had attached the spreadsheet for your ref.
http://www.etechnopia.com/vish/Book1ss.xlsm
After much googling, This is what i did based on mikes solution
Dim name As String
Dim id As Integer
Dim checkin As Date
Dim checkout As Date
name = Range("d6").Value
id = Range("d7").Value
checkin = Now
Range("d10") = checkin
Help anyone? im my very best here.
firstly I recommend to use range names for the important cells of your sheet
D6 EmpName
D7 EmpNo
D10 ClockInTime
D11 ClockOutTime
H5..H11 DataTable
This will enable you to reference them by name instead of hardcoding their addresses (bad bad hardcoding :-/ )
Secondly, your [Button] must serve a dual purpose ... it has to decide if a user is clocked in or out and do different things
a hi-level META code, executed at pressing [Button4] could be
if user clocked in
write current time into ClockOutTime ' remark: this may be superfluous
find DataTable record (EmpName, ClockInTime)
write ClockOutTime into record (EmpName, ClockInTime)
erase EmpName, EmpID, ClockInTime, ClockOutTime
else
write current time into ClockInTime
find first blank record in DataTable
write EmpName, EmpID, ClockInTime into DataTable record
endif
How to decide if a user is clocked in? If many users are using the same sheet at the same time (meaning 5 emps go there, write in their names and clock in) you need to examine DataTable for the first record of EmpNane without a ClockOutTime - if found he/she is in and needs to be clocked out.
more later ...
OK ... sorry was interrupted by Lady Gaga concerto in Vienna/AT
so here's a full code for the button
Sub ButtonPressed()
Dim DB As Range, Idx As Integer
Set DB = Range("DataTable")
If Range("EmpName") = "" Or Range("EmpNo") = "" Then
MsgBox "Enter your name and ID before pressing the button", vbCritical + vbOKOnly, "missing input"
Exit Sub
End If
Idx = UserClockedIn()
If Idx <> 0 Then
DB(Idx, 4) = Date + Time()
DB(Idx, 5).Formula = "=" & DB(Idx, 4).Address(RowAbsolute:=False, ColumnAbsolute:=False) & "-" & DB(Idx, 3).Address(RowAbsolute:=False, ColumnAbsolute:=False)
DB(Idx, 5).NumberFormat = "[hh]:mm"
Range("EmpName") = ""
Range("EmpNo") = ""
Else
Idx = 2
Do While DB(Idx, 1) <> ""
Idx = Idx + 1
Loop
DB(Idx, 1) = Range("EmpName")
DB(Idx, 2) = Range("EmpNo")
DB(Idx, 3) = Date + Time()
End If
End Sub
Private Function UserClockedIn() As Integer
Dim DB As Range, Idx As Integer
Set DB = Range("DataTable")
UserClockedIn = 0
Idx = 2
Do While DB(Idx, 1) <> ""
If DB(Idx, 1) = Range("EmpName") And DB(Idx, 2) = Range("EmpNo") And DB(Idx, 4) = "" Then
UserClockedIn = Idx
Exit Function
End If
Idx = Idx + 1
Loop
End Function
#user502908: I have not documented it because I want you to find out exactly what it does and by that have a quick start into Excel-VBA :-) It doesn't do too much and there are some basic thechniques you will apply again & again if you go into VBA ... try to populate ranges "ClockInTime" and "ClockOutTime" :-)))
Book1ssNew.xlsm
have fun
I tried another simpler method which i could cope with
Sub yes()
Dim findId As Integer
Dim FirstAddress As String
Dim FindString As Integer
Dim Rng As Range
FindString = Range("d7").Value
If Trim(FindString) <> "" Then
With Sheets("Sheet1").Range("F1:J100")
Set Rng = .find(What:=FindString, _
After:=.Cells(1), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
FirstAddress = Rng.Address
Rng.Offset(0, 2).Value = Now()
Else
MsgBox "Nothing found"
End If
End With
End If
End Sub
Search entire spreadsheet given id, when id found, to indicate dynamically the checkin timing.