VBA code to create command button and copy criteria based data from one worksheet to another

MMessenger98

New Member
Joined
Feb 27, 2024
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I could be approaching this in the complete wrong way so apologies if that is the case.

I have a basic form that I want to use to track quality assurance. The process is included in Col C, the score in Col D and then I have a Data validation list in in cell H4 (Date) and Cell H5 (Agent). Ideally I want to be able to select the date in mmm:yy format and the agent name, fill out the form and then use a command button to submit those results to another worksheet within the same workbook and paste them in the relevant cell based on the criteria provided.

As an example in relation to the images provided If I have chosen Jan-24 from date list and Matt Smith as agent and then keyed 10% in D4, 10% in D5 and then submit I'd like the data to be pasted on Data sheet in cells C2 and C3.

Looking for a VBA code that would achieve this.

EDIT:
Thank you in advance for any help with the VBA code or alternative solutions for how I should be setting up the worksheets to achieve this.
 

Attachments

  • Form 1.JPG
    Form 1.JPG
    38.8 KB · Views: 13
  • Date Sheet.JPG
    Date Sheet.JPG
    112.2 KB · Views: 13
Last edited by a moderator:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Do you mean something like this?
Can't see the row and column addresses in the pictures you posted, so you have to change them to the correct ones.

VBA Code:
Sub TS_DataTrans()
Dim wsForm As Worksheet: Set wsForm = Worksheets(1) ' WorkSheet with Form ***** Change if necessary
Dim wsData As Worksheet: Set wsData = Worksheets(2) ' Worksheet with Data ***** Change if necessary
Dim DateRNG As Range, AgentRNG As Range, FormDataRNG As Range
Set DateRNG = wsForm.Range("H10")   ' Date to search ***** Change if necessary
Set AgentRNG = wsForm.Range("H11")  ' Agent to search ***** Change if necessary
Set FormDataRNG = wsForm.Range("d10:d13") ' Cells to copy from Form ***** Change if necessary

Dim TmpRNG As Range

Set TmpRNG = wsData.Rows(1).Cells.Find(CDate(CStr(DateRNG.Value)), LookAt:=xlWhole, LookIn:=xlFormulas) ' Find Column (Date)
If TmpRNG Is Nothing Then
    MsgBox "Date was not found.": Exit Sub
Else
    Dim CurrentDateCol As Integer: CurrentDateCol = TmpRNG.Column ' Get Column No.
End If

Set TmpRNG = wsData.Columns(1).Cells.Find(AgentRNG.Value, LookIn:=xlValues) ' Find Row (Agent)
If TmpRNG Is Nothing Then
    MsgBox "Date was not found.": Exit Sub
Else
    Dim CurrentAgentRow As Long: CurrentAgentRow = TmpRNG.Row ' Get Row No.
End If

wsData.Cells(CurrentAgentRow, CurrentDateCol).Resize(4, 1).Value = FormDataRNG.Value ' Copy Data from Form Sheet to Data Sheet

End Sub

My apologies for any quirks, English is not my native language. "So I blame Google translate for all my goofs." :devilish:
 
Upvote 0
Hi, yes I assume the above will work but maybe I'm just missing some bits. At the moment I click the control box to submit but nothing happens. Adjusted code below

Sub TS_DataTrans()
Dim wsForm As Worksheet: Set wsForm = Worksheets("QC Form") ' WorkSheet with QC Form ***** Change if necessary
Dim wsData As Worksheet: Set wsData = Worksheets("Agent Level Data") ' Worksheet with Agent Level Data ***** Change if necessary
Dim DateRNG As Range, AgentRNG As Range, FormDataRNG As Range
Set DateRNG = wsForm.Range("H4") ' Date to search ***** Change if necessary
Set AgentRNG = wsForm.Range("H5") ' Agent to search ***** Change if necessary
Set FormDataRNG = wsForm.Range("d4:d38") ' Cells to copy from QC Form ***** Change if necessary
Dim TmpRNG As Range
Set TmpRNG = wsData.Rows(1).Cells.Find(CDate(CStr(DateRNG.Value)), LookAt:=xlWhole, LookIn:=xlFormulas) ' Find Column (Date)
If TmpRNG Is Nothing Then
MsgBox "Date was not found.": Exit Sub
Else
Dim CurrentDateCol As Integer: CurrentDateCol = TmpRNG.Column ' Get Column No.
End If
Set TmpRNG = wsData.Columns(1).Cells.Find(AgentRNG.Value, LookIn:=xlValues) ' Find Row (Agent)
If TmpRNG Is Nothing Then
MsgBox "Date was not found.": Exit Sub
Else
Dim CurrentAgentRow As Long: CurrentAgentRow = TmpRNG.Row ' Get Row No.
End If
wsData.Cells(CurrentAgentRow, CurrentDateCol).Resize(4, 1).Value = FormDataRNG.Value ' Copy Data from Form Sheet to Data Sheet
End Sub

