Overwriting data using VBA

kidwispa

Active Member
Joined
Mar 7, 2011
Messages
330
Hi all,

I have created the below userform in order for my manager to be able to make changes to an Agent's results by quarter...

UserForm4.jpg


The textboxes under the heading "Current Record" are poplulated when both Agent AND Quarter have been selected from the comboboxes above and the "Retrieve" button has been clicked. I have used the following code to achieve this (based on other code people have helped me with on here)

Code:
Private Sub CommandButton5_Click()
Dim criteria1 As String
Dim criteria2 As String
Dim rng As Range
Dim result
criteria1 = ComboBox1
criteria2 = ComboBox2
If Trim(criteria1) <> "" And Trim(criteria2) <> "" Then
result = Application.Match(criteria1, Worksheets("QtrData").Range("B:B"), 0)
If Not IsError(result) Then
With Worksheets("QtrData")
If criteria2 = "1" Then
TextBox30.Text = .Range("G:G")(result).Text
TextBox28.Text = .Range("K:K")(result).Text
TextBox26.Text = .Range("P:P")(result).Text
TextBox24.Text = .Range("T:T")(result).Text
TextBox18.Text = .Range("Z:Z")(result).Text
Else
If criteria2 = "2" Then
TextBox30.Text = .Range("AF:AF")(result).Text
TextBox28.Text = .Range("AJ:AJ")(result).Text
TextBox26.Text = .Range("AO:AO")(result).Text
TextBox24.Text = .Range("AS:AS")(result).Text
TextBox18.Text = .Range("AY:AY")(result).Text
Else
If criteria2 = "3" Then
TextBox30.Text = .Range("BE:BE")(result).Text
TextBox28.Text = .Range("BI:BI")(result).Text
TextBox26.Text = .Range("BN:BN")(result).Text
TextBox24.Text = .Range("BR:BR")(result).Text
TextBox18.Text = .Range("BX:BX")(result).Text
Else
If criteria2 = "4" Then
TextBox30.Text = .Range("CD:CD")(result).Text
TextBox28.Text = .Range("CH:CH")(result).Text
TextBox26.Text = .Range("CM:CM")(result).Text
TextBox24.Text = .Range("CQ:CQ")(result).Text
TextBox18.Text = .Range("CW:CW")(result).Text
 
 
End If
End If
End If
End If
End With
End If
Else
MsgBox "Please select Agent Name AND Quarter"
End If
End Sub

What I want to achieve is when my manager inputs new results in the textboxes under the heading "Amendment", and clicks the "CHANGE" button, the new results are input onto the sheet entitled "QtrData" in the correct columns against the correct agent (as shown in the code above).

There will be instances where my manager may not want to change the results for all of the entries, so where the corresponding textbox is left blank I want the result to stay the same as it is.

Can anyone help me with this?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Something like this should get you started [code for click event of 'CHANGE' button]:

Code:
With Worksheets("QtrData")
    If Criteria2 = "1" Then
        If Len(TextBox30.Text) > 0 Then .Range("G:G")(result).Text = TextBox30.Text
        If Len(TextBox28.Text) > 0 Then .Range("K:K")(result).Text = TextBox28.Text
        If Len(TextBox26.Text) > 0 Then .Range("P:P")(result).Text = TextBox26.Text
        If Len(TextBox24.Text) > 0 Then .Range("T:T")(result).Text = TextBox24.Text
        If Len(TextBox18.Text) > 0 Then .Range("Z:Z")(result).Text = TextBox18.Text
 
Upvote 0
Something like this should get you started [code for click event of 'CHANGE' button]:

Code:
With Worksheets("QtrData")
    If Criteria2 = "1" Then
        If Len(TextBox30.Text) > 0 Then .Range("G:G")(result).Text = TextBox30.Text
        If Len(TextBox28.Text) > 0 Then .Range("K:K")(result).Text = TextBox28.Text
        If Len(TextBox26.Text) > 0 Then .Range("P:P")(result).Text = TextBox26.Text
        If Len(TextBox24.Text) > 0 Then .Range("T:T")(result).Text = TextBox24.Text
        If Len(TextBox18.Text) > 0 Then .Range("Z:Z")(result).Text = TextBox18.Text

Thanks for your reply DB, however when I use the following code it doesn't make the necessary changes on the "QtrData" sheet... can you see what I've done wrong?

