Copy data with date condition

sofas

Active Member
Joined
Sep 11, 2022
Messages
468
Office Version
  1. 2019
Platform
  1. Windows
Hello, is there a way to transfer data to a specific sheet provided the date in label1 . exists
label1 = 26/09/2022 Then
The data is copied into the September Excel sheet
And so with all the months of the year

VBA Code:
Private Sub ListBox1_Click()
Me.TextBox1.Value = Me.ListBox1.Column(0)
ListBox1.Visible = False
End Sub

Private Sub TextBox1_Change()
On Error Resume Next
If TextBox1.Value = "" Then
ListBox1.Visible = False
Else
ListBox1.Visible = True
End If
Dim ls As Integer
Dim ss As Integer
 ls = Sheet4.Range("b10000").End(xlUp).Row
 Me.ListBox1.Clear
 For ss = 5 To ls
 a = Len(Me.TextBox2.Text)
 If Left(Sheet4.Cells(ss, "b").Value, a) = Left(Me.TextBox1.Text, a) Then
 Me.ListBox1.AddItem Sheet4.Cells(ss, "b").Value
 End If
 Next ss
End Sub

Private Sub UserForm_Activate()
Label4.Caption = Format(Date, "yyyy / mm / dd")
End Sub
 

Attachments

  • Screenshot 2022-09-26 133745.png
    Screenshot 2022-09-26 133745.png
    4.7 KB · Views: 5

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
This is not exactly what you asked for but it should get you started. Assume data is in Sheet1 beginning in A3 (which can be changed in the code). A3 contains header dates and B3 contains the header data. It transfers the data into month-specific worksheets.

TestWorkbook1.xlsm
AB
3DateData
411/4/202211/4 Data
512/2/202212/2 Data
611/12/202211/12 Data
712/4/202212/4 Data
811/15/202211/5 Data
99/2/20229/2 Data
101/15/2022rgtrew
112/15/2022gbvhjy
123/15/2022wqqqw
134/15/2022ggggb
145/15/2022gfdhhjj
156/15/2022utyu
167/15/2022terte
178/15/2022dhfvm
1810/15/2022jmghj
191/22/20227rgwey
202/22/2022rfe-gj
Sheet1


VBA Code:
Sub DateDataToWorksheet()
    
'   Cell range used for looping
    Dim rCell As Range
    
'   Location for data
    Dim rAnchorCell As Range
    
'   Name of month: Jan, Feb, etc.
    Dim sMonthName As String
    
'   Range of cells containing data.
    Dim rDateCells As Range
    
'   The last row in the data.
    Dim iLastRow As Long
    
'   Number of rows offset when putting data into month-specific worksheet.
    Dim iOffsetRow As Long
    
'   Row for next data item location.
    Dim iNextDataRow As Long
    
'   Worksheet containing source data.
    Dim wsSource As Worksheet
    
'   Month-specific worksheet.
    Dim wsTarget As Worksheet
    
'   Boolean indicating whether to delete source data as it is transferred to
'   month-specific worksheet.
    Dim bDeleteSourceData As Boolean
    
'   Specify worksheet containing source data. It is Sheet1 in this example.
    Set wsSource = ThisWorkbook.Worksheets("Sheet1")
    
'   The cell containing the header for dates data.
    Set rAnchorCell = wsSource.Range("A3")
    
'   Indicate whether to delete source data after transfer to month-specific workbook.
    bDeleteSourceData = False
    
'   Last row containing source data.
    iLastRow = wsSource.Cells(1).Offset(100000).End(xlUp).Row
    
'   Range containing all dates in source data.
    Set rDateCells = rAnchorCell.Offset(1).Resize(iLastRow - rAnchorCell.Row)

'   Loop all source data rows
    For Each rCell In rDateCells

'       Will be used to identify the month-specific worksheet.
        sMonthName = Format(rCell, "mmm")
        
'       Check worksheet exists.
        Set wsTarget = Nothing
        On Error Resume Next
        Set wsTarget = Worksheets(sMonthName)
        On Error GoTo 0
        
'       Create the sheet if it does not exist.
        If wsTarget Is Nothing _
         Then
         
'           Add month-specific sheet
            Sheets.Add.Name = sMonthName
            
            Set wsTarget = Worksheets(sMonthName)
            
            wsTarget.Move After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
            
            With wsTarget
'               Add a name to the worksheet for the first cell in the range where data
'               will go into the month-specific worksheet.
                .Names.Add Name:="AnchorCell", RefersTo:="='" & wsTarget.Name & "'!" & "$A$3"
                
