I have two sheets first one called Sheet8 is for the Main table that can be used by the Data Entry Form to enter the data in that table, and the second called Sheet9 that includes the table for the vlookup. What I want is in the data entry user form as soon as I enter the Name the Discipline is created automatically based on that name.
Screenshot of the Data Entry for the Main table in sheet8
Screenshot of the sheet9 Table
The code for Save Button
Private Sub btnSave_Click()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Sheet8")
Dim n As Long
n = sh.Range("A" & Application.Rows.Count).End(xlUp).Row
sh.Unprotect "1234"
sh.Range("A" & n + 1).Value = Me.txtDate.Value
sh.Range("B" & n + 1).Value = Me.txtName.Value
sh.Range("C" & n + 1).Value = Me.txtProjNo.Value
sh.Range("D" & n + 1).Value = Me.txtProjTitle.Value
sh.Range("E" & n + 1).Value = Me.txtBVEntity.Value
sh.Range("F" & n + 1).Value = Me.txtZIG.Value
sh.Range("G" & n + 1).Value = Me.txtSpenthrs.Value
sh.Range("H" & n + 1).Value = Me.comboCategory.Value
sh.Range("I" & n + 1).Value = Me.txtDiscipline.Value
sh.Range("J" & n + 1).Value = Me.txtSCV.Value
sh.Range("K" & n + 1).Value = Me.txtTotSCV.Value
sh.Range("L" & n + 1).Value = Me.txtCotMER.Value
sh.Range("M" & n + 1).Value = Me.txtBudgethrs.Value
sh.Range("N" & n + 1).Value = Me.txtBudget.Value
sh.Range("O" & n + 1).Value = Me.txtProgress.Value
sh.Range("P" & n + 1).Value = Me.txtEndDate.Value
sh.Protect "1234"
The code for the Name textbox
Private Sub txtName_AfterUpdate()
If WorksheetFunction.CountIf(Sheet9.Range("C:D"), Me.txtName.Value) = 0 Then
MsgBox "This Name is Invalid."
Me.txtName.Value = ""
Exit Sub
End If
With Me
.txtDiscipline = Application.WorksheetFunction.VLookup(Me.txtName, Sheet9.Range("Lookup"), 4, 0)
End With
End Sub
Related
I have written a program that counts bins that are empty (verified), empty (unverified), and not accessible (bins locked).
I am trying to count the bins that are locked from my Bin Conversions sheet that if they are TRUE (there are 20 that are true), then they are locked and will be counted on my Bin Report sheet.
My Bin Reports sheet counts 1 too many for each group (all groups total 23 instead of 20). A group example would be 4-Pallet, 2.5ft, 2 bins locked (instead of 1).
Bin Report
Bin Conversions
Sub getBinStatusArray()
calc (False)
Dim dSH As Worksheet
Dim brSH As Worksheet
Dim bcSH As Worksheet
Set dSH = ThisWorkbook.Sheets("data")
Set brSH = ThisWorkbook.Sheets("Bin Report")
Set bcSH = ThisWorkbook.Sheets("Bin Conversions")
Dim binLockCell As Byte, binType As String, binSize As Variant, binLocked As Boolean, b As Long, i As Long
Dim dataArray() As Variant
Dim binIDArray As Variant
'Create empty array cells
ReDim Preserve dataArray(1 To dSH.Range("A" & Rows.Count).End(xlUp).Row, 1 To 3)
'Navigates cells
With dSH
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
dataArray = .Range(.Cells(lastrow, 1), .Cells(1,
.Columns.Count).End(xlToLeft)).Value
End With
'Count Bin Conversion Cells
With bcSH
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow
.Range("E" & i).Value2 = Application.WorksheetFunction.CountIf(dSH.Range("A:A"), .Range("A" & i).Value2)
Next i
End With
'Generate Bin Report
With brSH
.Cells.ClearContents
.Range("H1").Value = "Filter Input"
.Range("B1").Value = "Bin Type"
.Range("I1").Value = "Bin Type"
.Range("C1").Value = "Bin Height"
.Range("J1").Value = "Bin Height"
.Range("D1").Value = "Verified"
.Range("K1").Value = "Verified"
.Range("E1").Value = "Unverified"
.Range("L1").Value = "Unverified"
.Range("F1").Value = "Bins Locked"
.Range("M1").Value = "Bins Locked"
For i = 2 To lastrow
If bcSH.Range("E" & i).Value = 1 Or Application.WorksheetFunction.CountIfs(bcSH.Range("G" & i), "true") Then
binType = bcSH.Range("B" & i).Value
binSize = bcSH.Range("C" & i).Value
binLocked = bcSH.Range("H" & i).Value
If .Range("b2") = "" Then
.Range("b2").Value = bcSH.Range("B" & i).Value
.Range("c2").Value = bcSH.Range("C" & i).Value
.Range("F2").Value2 = Application.WorksheetFunction.CountIfs(bcSH.Range("G" & i), "true")
ElseIf .Range("b2") <> "" Then
lastrow = brSH.Cells(Rows.Count, 2).End(xlUp).Row
For b = 2 To lastrow + 1
If brSH.Range("B" & b) = binType And brSH.Range("C" & b) = binSize Then
brSH.Range("D" & b) = brSH.Range("D" & b) + bcSH.Range("E" & i)
binLockCell = Application.WorksheetFunction.CountIfs(bcSH.Range("G" & i), "true")
brSH.Range("F" & b) = binLockCell + brSH.Range("F" & b)
Exit For
ElseIf b = lastrow Then
.Range("b" & b + 1).Value = bcSH.Range("B" & i).Value
.Range("c" & b + 1).Value = bcSH.Range("c" & i).Value
.Range("D" & b + 1).Value = bcSH.Range("E" & i).Value
binLockCell = Application.WorksheetFunction.CountIfs(bcSH.Range("G" & i), "true")
.Range("F" & b + 1) = binLockCell + .Range("F" & b + 1)
End If
Next b
End If
End If
Next i
Range("b1").CurrentRegion.sort key1:=Range("b1"), order1:=xlAscending, _
key2:=Range("C1"), order2:=xlAscending, Header:=xlYes
End With
calc (True)
End Sub
You are looping For b = 2 To lastrow + 1 but adding a new line when b = lastrow i.e. before the loop has ended. So on the last iteration when b = lastrow + 1 it summates the record again. One fix would be use a flag.
ElseIf .Range("b2") <> "" Then
Dim bExists: bExists = False
lastrow = brSH.Cells(Rows.Count, 2).End(xlUp).Row
' increment existing
For b = 2 To lastrow
If brSH.Range("B" & b) = binType And brSH.Range("C" & b) = binSize Then
brSH.Range("D" & b) = brSH.Range("D" & b) + bcSH.Range("E" & i)
binLockCell = Application.WorksheetFunction.CountIfs(bcSH.Range("G" & i), "true")
brSH.Range("F" & b) = binLockCell + brSH.Range("F" & b)
bExists = True
Exit For
Next b
' or add new line
If Not bExists Then
.Range("b" & b + 1).Value = bcSH.Range("B" & i).Value
.Range("c" & b + 1).Value = bcSH.Range("c" & i).Value
.Range("D" & b + 1).Value = bcSH.Range("E" & i).Value
binLockCell = Application.WorksheetFunction.CountIfs(bcSH.Range("G" & i), "true")
.Range("F" & b + 1) = binLockCell + .Range("F" & b + 1)
End If
End If
I have an Excel sheet (doc1) with 4 columns. In "A" I have people names. In "B","C" and "D", I have informations on the CV of each of these people. I would like to extract in another sheet (doc2) these informations in a specific format: For each CV information, I would like to insert a row with the name of the person in "A" and one information about his CV in "B". Basically if I have 3 informations about a person in doc1 (In B,C and D), I want to have 3 rows : In A1, A2 and A3 the name of the person, and in B1, B2 and B3 the person's infos.
I have a macro which does the exact opposite, it is basically doing a Vlookup which throws multiple results. Any idea on how to turn this around? Thanks!
Option Explicit
Sub GO()
Dim J As Long
Dim I As Integer
Dim K As Long
Dim Indice As Long
Dim Tablo
Dim Nb As Integer
Application.ScreenUpdating = False
ReDim Tablo(1 To Range("A" & Rows.Count).End(xlUp).Row - 2, 1 To 2)
Tablo(1, 1) = Range("A2")
Tablo(1, 2) = Range("B2")
Nb = 1
For J = 3 To Range("A" & Rows.Count).End(xlUp).Row
For K = 1 To UBound(Tablo)
If Range("A" & J) = Tablo(K, 1) Then
For I = 1 To UBound(Tablo, 2)
If Tablo(K, I) = "" Then
Tablo(K, I) = Range("B" & J)
Exit For
End If
Next I
If I > UBound(Tablo, 2) Then
ReDim Preserve Tablo(1 To UBound(Tablo), 1 To UBound(Tablo, 2) + 1)
Tablo(K, UBound(Tablo, 2)) = Range("B" & J)
End If
Exit For
ElseIf Tablo(K, 1) = "" Then
Nb = Nb + 1
Tablo(K, 1) = Range("A" & J)
Tablo(K, 2) = Range("B" & J)
Exit For
End If
Next K
Next J
With Sheets("doc2")
.Cells.ClearContents
.Range("A2").Resize(Nb, UBound(Tablo, 2)) = Tablo
.Range("A1") = "Name"
.Range("B1") = "C.V info 1"
.Range("B1").AutoFill .Range("B1").Resize(, UBound(Tablo, 2) - 1), xlFillSeries
End With
End Sub
try somethihng like this:
Function NeverCallAFunctionGO:
dim doc1 as worksheet, doc2 as worksheet
dim lRow as long
'set your doc1 and doc2 sheets
lRow = 1
For i = 1 to doc1.range("A1").end(xldown).row
doc2.range("A" & lRow).value = doc1.range("A" & i).value
doc2.range("B" & lRow).value = doc1.range("B" & i).value
doc2.range("B" & lRow+1).value = doc1.range("C" & i).value
doc2.rangE("B" & lRow+2).value = doc1.rangE("D" & i).value
lRow = lRow + 3
Next i
The following code runs but, not getting the results. The information is there in the correct range.
Dim ID As Range
Dim SN As Range
Dim i As Integer
Set ID = Sheet6.Range("B2:B8")
Set SN = Sheet2.Range("C7:C184")
For i = 2 To ID.Cells.count
If ID.Cells(i) = SN.Cells(i) Then
MsgBox "do something"
ID.Cells.Offset(0, 2).Value = SN.Cells.Offset(0, -2).Value
Else
MsgBox "sorry"
End If
Next
i found another code and modified it to my work sheet. This one works great.
Dim i As Long
Dim j As Long
For i = 2 To 40
If Sheet6.Range("C" & i).Value = "" Then
Exit For
End If
For j = 7 To 1000
If Sheet2.Range("c" & j).Value = "" Then
Exit For
End If
If Sheet6.Range("C" & i).Text = Sheet2.Range("c" & j).Text Then
Sheet6.Range("C" & i).Offset(0, 1).Value = Sheet2.Range("c" & j).Offset(0, -2).Value
Sheet6.Range("C" & i).Offset(0, 2).Value = Sheet2.Range("c" & j).Offset(0, 2).Value
Exit For
End If
Next j
Next i
I'm trying to make multiple selections from Sheet2. The value is from the same column but different rows (thinking if using ActiveCell.Offset(1,0) will be feasible).
My code takes the value from an ActiveCell select and runs a macro compares it to another sheet (Sheet10) with some information to copy and paste in a target sheet (Sheet5).
The following is the code that I have right now.
a = Sheet10.Cells(Rows.Count, 1).End(xlUp).Row
c = Sheet2.Cells(Rows.Count, 5).End(xlUp).Row
For Each cell In Range(ActiveCell, ActiveCell.Offset(1, 0))
For i = 2 To a 'from Row 1 to the last row of "DMP"
Debug.Print ("i = " & i)
If cell.Value = Sheet10.Cells(i, 1).Value Then 'if selected cell matches (i,1) of "Sheet10 (DMP)"
Debug.Print ("ActiveCell =" & ActiveCell.Value)
For k = 1 To 20 'from Column 1 to Column 20
Debug.Print ("k = " & k)
For r = 1 To c 'from Row 1 to the last row of "Sheet 2(LightOn SKU)"
Debug.Print ("r = " & r)
If Sheet10.Cells(i, k).Value = Sheet2.Cells(r, 4).Value Then 'if value of (i,k) of "Sheet10 (DMP)" = (r,4) of "Sheet2 (LightOn SKU)"
Sheet2.Range("A" & r & ":G" & r).Copy
Sheet5.Activate
b = Sheet5.Cells(Rows.Count, 1).End(xlUp).Row
Sheet5.Cells(b + 1, 1).Select
ActiveSheet.Paste
Range("A" & r & ":L" & r).Borders.Color = vbBlack
End If
Next
Next
End If
Next
Next
Right now, it's running on an endless loop.
Still Unclear
Sub ACCopy2()
Dim a As Long
Dim c As Long
Dim r As Long
Dim i As Long
Dim k As Integer
Dim b As Long
a = Sheet10.Cells(Rows.Count, 1).End(xlUp).Row
c = Sheet2.Cells(Rows.Count, 5).End(xlUp).Row
For r = 1 To c ' from Row 1 to the last row of "Sheet 2(LightOn SKU)"
Debug.Print ("r = " & r)
For i = 2 To a ' from Row 1 to the last row of "DMP"
Debug.Print ("i = " & i)
' if selected cell matches (i,1) of "Sheet10 (DMP)"
If Sheet2.Cells(r, 1).Value = Sheet10.Cells(i, 1).Value Then
Debug.Print ("Sheet2 =" & Sheet2.Cells(r, 1).Value)
For k = 1 To 20 ' from Column 1 to Column 20
Debug.Print ("k = " & k)
' if value of (i,k) of "Sheet10 (DMP)" = (r,4) of
' "Sheet2 (LightOn SKU)"
If Sheet10.Cells(i, k).Value = Sheet2.Cells(r, 4).Value Then
With Sheet5
b = Sheet5.Cells(Rows.Count, 1).End(xlUp).Row
Sheet2.Range("A" & r & ":G" & r).Copy .Cells(b + 1, 1)
.Range("A" & r & ":L" & r).Borders.Color = vbBlack
End With
End If
Next
End If
Next
Next
End Sub
The user can enter up to 10 members at once.
Column A will be "Team Number"
Column B will be "Number of Member"
Column C will be "Member Name"
Column D will be "Month Available"
Column E will be "Number of Family Members Coming"
Column F will be "Family Members"
I have trouble trying to input the userform values to the worksheet.
'inputValue
Dim RowCount As Long
Dim rStart As Long
Dim rFirstEnd As Long
Dim rLastEnd As Long
RowCount = Worksheets("Sheet1").Range("A1").CurrentRegion.Rows.Count
rMemberEnd = CLng(txtNoMember.Value)
rMonthEnd = CLng(txtNoMember.Value)
rFamilyMemberEnd = CLng(txtNoFamilyMemberValue)
For rStart = 1 To rMemberEnd
With Worksheets(“Sheet1").Range("A1")
.Offset(RowCount + rStart, 0).Value = txtTeamNo.Text
.Offset(RowCount + rStart , 1).Value = txtNoMember.Text
.Offset(RowCount + rStart , 2).Value = Controls("txtMemberName” & Format(rStart, "00")).Value
For rStart = 1 To rMonthEnd
With Worksheets(“Sheet1").Range("A1")
If Controls ("chkMonth” & Format(rStart, "00")).Value = True Then
.Offset(RowCount + rStart , 3).Value = CLng(Right$(Controls("chkMonth” & Format(rStart, "00")).Name, 2))
.Offset(RowCount + rStart , 4).Value = txtNoFamilyMember.Text
For rStart = 1 To rFamilyMemberEnd
With Worksheets(“Sheet1").Range("A1")
.Offset(RowCount + rStart , 5).Value = Controls("txtFamilyMember" & Format(rStart, "00")).Text
End With
End If
End With
End With
Next
This is the input to the worksheet.
This is what the UserForm looks like
I've built a UserForm similar to yours and named the controls on it accordingly
now, double clicked the command button and used this code
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
Dim members As Long
members = CLng(txtNoMember.Value)
Dim family As Long
family = CLng(txtNoFamilyMember.Value)
Dim months As Long
Dim i As Long
For i = 1 To 12
If Controls("chkMonth" & Format(i, "00")).Value = True Then
months = months + 1
End If
Next i
Dim total As Long
total = members * months * family
Dim j As Long, k As Long, m As Long, n As Long
For i = 1 To members
For j = 1 To total / members
ws.Range("A" & ws.Range("A" & Rows.Count).End(xlUp).Row + 1) = CLng(txtTeamNo)
ws.Range("B" & ws.Range("B" & Rows.Count).End(xlUp).Row + 1) = members
ws.Range("C" & ws.Range("C" & Rows.Count).End(xlUp).Row + 1) = Controls("txtMemberName" & Format(i, "00")).Value
ws.Range("E" & ws.Range("E" & Rows.Count).End(xlUp).Row + 1) = family
Next j
For j = 1 To months
For m = 1 To family
If Len(Controls("txtFamilyMember" & Format(m, "00")).Text) <> vbNullString Then
ws.Range("F" & ws.Range("F" & Rows.Count).End(xlUp).Row + 1) = Controls("txtFamilyMember" & Format(m, "00")).Text
End If
Next m
Next j
For j = 1 To 12
If Controls("chkMonth" & Format(j, "00")).Value = True Then
ws.Range("D" & ws.Range("D" & Rows.Count).End(xlUp).Row + 1) = CLng(Right$(Controls("chkMonth" & Format(j, "00")).Name, 2))
ws.Range("D" & ws.Range("D" & Rows.Count).End(xlUp).Row).Resize(family, 1).Formula = ws.Range("D" & ws.Range("D" & Rows.Count).End(xlUp).Row).Formula
End If
Next j
Next i
Me.Hide
End Sub
which produced
Me.Hide hides the form instead of unloading it. Therefore the data on the form should still be accessible from the Module's code.
In your mode you load the form like this
UserForm1.Show
and then when you're done fetching data from it (again, that's in the Module no under the button's event) you unload it
Unload UserForm1