Write Data to Specific Row in Worksheet - VBA

Olly_S

New Member
Joined
Nov 8, 2021
Messages
8
Office Version
  1. 2021
Platform
  1. Windows
Hi all,

Having some issues with what I'm sure is a fairly easily solved issue. I am trying to create a brief form where a user is able to update three specific cells in another worksheet in my excel, based on the row defined by the user. The code as follows;

VBA Code:
Sub PO_Sent()

    Dim shPO_Approve As Worksheet '[I]this is the worksheet the user is entering their data[/I]

    Dim shPO_Database As Worksheet '[I]this is the worksheet the data should end up in[/I]

    Dim PO_Number As String '[I]This is used to identify the relevant row in the target worksheet[/I]

    Dim DocuSign_Ref As String '[I]This is the first piece of data to write to shPO_Database[/I]

    Dim PO_DateSent As String '[I]This is the second piece of data to write to shPO_Database[/I]

    Set shPO_Approve = ThisWorkbook.Sheets("PO Approval") [I]'Defining the data entry worksheet[/I]

    Set shPO_Database = ThisWorkbook.Sheets("Purchase Orders") [I]'Defining the target worksheet[/I]

    PO_Number = shPO_Approve.Range("D5")

    DocuSign_Ref = shPO_Approve.Range("D7")

    PO_DateSent = shPO_Approve.Range("D9")

    lastrow = shPO_Database.Cells(Rows.Count, 1).End(xlUp).Row [I]'This is used to find the last row of data in the target worksheet[/I]

        For i = 2 To lastrow [I]'to ignore the heading[/I]

            If ThisWorkbook.Sheets("PO Approval").Cells(i, 6).Value = PO_Number Then [I]'the corresponding data is in column 6, this tells the if where to look[/I]
      
            shPO_Database.Cells(i, 20).Value = PO_DateSent [I]'updates with the value in input worksheet, cell D9 to the target worksheet, column 20[/I]
 
            shPO_Database.Cells(i, 21).Value = DocuSign_Ref [I]'updates with the value in input worksheet, cell D7 to the target worksheet, column 21[/I]
 
            shPO_Database.Cells(i, 22).Value = Application.UserName [I]'Updates with the current user's user name in column 22[/I]

        End If
        Next

End Sub

The problem is when I run the macro, nothing appears in the columns specified in the target worksheet. Any ideas!?

Thanks,

Olly
 
Last edited by a moderator:

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Ah figured it out. The IF statement was referencing the input worksheet, not the destination worksheet with the relevant data to match. So the IF just came back negative everytime. Working now!
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,752
Members
448,989
Latest member
mariah3

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