VBA code need help

artilheirogomes

New Member
Joined
Mar 18, 2021
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Hello Excel masters

I need some assistance. Currently i have a database where i need to keep track of the times spent between tasks. My current issue with the code is:

Every time that is inserted a value into the cell "F2" on excel it should be inserted into the tab "Rack SN" (see attachments for the file).
- If is the same value, then should be placed on the next empty row the same value
- If is different value should create another table to track the tasks for another RackSN

Is someone that can help me with this?

This is the code i use at the moment but is not complete, i got stock on the filtering SN and insert into cells:

VBA Code:
Sub AddRecordToTable()

Dim ws As Worksheet
Dim newcolumn As ListColumn
Dim rownum As Integer
Dim columnnum As Integer
Dim racksn As Integer

Dim sn_tempnew As Integer
Dim sn_tempold As Integer


Set ws = ActiveSheet
Set newrow = ws.ListObjects("NewRack").ListRows.Add


'insert new table if different Rack SN


'insert full table

    With newrow

    'table
        .Range(1, 1) = "Details"
        .Range(1, 2) = "Rack SN"
        .Range(1, 3) = "Time"
        .Range(1, 4) = "Time taken"
        .Range(1, 5) = "Total time"
        .Range(2, 1) = "Start"
        .Range(3, 1) = "RackScan"
        .Range(4, 1) = "Screws"
        .Range(5, 1) = "Cabeling"
        .Range(6, 1) = "Physical damage"
        .Range(7, 1) = "Management"
    'merged cells
    '    .Range(2, 5).Merge
    '    .Range(3, 5).Merge
    '    .Range(4, 5).Merge
    '    .Range(5, 5).Merge
    '    .Range(6, 5).Merge
    '    .Range(7, 5).Merge
    '    Worksheets("Sheet1").Range("E2:E7").HorizontalAlignment = xlCenter
    '    Worksheets("Sheet1").Range("E2:E7").VerticalAlignment = xlCenter
    End With


'insert timestamp into cells

    Select Case Cells(2, 3).Value
        Case False
            Cells(2, 3).ClearContents
        Case True
            Cells(2, 3).Value = "=Timestamp(B)"
    End Select

    Select Case Cells(3, 3).Value
        Case False
            Cells(3, 3).ClearContents
        Case True
            Cells(3, 3).Value = "=Timestamp(B)"
    End Select
   
    Select Case Cells(4, 3).Value
        Case False
            Cells(4, 3).ClearContents
        Case True
            Cells(4, 3).Value = "=Timestamp(B)"
    End Select
   
    Select Case Cells(5, 3).Value
        Case False
            Cells(5, 3).ClearContents
        Case True
            Cells(5, 3).Value = "=Timestamp(B)"
    End Select

    Select Case Cells(6, 3).Value
        Case False
            Cells(6, 3).ClearContents
        Case True
            Cells(6, 3).Value = "=Timestamp(B)"
    End Select
   
    Select Case Cells(7, 3).Value
        Case False
            Cells(7, 3).ClearContents
        Case True
            Cells(7, 3).Value = "=Timestamp(B)"
    End Select
   

'insert time difference calculator

    Select Case Cells(3, 4).Value
        Case False
            Cells(3, 4).ClearContents
        Case True
            Cells(3, 4).Value = "=IFERROR(C-(C-1),"")"
    End Select
   
    Select Case Cells(4, 4).Value
        Case False
            Cells(4, 4).ClearContents
        Case True
            Cells(4, 4).Value = "=IFERROR(C-(C-1),"")"
    End Select
   
    Select Case Cells(5, 4).Value
        Case False
            Cells(5, 4).ClearContents
        Case True
            Cells(5, 4).Value = "=IFERROR(C-(C-1),"")"
    End Select

    Select Case Cells(6, 4).Value
        Case False
            Cells(6, 4).ClearContents
        Case True
            Cells(6, 4).Value = "=IFERROR(C-(C-1),"")"
    End Select
   
    Select Case Cells(7, 4).Value
        Case False
            Cells(7, 4).ClearContents
        Case True
            Cells(7, 4).Value = "=IFERROR(C-(C-1),"")"
    End Select