'               Put data headers into the target worksheet
                .Range("AnchorCell").Value = "Date"
                .Range("AnchorCell").Offset(, 1).Value = "Data"
            End With
            
        End If
        
'       Record data for month-specific worksheet.
        With wsTarget
        
'           The next available row into which to put data.
            iNextDataRow = .Range("AnchorCell").Offset(100000).End(xlUp).Row + 1

'           Offset from the header row for the next availble data row.
            iOffsetRow = iNextDataRow - .Range("AnchorCell").Row

'           Put source data into the month-specific worksheet.
            .Range("AnchorCell").Offset(iOffsetRow).Value = rCell.Value
            .Range("AnchorCell").Offset(iOffsetRow, 1).Value = rCell.Offset(, 1).Value
            
'           Autofit the two data columns so dates and data are viewable.
            .Range("AnchorCell").Offset(iOffsetRow).EntireColumn.AutoFit
            .Range("AnchorCell").Offset(iOffsetRow, 1).EntireColumn.AutoFit
        
        End With

'       Clear source data after it is transferred to month-specific worksheet.
        If bDeleteSourceData _
         Then
            rCell.Value = ""
            rCell.Offset(, 1).Value = ""
        End If

    Next rCell
    
'   Arrange month-specific worksheets chronologically: Jan, Feb, etc.
    Call ArrangeMonthWorksheets

End Sub


Sub ArrangeMonthWorksheets()

    Dim asMonths() As String
    
    ReDim asMonths(12)
    
    Dim iMonth As Long
    
    Dim wsMonth As Worksheet
    
    Dim sMonthName As String
    
    asMonths(1) = "Jan"
    asMonths(2) = "Feb"
    asMonths(3) = "Mar"
    asMonths(4) = "Apr"
    asMonths(5) = "May"
    asMonths(6) = "Jun"
    asMonths(7) = "Jul"
    asMonths(8) = "Aug"
    asMonths(9) = "Sep"
    asMonths(10) = "Oct"
    asMonths(11) = "Nov"
    asMonths(12) = "Dec"
    
    For iMonth = 1 To UBound(asMonths)
    
        sMonthName = asMonths(iMonth)
        
'       Check worksheet exists.
        Set wsMonth = Nothing
        On Error Resume Next
        Set wsMonth = Worksheets(sMonthName)
        On Error GoTo 0

        If Not wsMonth Is Nothing _
         Then
            wsMonth.Move After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
        End If
    
    Next iMonth

End Sub
 
Upvote 0
This is not exactly what you asked for but it should get you started. Assume data is in Sheet1 beginning in A3 (which can be changed in the code). A3 contains header dates and B3 contains the header data. It transfers the data into month-specific worksheets.

TestWorkbook1.xlsm
AB
3DateData
411/4/202211/4 Data
512/2/202212/2 Data
611/12/202211/12 Data
712/4/202212/4 Data
811/15/202211/5 Data
99/2/20229/2 Data
101/15/2022rgtrew
112/15/2022gbvhjy
123/15/2022wqqqw
134/15/2022ggggb
145/15/2022gfdhhjj
156/15/2022utyu
167/15/2022terte
178/15/2022dhfvm
1810/15/2022jmghj
191/22/20227rgwey
202/22/2022rfe-gj
Sheet1


VBA Code:
Sub DateDataToWorksheet()
   
'   Cell range used for looping
    Dim rCell As Range
   
'   Location for data
    Dim rAnchorCell As Range
   
'   Name of month: Jan, Feb, etc.
    Dim sMonthName As String
   
'   Range of cells containing data.
    Dim rDateCells As Range
   
'   The last row in the data.
    Dim iLastRow As Long
   
'   Number of rows offset when putting data into month-specific worksheet.
    Dim iOffsetRow As Long
   
'   Row for next data item location.
    Dim iNextDataRow As Long
   
'   Worksheet containing source data.
    Dim wsSource As Worksheet
   
'   Month-specific worksheet.
    Dim wsTarget As Worksheet
   
'   Boolean indicating whether to delete source data as it is transferred to
'   month-specific worksheet.
    Dim bDeleteSourceData As Boolean
   
'   Specify worksheet containing source data. It is Sheet1 in this example.
    Set wsSource = ThisWorkbook.Worksheets("Sheet1")
   
'   The cell containing the header for dates data.
    Set rAnchorCell = wsSource.Range("A3")
   