Is there more lines I need to amend maybe?

Thank you
 
Upvote 0
Have you attached the TS_DataTrans macro to that button?

Right click button -> select Assign macro -> select TS_DataTrans from the list
 
Upvote 0
Not sure if the additional images below would help based on the real scenario rather then the example one I used previously. My data sheet goes from A1 to Z1541. You should be able to see the row and Col for the QC form.

I've added the control box visual code.

When I click right on the 'submit' button which is the control box it doesn't do anything. I tried to click on design mode and then right click on the submit button but the option to assign macro doesn't come up. If I try to run the code manually though it does ask me what to run it against and I've chosen TS_DataTrans from the list but it still doesn't seem to run.
 

Attachments

  • QC Form.JPG
    QC Form.JPG
    83.1 KB · Views: 6
  • Control Button.JPG
    Control Button.JPG
    21.1 KB · Views: 6
  • Data Sheet.JPG
    Data Sheet.JPG
    217.6 KB · Views: 6
Upvote 0
Fixed ranges.
Does this work in principle?

VBA Code:
Sub TS_DataTrans()
Dim wsForm As Worksheet: Set wsForm = Worksheets("QC Form") ' WorkSheet with Form ***** Change if necessary
Dim wsData As Worksheet: Set wsData = Worksheets("Agent Level Data") ' Worksheet with Data ***** Change if necessary
Dim DateRNG As Range, AgentRNG As Range, FormDataRNG As Range
Set DateRNG = wsForm.Range("H4")   ' Date to search ***** Change if necessary
Set AgentRNG = wsForm.Range("H5")  ' Agent to search ***** Change if necessary
Set FormDataRNG = wsForm.Range("d4:d38") ' Cells to copy from Form ***** Change if necessary

Dim TmpRNG As Range

Set TmpRNG = wsData.Rows(1).Cells.Find(CDate(CStr(DateRNG.Value)), LookAt:=xlWhole, LookIn:=xlFormulas) ' Find Column (Date)
If TmpRNG Is Nothing Then
    MsgBox "Date was not found.": Exit Sub
Else
    Dim CurrentDateCol As Integer: CurrentDateCol = TmpRNG.Column ' Get Column No.
End If

Set TmpRNG = wsData.Columns(1).Cells.Find(AgentRNG.Value, LookIn:=xlValues) ' Find Row (Agent)
If TmpRNG Is Nothing Then
    MsgBox "Date was not found.": Exit Sub
Else
    Dim CurrentAgentRow As Long: CurrentAgentRow = TmpRNG.Row ' Get Row No.
End If

wsData.Cells(CurrentAgentRow, CurrentDateCol).Resize(35, 1).Value = FormDataRNG.Value                                                                ' Copy Data from Form Sheet to Data Sheet

End Sub

My apologies for any quirks, English is not my native language. "So I blame Google translate for all my goofs." :devilish:
 
Upvote 0
The code uses Excel's built-in (range.find) function, but it really doesn't like dates.

Check that:
Worksheets("QC Form").range("H4")
and
Worksheets("Agent Level Data")
use exactly the same formatting.

(What formatting is defined for them?)
 
Upvote 0
I did wonder this whilst I was sending the previous message.

The QC Form data is from a data validation list and the format is (MMM:YY) so Jan-24 but the actual date keyed is 01/01/24 whereas on agent level data it appears the format is simply Jan-24 and has been keyed that way rather then 01/01/24. Both however are the same format.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,952
Members
449,095
Latest member
nmaske

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