'Insert the total time taken for tasks


'Insert data into next empty cell
   
   
   
End Sub
 

Attachments

  • template.PNG
    template.PNG
    9.7 KB · Views: 9
Last edited by a moderator:
Ok, try this then:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lrwForNewTable As Long, SF As String, valSF As String, fnd As Range

    SF = "F2" 'Cell address of scan field
    
    If Not Intersect(Target, Range(SF)) Is Nothing And Range(SF) <> "" Then 'if scan field is changed and not blank
    
        Application.ScreenUpdating = False
        
        valSF = Range(SF)
        Range(SF) = ""
        Set fnd = Range("B:B").Find(valSF, LookAt:=xlWhole, SearchDirection:=xlPrevious)
        
        If Not fnd Is Nothing Then 'If the SN typed in is in an existing table
        
            If fnd.Offset(, -1) <> "Management" Then 'If the destination table is not fully filled
            
                fnd.Offset(1) = valSF 'Transfer scan field value
                fnd.Offset(1, 1) = Timestamp(fnd.Offset(1)) 'Input timestamp
                
            Else 'If the destination table is fully filled
            
                rwForNewTable = Cells(Rows.Count, "A").End(xlUp).Row + 2 'Get row for new table
                Range("A1").Resize(7, 5).Copy Cells(rwForNewTable, "A") 'Insert new table
                Cells(rwForNewTable, "A").Offset(1, 1).Resize(6, 2).ClearContents 'Clear data in the new table
                Cells(rwForNewTable, "A").Offset(1, 1) = valSF 'Transfer scan field value
                Cells(rwForNewTable, "A").Offset(1, 2) = Timestamp(Cells(rwForNewTable, "A").Offset(1, 1)) 'Input timestamp
                
            End If
            
        Else 'If the SN typed in is not in the existing tables
        
            rwForNewTable = Cells(Rows.Count, "A").End(xlUp).Row + 2 'Get row for new table
            Range("A1").Resize(7, 5).Copy Cells(rwForNewTable, "A") 'Insert new table
            Cells(rwForNewTable, "A").Offset(1, 1).Resize(6, 2).ClearContents 'Clear data in the new table
            Cells(rwForNewTable, "A").Offset(1, 1) = valSF 'Transfer scan field value
            Cells(rwForNewTable, "A").Offset(1, 2) = Timestamp(Cells(rwForNewTable, "A").Offset(1, 1)) 'Input timestamp
            
        End If
        
        Application.ScreenUpdating = True
        
    End If
End Sub
Note: I presuppose you use the following formulas:
1165384 Create new table based on cell value.xlsm
ABCDEF
1DetailsRack SNTimeTime takenTotal timeScan field
2Start 00:00:00
3RackScan 
4Screws 
5Cabeling 
6Physical Damage 
7Management 
Sheet1
Cell Formulas
RangeFormula
D2D2=IF(C2="","","00:00:00")
E2E2=IFERROR(TEXT(LOOKUP(2,1/(C2:C7<>""),C2:C7)-C2,"hh:mm:ss"),"00:00:00")
D3:D7D3=IF(C3<>"",TEXT(C3-C2,"hh:mm:ss"),"")
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hello,

Yes, if the 3 is scanned needs to be inserted in the new table, if 2 is scanned then just adds on the correspondent table from the 2 on the latest empty line
And the 3 for the 3, an so forward. The idea is having just 1 file limited to the insertion of the only cell where should be scanned the SN from different racks and as the tests while the tests are completed it autopopulates the cells with the validation from the SN is only needed to fill in the information. If there is 7 racks for example, the serial numbers are scanned and starts to build the tables and randomly as the tests are finished the worker only scan the SN as indication that the test is finished, that is the reason we use only the SN to validate and add to the last line of each table or new table if different SN.
 
