How to put a function in a cell using VBA code?

seragrefaat

Board Regular
Joined
Nov 16, 2020
Messages
53
Office Version
  1. 365
Platform
  1. Windows
this is a macro recording for what i want to do
VBA Code:
 Range("Table1[[#Headers],[Oct 4, 2020]]").Select
    Selection.ListObject.ListColumns.Add
    Range("Table1[[#Headers],[Oct 4, 2021]]").Select
    ActiveCell.FormulaR1C1 = "Absence nu"
    Range("O2").Select
    ActiveCell.FormulaR1C1 = "=COUNTBLANK(Table1[@[Nov 4, 2020]:[Oct 4, 2020]])"
    Range("O3").Select
    Range("Table1[Absence nu]").FormulaR1C1 = _
        "=COUNTBLANK(Table1[@[Nov 4, 2020]:[Oct 4, 2020]])"
    ActiveWindow.SmallScroll Down:=-18
I want to replace the above with a vba code because headers are not fixed names also rows of the table are not a fixed number.

Thanks
 
If you use Excel Max's code you would want to convert the sub declaration from
VBA Code:
Sub CountBlankCells()
to...
VBA Code:
Private Sub Workbook_Open()
You will want to place this under the "ThisWorkbook" object for the workbook you want this to run when it opens.

I still believe that you can do what you want to do without VBA though how are your dates determined? In the example you gave the dates are pretty random, does the class not take place every day? It looks like your old macro was adding columns so are you looking for something that adds a the current date to the table when someone opens the file?
No actually i have a piece of code that pop up msgbox with asking the user to enter number and I placed Excel Max Code under it. and it works perfectly.
This is the est method I mean VBA code for me because the original file is extracted from another application and dates are nor fixed wvery time also number of rows(students) are not fixed in each class.
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
I'm glad that you are satisfied.
For everyone who is interested for this problem I have similar code.
Now you can insert formula direct behind last column in the table for each row and save workbook without VBA code.
Formulas stays in the sheet.
Table works and you can use it with all calculation possbilities and inserting or removeing columns functionalities.
VBA Code:
Sub CountBlankCellsFormula()

    Dim varNColumns, varNRows, varNLoop As Long
    Dim varWorksheetName As String
  
    varWorksheetName = "Sheet1"

    varNColumns = Sheets(varWorksheetName).Cells(1, Columns.Count).End(xlToLeft).Column
    varNRows = Sheets(varWorksheetName).Cells(Rows.Count, 1).End(xlUp).Row
  
    If Sheets(varWorksheetName).Cells(1, varNColumns).Value = "Absecnce" Then
            Sheets(varWorksheetName).Columns(varNColumns).Delete
            Sheets(varWorksheetName).Cells(1, varNColumns).Value = "Absecnce"
    Else
            Sheets(varWorksheetName).Cells(1, varNColumns + 1).Value = "Absecnce"
    End If
  
    varNColumns = Sheets(varWorksheetName).Cells(1, Columns.Count).End(xlToLeft).Column - 1
  
    Sheets(varWorksheetName).Cells(2, varNColumns + 1).FormulaR1C1 = _
        "=COUNTBLANK(RC[-" & varNColumns - 1 & "]:RC[-1])"
    Sheets(varWorksheetName).Cells(2, varNColumns + 1).AutoFill _
        Destination:=Range(Cells(2, varNColumns + 1), Cells(varNRows, varNColumns + 1))

End Sub
 
Upvote 0

Forum statistics

Threads
1,221,545
Messages
6,160,447
Members
451,647
Latest member
Tdeulkar

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