'   Indicate whether to delete source data after transfer to month-specific workbook.
    bDeleteSourceData = False
   
'   Last row containing source data.
    iLastRow = wsSource.Cells(1).Offset(100000).End(xlUp).Row
   
'   Range containing all dates in source data.
    Set rDateCells = rAnchorCell.Offset(1).Resize(iLastRow - rAnchorCell.Row)

'   Loop all source data rows
    For Each rCell In rDateCells

'       Will be used to identify the month-specific worksheet.
        sMonthName = Format(rCell, "mmm")
       
'       Check worksheet exists.
        Set wsTarget = Nothing
        On Error Resume Next
        Set wsTarget = Worksheets(sMonthName)
        On Error GoTo 0
       
'       Create the sheet if it does not exist.
        If wsTarget Is Nothing _
         Then
        
'           Add month-specific sheet
            Sheets.Add.Name = sMonthName
           
            Set wsTarget = Worksheets(sMonthName)
           
            wsTarget.Move After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
           
            With wsTarget
'               Add a name to the worksheet for the first cell in the range where data
'               will go into the month-specific worksheet.
                .Names.Add Name:="AnchorCell", RefersTo:="='" & wsTarget.Name & "'!" & "$A$3"
               
'               Put data headers into the target worksheet
                .Range("AnchorCell").Value = "Date"
                .Range("AnchorCell").Offset(, 1).Value = "Data"
            End With
           
        End If
       
'       Record data for month-specific worksheet.
        With wsTarget
       
'           The next available row into which to put data.
            iNextDataRow = .Range("AnchorCell").Offset(100000).End(xlUp).Row + 1

'           Offset from the header row for the next availble data row.
            iOffsetRow = iNextDataRow - .Range("AnchorCell").Row

'           Put source data into the month-specific worksheet.
            .Range("AnchorCell").Offset(iOffsetRow).Value = rCell.Value
            .Range("AnchorCell").Offset(iOffsetRow, 1).Value = rCell.Offset(, 1).Value
           
'           Autofit the two data columns so dates and data are viewable.
            .Range("AnchorCell").Offset(iOffsetRow).EntireColumn.AutoFit
            .Range("AnchorCell").Offset(iOffsetRow, 1).EntireColumn.AutoFit
       
        End With

'       Clear source data after it is transferred to month-specific worksheet.
        If bDeleteSourceData _
         Then
            rCell.Value = ""
            rCell.Offset(, 1).Value = ""
        End If

    Next rCell
   
'   Arrange month-specific worksheets chronologically: Jan, Feb, etc.
    Call ArrangeMonthWorksheets

End Sub


Sub ArrangeMonthWorksheets()

    Dim asMonths() As String
   
    ReDim asMonths(12)
   
    Dim iMonth As Long
   
    Dim wsMonth As Worksheet
   
    Dim sMonthName As String
   
    asMonths(1) = "Jan"
    asMonths(2) = "Feb"
    asMonths(3) = "Mar"
    asMonths(4) = "Apr"
    asMonths(5) = "May"
    asMonths(6) = "Jun"
    asMonths(7) = "Jul"
    asMonths(8) = "Aug"
    asMonths(9) = "Sep"
    asMonths(10) = "Oct"
    asMonths(11) = "Nov"
    asMonths(12) = "Dec"
   
    For iMonth = 1 To UBound(asMonths)
   
        sMonthName = asMonths(iMonth)
       
'       Check worksheet exists.
        Set wsMonth = Nothing
        On Error Resume Next
        Set wsMonth = Worksheets(sMonthName)
        On Error GoTo 0

        If Not wsMonth Is Nothing _
         Then
            wsMonth.Move After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
        End If
   
    Next iMonth

End Sub


Thank you very much, the code has been placed, but frankly, after several attempts, I could not complete the task. Please help

 
Upvote 0
I'm not sure how else to assist. What I provided may not be helpful to you but it works. It was meant to help you create what is needed. I cannot create your whole project. And I cannot see the file on Dropbox as I do not have an account.

To get what I sent to work put the data shown into a worksheet named Sheet1. Data starts in cell A3. The program creates month-specific worksheets that contain the data in Sheet1. After putting the data into Sheet1 cell A3 put the code into a code module then run it. The purpose for the code I provided is to show you how to create month-specific worksheets in the workbook. You'll have to adapt it for your needs.
 
Upvote 0
Upvote 0
As Fluff says, just close that box, and then click on the "Download" button.
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,412
Members
448,959
Latest member
camelliaCase

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