VBA code to copy to a specific sheet

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,375
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am trying to make some way to transfer specific entries to a monthly sheet, that is not depending on the date. I want to select the row then choose a month from a combo, txtDirectMonth and type a year into txtDirectYear then press a button. I then want the row to transfer to the relevant monthly sheet. There are sheets, July 2018 to June 2019. What code could I use?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
If your combobox or textbox are ActiveX controls then put the following code in your button.


Code:
Private Sub CommandButton1_Click()
    Dim exist As Boolean
    Dim wName As String
    Dim sh As Worksheet
    Dim wRow As Double, uRow As Double
    
    Application.ScreenUpdating = False
    
    'Validations
    If txtDirectMonth.Value = "" Then
        MsgBox "Enter Month"
        txtDirectMonth.Select
        Exit Sub
    End If
    
    If txtDirectYear.Value = "" Then
        MsgBox "Enter Year"
        txtDirectYear.Select
        Exit Sub
    End If
    
    exist = False
    wName = LCase(txtDirectMonth.Value) & " " & LCase(txtDirectYear.Value)
    For Each sh In Sheets
        If LCase(sh.Name) = wName Then
            exist = True
            Exit For
        End If
    Next
    If exist = False Then
        MsgBox "The sheet : " & wName & " does not exist", vbCritical
    Else
        wRow = ActiveCell.Row
        uRow = Sheets(wName).Range("A" & Rows.Count).End(xlUp).Row + 1
        ActiveSheet.Rows(wRow).Copy
        Sheets(wName).Rows(uRow).PasteSpecial xlValue
        ActiveSheet.Rows(wRow).Delete
    End If
    
    Application.ScreenUpdating = False
        
    MsgBox "Transferred row"
End Sub

Try and tell me.
 
Upvote 0
Try and tell me.

Thank you, that is brilliant! I forgot to tell you one thing. Not every column in the row is needed as some columns are used for calculation purposes, etc. On the monthly sheet, I need these columns from the home sheet- A-J, O, AD-AF. A-J will go to columns A-J on the monthly sheet, O will go to column K and AD-AF from the home sheet will go to columns N-P on the monthly sheet. I also need formulas in columns L and M on each monthly sheet.

L = K * 0.1
M = L + K


Thanks for helping me :)
 
Upvote 0
goes the updated code

Code:
Private Sub CommandButton1_Click()
    Dim exist As Boolean
    Dim wName As String
    Dim sh As Worksheet
    Dim wRow As Double, uRow As Double
    
    Application.ScreenUpdating = False
    
    'Validations
    If txtDirectMonth.Value = "" Then
        MsgBox "Enter Month"
        txtDirectMonth.Select
        Exit Sub
    End If
    
    If txtDirectYear.Value = "" Then
        MsgBox "Enter Year"
        txtDirectYear.Select
        Exit Sub
    End If
    
    exist = False
    wName = LCase(txtDirectMonth.Value) & " " & LCase(txtDirectYear.Value)
    For Each sh In Sheets
        If LCase(sh.Name) = wName Then
            exist = True
            Exit For
        End If
    Next
    If exist = False Then
        MsgBox "The sheet : " & wName & " does not exist", vbCritical
    Else
        wRow = ActiveCell.Row
        uRow = Sheets(wName).Range("A" & Rows.Count).End(xlUp).Row + 1
        Range(Cells(wRow, "A"), Cells(wRow, "J")).Copy
        Sheets(wName).Range("A" & uRow).PasteSpecial xlValue
        
        Range(Cells(wRow, "O"), Cells(wRow, "O")).Copy
        Sheets(wName).Range("K" & uRow).PasteSpecial xlValue
        
        Range(Cells(wRow, "AD"), Cells(wRow, "AF")).Copy
        Sheets(wName).Range("N" & uRow).PasteSpecial xlValue
        
        Sheets(wName).Range("L" & uRow).Formula = "=K" & uRow & " * 0.1"
        Sheets(wName).Range("M" & uRow).Formula = "=L" & uRow & " + K" & uRow
        'ActiveSheet.Rows(wRow).Copy
        'Sheets(wName).Rows(uRow).PasteSpecial xlValue
        ActiveSheet.Rows(wRow).Delete
    End If
    
    Application.ScreenUpdating = False
        
    MsgBox "Transferred row"
End Sub
 
Upvote 0
goes the updated code

Code:
Private Sub CommandButton1_Click()
    Dim exist As Boolean
    Dim wName As String
    Dim sh As Worksheet
    Dim wRow As Double, uRow As Double
    
    Application.ScreenUpdating = False
    
    'Validations
    If txtDirectMonth.Value = "" Then
        MsgBox "Enter Month"
        txtDirectMonth.Select
        Exit Sub
    End If
    
    If txtDirectYear.Value = "" Then
        MsgBox "Enter Year"
        txtDirectYear.Select
        Exit Sub
    End If
    
    exist = False
    wName = LCase(txtDirectMonth.Value) & " " & LCase(txtDirectYear.Value)
    For Each sh In Sheets
        If LCase(sh.Name) = wName Then
            exist = True
            Exit For
        End If
    Next
    If exist = False Then
        MsgBox "The sheet : " & wName & " does not exist", vbCritical
    Else
        wRow = ActiveCell.Row
        uRow = Sheets(wName).Range("A" & Rows.Count).End(xlUp).Row + 1
        Range(Cells(wRow, "A"), Cells(wRow, "J")).Copy
        Sheets(wName).Range("A" & uRow).PasteSpecial xlValue
        
        Range(Cells(wRow, "O"), Cells(wRow, "O")).Copy
        Sheets(wName).Range("K" & uRow).PasteSpecial xlValue
        
        Range(Cells(wRow, "AD"), Cells(wRow, "AF")).Copy
        Sheets(wName).Range("N" & uRow).PasteSpecial xlValue
        
        Sheets(wName).Range("L" & uRow).Formula = "=K" & uRow & " * 0.1"
        Sheets(wName).Range("M" & uRow).Formula = "=L" & uRow & " + K" & uRow
        'ActiveSheet.Rows(wRow).Copy
        'Sheets(wName).Rows(uRow).PasteSpecial xlValue
        ActiveSheet.Rows(wRow).Delete
    End If
    
    Application.ScreenUpdating = False
        
    MsgBox "Transferred row"
End Sub

Why are two lines of code commented out near the bottom?
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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