How to use Visual basic editor to Create a new sheet and insert text & formula

DavidW2022

New Member
Joined
Dec 10, 2021
Messages
1
Office Version
  1. 365
Platform
  1. MacOS
I am looking for a command that can add a new sheet within Excel. Which I have:

Sheets.Add(After:=Sheets("Combined")).Name = "Metrics"

But after this I am wanting to add text shown below automatically. Column A is just names and column B is counting occurances on the first sheet "=COUNTIF(Combined!$F:$F,"*"&A1&"*")"

Mitzi
0​
DAN
4​
TW
1​
AA
11​
Comps
1​
Holds
0​
kh
1​
DW
0​
RL
1​
SPH
0​
TimJ
0​
LP
6​
TH
0​
Total
25​

Is there a way I can automate this all into one command line?
 

Attachments

  • Screen Shot 2021-12-10 at 1.19.06 PM.png
    Screen Shot 2021-12-10 at 1.19.06 PM.png
    32.8 KB · Views: 6

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Is there a way I can automate this all into one command line?

I don't think it's possible on a single line, but try the following:

VBA Code:
Sub CreateSheet()
  Dim sh As Worksheet
  Dim arr As Variant
  
  Application.DisplayAlerts = False
  Application.ScreenUpdating = False
  On Error Resume Next
    Sheets("Metrics").Delete
  On Error GoTo 0
  
  Set sh = Sheets.Add(after:=Sheets("Combined"))
  sh.Name = "Metrics"
  arr = Array("Mitzi", "DAN", "TW", "AA", "Comps", "Holds", "kh", "DW", "RL", "SPH", "TimJ", "LP", "TH")
  sh.Range("A1").Resize(UBound(arr) + 1).Value = Application.Transpose(arr)
  sh.Range("B1:B" & UBound(arr) + 1).Formula = "=COUNTIF(Combined!$F:$F,""*"" & A1 & ""*"")"
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,610
Messages
6,056,296
Members
444,855
Latest member
archadiel

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