Separate Data by Year

Agates

New Member
Joined
Jul 13, 2012
Messages
29
My intention was to separate the generated data into different sheets by year, but obvioulsy I have a conflict between the IF year section and the array/loop section. I really have no idea where to go from here. Anyone have any ideas please.

The array/loop section was originally written to work with only one sheet, my logic didn't quite pan out. :(

Code:
Private Sub EnterPrint_cmd_Click()
 
Dim iRow As Long
Dim ws13 As Worksheet
Dim ws14 As Worksheet
Dim ws15 As Worksheet
Dim ws16 As Worksheet
Dim vws As Worksheet
Set ws13 = Worksheets("SampleLog 2013")
Set ws14 = Worksheets("SampleLog 2014")
Set ws15 = Worksheets("SampleLog 2015")
Set ws16 = Worksheets("SampleLog 2016")
Dim txtSamp, i, j, k
Dim ts As Date
ts = Now
Application.ScreenUpdating = False
'find year and first empty row in database
    If Year(ts) = 2013 Then
        iRow = ws13.Cells(Rows.Count, 1) _
        .End(xlUp).Offset(1, 0).Row
        Set vws = ws13
    ElseIf Year(ts) = 2014 Then
        iRow = ws14.Cells(Rows.Count, 1) _
        .End(xlUp).Offset(1, 0).Row
        Set vws = ws14
    ElseIf Year(ts) = 2015 Then
        iRow = ws15.Cells(Rows.Count, 1) _
        .End(xlUp).Offset(1, 0).Row
        Set vws = ws15
    ElseIf Year(ts) = 2016 Then
        iRow = ws16.Cells(Rows.Count, 1) _
        .End(xlUp).Offset(1, 0).Row
        Set vws = ws16
    ElseIf (Year(ts) > 2016) Then
        MsgBox "Year out of range"
    End If
        
'check for Person
    If Trim(Me.txtPerson.Value) = "" Then
        Me.txtPerson.SetFocus
        MsgBox "Please enter Person"
        Exit Sub
    End If
'check for a Sample Id
    If Trim(Me.txtSample1.Value) = "" Then
        Me.txtSample1.SetFocus
            MsgBox "Please enter a Sample Id"
            Exit Sub
    End If
'Create array for textboxes
txtSamp = Array(txtSample1, txtSample2, txtSample3, txtSample4, txtSample5, txtSample6, txtSample7, _
txtSample8, txtSample9, txtSample10, txtSample11, txtSample12, txtSample13, txtSample14)
    For i = 0 To 13
        If txtSamp(i).Value <> "" Then 'Test each tb for value
            vws.Cells(iRow + i, 1) = Left(txtSamp(i), 13) 'Post value
        End If
    
    
    Next
    For j = 0 To 13
        If vws.Cells(iRow + j, 1) <> "" Then 'Test if value entered in range col A
        vws.Cells(iRow + j, 2) = ts 'Date/time stamp
        End If
        
     Next................
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Forum statistics

Threads
1,214,983
Messages
6,122,583
Members
449,089
Latest member
Motoracer88

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