Code:
Private Sub CommandButton11_Click()
Dim criteria1 As String
Dim criteria2 As String
Dim result
criteria1 = ComboBox1
criteria2 = ComboBox2
If Trim(criteria1) <> "" And Trim(criteria2) <> "" Then
With Worksheets("QtrData")
    If criteria2 = "1" Then
        If Len(TextBox30.Text) > 0 Then .Range("G:G")(result).Text = TextBox30.Text
        If Len(TextBox28.Text) > 0 Then .Range("K:K")(result).Text = TextBox28.Text
        If Len(TextBox26.Text) > 0 Then .Range("P:P")(result).Text = TextBox26.Text
        If Len(TextBox24.Text) > 0 Then .Range("T:T")(result).Text = TextBox24.Text
        If Len(TextBox18.Text) > 0 Then .Range("Z:Z")(result).Text = TextBox18.Text
    If criteria2 = "2" Then
        If Len(TextBox30.Text) > 0 Then .Range("AF:AF")(result).Text = TextBox30.Text
        If Len(TextBox28.Text) > 0 Then .Range("AJ:AJ")(result).Text = TextBox28.Text
        If Len(TextBox26.Text) > 0 Then .Range("AO:AO")(result).Text = TextBox26.Text
        If Len(TextBox24.Text) > 0 Then .Range("AS:AS")(result).Text = TextBox24.Text
        If Len(TextBox18.Text) > 0 Then .Range("AY:AY")(result).Text = TextBox18.Text
    If criteria2 = "3" Then
        If Len(TextBox30.Text) > 0 Then .Range("BE:BE")(result).Text = TextBox30.Text
        If Len(TextBox28.Text) > 0 Then .Range("BI:BI")(result).Text = TextBox28.Text
        If Len(TextBox26.Text) > 0 Then .Range("BN:BN")(result).Text = TextBox26.Text
        If Len(TextBox24.Text) > 0 Then .Range("BR:BR")(result).Text = TextBox24.Text
        If Len(TextBox18.Text) > 0 Then .Range("BX:BX")(result).Text = TextBox18.Text
    If criteria2 = "4" Then
        If Len(TextBox30.Text) > 0 Then .Range("CD:CD")(result).Text = TextBox30.Text
        If Len(TextBox28.Text) > 0 Then .Range("CH:CH")(result).Text = TextBox28.Text
        If Len(TextBox26.Text) > 0 Then .Range("CM:CM")(result).Text = TextBox26.Text
        If Len(TextBox24.Text) > 0 Then .Range("CQ:CQ")(result).Text = TextBox24.Text
        If Len(TextBox18.Text) > 0 Then .Range("CW:CW")(result).Text = TextBox18.Text
End If
End If
End If
End If
End With
End If
MsgBox "Changes Made"
End
End Sub

:)
 
Upvote 0
The Text property of a range is read-only. You set the Value property.

Code:
If Len(TextBox30.Text) Then .Cells(result, "G").value = TextBox30.Text
 
Upvote 0
The Text property of a range is read-only. You set the Value property.

Code:
If Len(TextBox30.Text) Then .Cells(result, "G").value = TextBox30.Text

Thanks Shq, although I've tried making the advised changes and when the command button is pressed, the changes aren't made to the correct cells...

I haven't had any error messages so i can't work out what is wrong... any ideas? :(
 
Upvote 0
Try changing the number of the TextBox on the right side of the equal sign in the code I gave you. It should refer to the TextBox containing the value you want to change to.

For example:

If Len(TextBox30.Text) > 0 Then .Range("G:G")(result).Text = TextBox50.Text

We have no idea what name those text boxes have.
 
Last edited:
Upvote 0
hi all,

Thanks for your suggestions. Here is the code I currently have...

Code:
Private Sub CommandButton11_Click()
Dim criteria1 As String
Dim criteria2 As String
Dim result
criteria1 = ComboBox1
criteria2 = ComboBox2
result = Application.Match(criteria1, Worksheets("QtrData").Range("B:B"), 0)
If Trim(criteria1) <> "" And Trim(criteria2) <> "" Then
With Worksheets("QtrData")
    If criteria2 = "1" Then
        If Len(TextBox30.Text) > 0 Then .Cells(result, "G").Value = TextBox29.Text
        If Len(TextBox28.Text) > 0 Then .Cells(result, "K").Value = TextBox27.Text
        If Len(TextBox26.Text) > 0 Then .Cells(result, "P").Value = TextBox25.Text
        If Len(TextBox24.Text) > 0 Then .Cells(result, "T").Value = TextBox20.Text
        If Len(TextBox18.Text) > 0 Then .Cells(result, "Z").Value = TextBox19.Text
    Else
    If criteria2 = "2" Then
        If Len(TextBox30.Text) > 0 Then .Cells(result, "AF").Value = TextBox29.Text
        If Len(TextBox28.Text) > 0 Then .Cells(result, "AJ").Value = TextBox27.Text
        If Len(TextBox26.Text) > 0 Then .Cells(result, "AO").Value = TextBox25.Text
        If Len(TextBox24.Text) > 0 Then .Cells(result, "AS").Value = TextBox20.Text
        If Len(TextBox18.Text) > 0 Then .Cells(result, "AY").Value = TextBox19.Text
    Else
    If criteria2 = "3" Then
        If Len(TextBox30.Text) > 0 Then .Cells(result, "BE").Value = TextBox29.Text
        If Len(TextBox28.Text) > 0 Then .Cells(result, "BI").Value = TextBox27.Text
        If Len(TextBox26.Text) > 0 Then .Cells(result, "BN").Value = TextBox25.Text
        If Len(TextBox24.Text) > 0 Then .Cells(result, "BR").Value = TextBox20.Text
        If Len(TextBox18.Text) > 0 Then .Cells(result, "BX").Value = TextBox19.Text
    Else
    If criteria2 = "4" Then
        If Len(TextBox30.Text) > 0 Then .Cells(result, "CD").Value = TextBox29.Text
        If Len(TextBox28.Text) > 0 Then .Cells(result, "CH").Value = TextBox27.Text
        If Len(TextBox26.Text) > 0 Then .Cells(result, "CM").Value = TextBox25.Text
        If Len(TextBox24.Text) > 0 Then .Cells(result, "CQ").Value = TextBox20.Text
        If Len(TextBox18.Text) > 0 Then .Cells(result, "CW").Value = TextBox19.Text
End If
End If
End If
End If
End With
End If
MsgBox "Changes Made"
End
End Sub

Currently the code above changes the data from the current record to the amended where the original file is greater than zero. What would I need to change to ensure all conditions below are met?

-If the current record is empty and the amended is complete, then use the amended;
-If the current record is populated and amended is complete, then use new;
-If the current record is populated but the amended isn't, then keep current record as it is.

???

Thanks again for all your help

:)
 
