Macro to fill first empty column

Melzebu

New Member
Joined
Jan 24, 2022
Messages
8
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
Platform
  1. Windows
Hi everyone,

I have a table with few headers and I would like to create a macro that fills the cells of the first empty column based on the length of another column and its content based on a third column.
I'll try to explain a little bit better.

Let's say I have a worksheet called "Report" with a table, and the headers of this table are:
Case NumberStatusSeverityPrioritySubjectDate/Time OpenedDate/Time ClosedFRT
respectively from column A to column H

I would like to add another column (in this case I) with header "Year - Month" and the formula that takes input from "Date/Time Opened" column and reports in text only the year-month.
I created the following macro:

Range("I1").Select
ActiveCell.FormulaR1C1 = "Year - Month"
Range("I2:I" & Cells(Rows.Count, "H").End(xlUp).Row).FormulaR1C1 = "=TEXT([@[Date/Time Opened]],""yyyy-mm"")"

This does its job until I select I1 and run it but I would like to change first that the range.select is not I1 but the first empty column of the worksheet "Report" and obviously that the range of cells - Range("I2:I" & Cells(Rows.Count, "H") - is the column selected, so the first empty column.

I need this because the table may have more columns than from A to H so the in some cases the range could be column I in other cases it could be column K or J.

I hope I explained the point.

Let me know please if there's something I can do to specify those parameters.

Thank you all.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Have a try with:
VBA Code:
Dim LC     As Long
Dim LR     As Long
LC = Cells(1, Cells.Columns.Count).End(xlToLeft).Column + 1 'find first empty column
Cells(1, LC).FormulaR1C1 = "Year - Month"
LR = Cells(Rows.Count, "H").End(xlUp).Row
Range(Cells(2, LC), Cells(LR, LC)).FormulaR1C1 = "=TEXT([@[Date/Time Opened]],""yyyy-mm"")"
 
Upvote 0
Solution
Thanks for the positive feedback(y), glad having been of some help.
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,603
Members
449,038
Latest member
Arbind kumar

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