The following code is not working as expected. I does not populate the companies sheet…
- User
The following code is not working as expected. I does not populate the companies sheet with values from Sheet Providers. Function getCompanyName() As String Dim providersSheet As Worksheet Set providersSheet = ThisWorkbook.Sheets("Providers") Dim activeRow As Range Set activeRow = providersSheet.Range(activeCellAddress).EntireRow getCompanyName = activeRow.Cells(1, 3).Value End Function
Private Sub Worksheet_Open(ByVal Target As Range) Call populateCompanyData End Sub
Sub populateCompanyData() Dim companySheet As Worksheet Set companySheet = ActiveWorkbook.ActiveSheet Dim lastRow As Long lastRow = companySheet.Cells(Rows.Count, 1).End(xlUp).Row Dim emptyRow As Long emptyRow = lastRow + 1 companySheet.Cells(emptyRow, 1).Value = getCompanyName() companySheet.Cells(emptyRow, 4).Value = ThisWorkbook.Sheets("Providers").Range(activeCellAddress).Offset(0, 1).Value End Sub
- Assistant
The issue with this code is that the "activeCellAddress" variable is not defined, and so it is causing an error. To fix this, we can either define "activeCellAddress" or use a different approach to retrieve the necessary values.
One possible solution is to modify the "populateCompanyData" sub to pass the active cell as an argument to the "getCompanyName" function:
Sub populateCompanyData() Dim companySheet As Worksheet Set companySheet = ActiveWorkbook.ActiveSheet Dim lastRow As Long lastRow = companySheet.Cells(Rows.Count, 1).End(xlUp).Row Dim emptyRow As Long emptyRow = lastRow + 1 companySheet.Cells(emptyRow, 1).Value = getCompanyName(ActiveCell) companySheet.Cells(emptyRow, 4).Value = ThisWorkbook.Sheets("Providers").Range(ActiveCell.Address).Offset(0, 1).Value End Sub
Then, we can modify the "getCompanyName" function to accept the active cell as an argument and use it to retrieve the necessary values:
Function getCompanyName(ByVal activeCell As Range) As String Dim providersSheet As Worksheet Set providersSheet = ThisWorkbook.Sheets("Providers") Dim activeRow As Range Set activeRow = activeCell.EntireRow getCompanyName = activeRow.Cells(1, 3).Value End Function
- User
How can I get the original code to work by defining the “activeCellAddress” variable
- Assistant
To define the "activeCellAddress" variable, you can use the "Application.Caller" property to retrieve the address of the cell that called the "populateCompanyData" sub. Here's how you can modify the original code to include the "activeCellAddress" variable:
Function getCompanyName() As String Dim providersSheet As Worksheet Set providersSheet = ThisWorkbook.Sheets("Providers") Dim activeRow As Range Set activeRow = providersSheet.Range(activeCellAddress).EntireRow getCompanyName = activeRow.Cells(1, 3).Value End Function
Sub populateCompanyData() Dim companySheet As Worksheet Set companySheet = ActiveWorkbook.ActiveSheet Dim lastRow As Long lastRow = companySheet.Cells(Rows.Count, 1).End(xlUp).Row Dim emptyRow As Long emptyRow = lastRow + 1 Dim activeCellAddress As String ' define variable here activeCellAddress = Application.Caller.Address ' retrieve address of calling cell companySheet.Cells(emptyRow, 1).Value = getCompanyName() companySheet.Cells(emptyRow, 4).Value = ThisWorkbook.Sheets("Providers").Range(activeCellAddress).Offset(0, 1).Value End Sub