Upvote 0
Have worked it out so thought I'd post... :)

Code:
Private Sub CommandButton11_Click()
Dim criteria1 As String
Dim criteria2 As String
Dim result
criteria1 = ComboBox1
criteria2 = ComboBox2
result = Application.Match(criteria1, Worksheets("QtrData").Range("B:B"), 0)
If Trim(criteria1) <> "" And Trim(criteria2) <> "" Then
With Worksheets("QtrData")
    If criteria2 = "1" Then
        If Len(TextBox30.Text) >= 0 And Len(TextBox29.Text) > 0 Then .Cells(result, "G").Value = TextBox29.Text
        If Len(TextBox28.Text) >= 0 And Len(TextBox27.Text) > 0 Then .Cells(result, "K").Value = TextBox27.Text
        If Len(TextBox26.Text) >= 0 And Len(TextBox25.Text) > 0 Then .Cells(result, "P").Value = TextBox25.Text
        If Len(TextBox24.Text) >= 0 And Len(TextBox20.Text) > 0 Then .Cells(result, "T").Value = TextBox20.Text
        If Len(TextBox18.Text) >= 0 And Len(TextBox19.Text) > 0 Then .Cells(result, "Z").Value = TextBox19.Text
    Else
    If criteria2 = "2" Then
        If Len(TextBox30.Text) >= 0 And Len(TextBox29.Text) > 0 Then .Cells(result, "AF").Value = TextBox29.Text
        If Len(TextBox28.Text) >= 0 And Len(TextBox27.Text) > 0 Then .Cells(result, "AJ").Value = TextBox27.Text
        If Len(TextBox26.Text) >= 0 And Len(TextBox25.Text) > 0 Then .Cells(result, "AO").Value = TextBox25.Text
        If Len(TextBox24.Text) >= 0 And Len(TextBox20.Text) > 0 Then .Cells(result, "AS").Value = TextBox20.Text
        If Len(TextBox18.Text) >= 0 And Len(TextBox19.Text) > 0 Then .Cells(result, "AY").Value = TextBox19.Text
    Else
    If criteria2 = "3" Then
        If Len(TextBox30.Text) >= 0 And Len(TextBox29.Text) > 0 Then .Cells(result, "BE").Value = TextBox29.Text
        If Len(TextBox28.Text) >= 0 And Len(TextBox27.Text) > 0 Then .Cells(result, "BI").Value = TextBox27.Text
        If Len(TextBox26.Text) >= 0 And Len(TextBox25.Text) > 0 Then .Cells(result, "BN").Value = TextBox25.Text
        If Len(TextBox24.Text) >= 0 And Len(TextBox20.Text) > 0 Then .Cells(result, "BR").Value = TextBox20.Text
        If Len(TextBox18.Text) >= 0 And Len(TextBox19.Text) > 0 Then .Cells(result, "BX").Value = TextBox19.Text
    Else
    If criteria2 = "4" Then
        If Len(TextBox30.Text) >= 0 And Len(TextBox29.Text) > 0 Then .Cells(result, "CD").Value = TextBox29.Text
        If Len(TextBox28.Text) >= 0 And Len(TextBox27.Text) > 0 Then .Cells(result, "CH").Value = TextBox27.Text
        If Len(TextBox26.Text) >= 0 And Len(TextBox25.Text) > 0 Then .Cells(result, "CM").Value = TextBox25.Text
        If Len(TextBox24.Text) >= 0 And Len(TextBox20.Text) > 0 Then .Cells(result, "CQ").Value = TextBox20.Text
        If Len(TextBox18.Text) >= 0 And Len(TextBox19.Text) > 0 Then .Cells(result, "CW").Value = TextBox19.Text
End If
End If
End If
End If
End With
End If
MsgBox "Changes Made"
End
End Sub

:)
 
Upvote 0

Forum statistics

Threads
1,224,525
Messages
6,179,319
Members
452,905
Latest member
deadwings

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