Upvote 0
Ok, try this then:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lrwForNewTable As Long, SF As String, valSF As String, fnd As Range

    SF = "F2" 'Cell address of scan field
   
    If Not Intersect(Target, Range(SF)) Is Nothing And Range(SF) <> "" Then 'if scan field is changed and not blank
   
        Application.ScreenUpdating = False
       
        valSF = Range(SF)
        Range(SF) = ""
        Set fnd = Range("B:B").Find(valSF, LookAt:=xlWhole, SearchDirection:=xlPrevious)
       
        If Not fnd Is Nothing Then 'If the SN typed in is in an existing table
       
            If fnd.Offset(, -1) <> "Management" Then 'If the destination table is not fully filled
           
                fnd.Offset(1) = valSF 'Transfer scan field value
                fnd.Offset(1, 1) = Timestamp(fnd.Offset(1)) 'Input timestamp
               
            Else 'If the destination table is fully filled
           
                rwForNewTable = Cells(Rows.Count, "A").End(xlUp).Row + 2 'Get row for new table
                Range("A1").Resize(7, 5).Copy Cells(rwForNewTable, "A") 'Insert new table
                Cells(rwForNewTable, "A").Offset(1, 1).Resize(6, 2).ClearContents 'Clear data in the new table
                Cells(rwForNewTable, "A").Offset(1, 1) = valSF 'Transfer scan field value
                Cells(rwForNewTable, "A").Offset(1, 2) = Timestamp(Cells(rwForNewTable, "A").Offset(1, 1)) 'Input timestamp
               
            End If
           
        Else 'If the SN typed in is not in the existing tables
       
            rwForNewTable = Cells(Rows.Count, "A").End(xlUp).Row + 2 'Get row for new table
            Range("A1").Resize(7, 5).Copy Cells(rwForNewTable, "A") 'Insert new table
            Cells(rwForNewTable, "A").Offset(1, 1).Resize(6, 2).ClearContents 'Clear data in the new table
            Cells(rwForNewTable, "A").Offset(1, 1) = valSF 'Transfer scan field value
            Cells(rwForNewTable, "A").Offset(1, 2) = Timestamp(Cells(rwForNewTable, "A").Offset(1, 1)) 'Input timestamp
           
        End If
       
        Application.ScreenUpdating = True
       
    End If
End Sub
Note: I presuppose you use the following formulas:
1165384 Create new table based on cell value.xlsm
ABCDEF
1DetailsRack SNTimeTime takenTotal timeScan field
2Start 00:00:00
3RackScan 
4Screws 
5Cabeling 
6Physical Damage 
7Management 
Sheet1
Cell Formulas
RangeFormula
D2D2=IF(C2="","","00:00:00")
E2E2=IFERROR(TEXT(LOOKUP(2,1/(C2:C7<>""),C2:C7)-C2,"hh:mm:ss"),"00:00:00")
D3:D7D3=IF(C3<>"",TEXT(C3-C2,"hh:mm:ss"),"")
Is working but the SN still dont follow to the new table.
 
Upvote 0
I just have one last question, is it possible to have static scan field, for example, when inserted the SN on the scan field cell, always returns to the F2?
Do you mean you want to keep the value typed into F2 even after it's transferred onto the table?
 
Upvote 0
Do you mean you want to keep the value typed into F2 even after it's transferred onto the table?
No need, just instead of going the next cell it gets automatically the position ready on F2 for scanning instead of navigating again to F2 to scan the SN
 
Upvote 0
No need, just instead of going the next cell it gets automatically the position ready on F2 for scanning instead of navigating again to F2 to scan the SN
Oh right, it's funny I didn't understand your demand correctly.
That's pretty easy. Give me a moment, I'll modify the code.
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,849
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top