I want to capture the cell letter of the cell a button is sitting on.
I find that I have to click the cell first BEFORE I then click the button to get then get the cell letter.
location = ActiveCell.Address
Is there away to get the 'cell letter' of where the button sits when just (only) clicking the button (regardless of the active cell)?
Sub Button_Click()
Dim location As String
Dim cellLetter As String
Dim CalculateTaxes As Workbook
Dim incomeCell As Range
Dim fedTaxCell As Range
Dim stateTaxCell As Range
Dim medicareCell As Range
Dim ssCell As Range
' Get the location of where the button is that was clicked.
' BUT THIS IS NOT WHAT I WANT AS IT TAKES THE ACTIVE CELL WHICH MIGHT NOT BE THE CELL OF THE BUTTON I CLICK.
location = ActiveCell.Address
' Get the cell letter from the location.
cellLetter = Mid(location, 2, 1)
' Open the child "2023 - Calculate taxes" workbook.
Set CalculateTaxes = Workbooks.Open("C:\Dans\Personal\Sergio\Business\Income and expense\2023\2023 - Calculate taxes.xlsm")
' Get references to the cells in the child "2023 - Calculate taxes" workbook.
' Get the values from the cells.
Set incomeCell = CalculateTaxes.Worksheets("CalcTaxes").Range("G5")
Set fedTaxCell = CalculateTaxes.Worksheets("CalcTaxes").Range("B30")
Set stateTaxCell = CalculateTaxes.Worksheets("CalcTaxes").Range("B31")
Set ssCell = CalculateTaxes.Worksheets("CalcTaxes").Range("B32")
Set medicareCell = CalculateTaxes.Worksheets("CalcTaxes").Range("B33")
' Set the child's "Total Income" cell's amount to the parent's value in the parent workbook's sheet.
incomeCell.Value = ThisWorkbook.Worksheets("ForIrsIncomeAndExpenses").Range(cellLetter & "12").Value
' Get the calculated values from the child "2023 - Calculate taxes" workbook and set the values in the parent workbook's sheet.
ThisWorkbook.Worksheets("ForIrsIncomeAndExpenses").Range(cellLetter & "17").Value = fedTaxCell.Value
ThisWorkbook.Worksheets("ForIrsIncomeAndExpenses").Range(cellLetter & "18").Value = stateTaxCell.Value
ThisWorkbook.Worksheets("ForIrsIncomeAndExpenses").Range(cellLetter & "19").Value = ssCell.Value
ThisWorkbook.Worksheets("ForIrsIncomeAndExpenses").Range(cellLetter & "20").Value = medicareCell.Value
' Force the child "2023 - Calculate taxes" to not save any changes and not prompt for it.
CalculateTaxes.Saved = True
' Close the child "2023 - Calculate taxes" workbook.
CalculateTaxes.Close
End Sub
Tuhin PaulPosted Apr 16, 2023, 7:12 AM
You can modify the code :
Tuhin PaulPosted Apr 16, 2023, 7:10 AM
I think there is no direct way to get the cell letter of the cell a button is sitting on without first selecting the cell. You can try a workaround where you use the TopLeftCell property of the button to get the top-left cell of the button's bounding box, and then extract the column letter from the address of that cell.
First LastPosted Apr 15, 2023, 3:54 PM
I have multiple buttons and I am trying to not replicate basically the same code amongst all.
So I am using 1 method and use the 'column cell letter' of each button to get what I need to calcualte the taxes and place the values into the corresponding tax column.
First LastPosted Apr 15, 2023, 3:48 PM
I was able to capture the 'column cell letter' of the cell the button is sitting on even when another cell has reference.
I found the solution here: https://stackoverflow.com/questions/2956196/get-the-cell-address-when-a-form-button-in-it-is-clicked
For instance cell column F, row 3 is clicked and has reference. My button sits in cell column C, row 13.
I was able to get what I need by this:
' Get the location (the cell it sits in) of where the button is that was clicked.
Set button_clicked = ActiveSheet.Shapes(Application.Caller).TopLeftCell
' It returns $C$13
' Pull out the 'column cell letter' from the location so it can be used to set cell locations.
columnCellLetter = Mid(button_clicked.Address, 2, 1)
' It returns C. Which is what I need to proceed further.
Naimish MakwanaPosted Apr 15, 2023, 3:10 PM
Unfortunately, there is no direct way to capture the cell letter of the cell a button is sitting on, without first selecting the cell. This is because the button itself does not have a cell address property that you can reference.
One workaround you could try is to assign a name to the cell that the button is sitting on, and then use that name to reference the cell in your code. To do this, follow these steps:
This way, you can reference the cell without having to select it first. Just make sure that the cell name remains the same even if you move the button to a different cell.