VBA experts, I need guidance on VBA code to do a few things.

jherrera

New Member
Joined
Sep 5, 2023
Messages
1
Office Version
  1. 2019
Platform
  1. Windows
I have been looking for a VBA subroutine to add it into a "main routine." My program will always have three sheets: the "start" sheet (permanent sheet), "a," and "b" or "x" and "z" (only two scenarios). What I want to accomplish is this:
1. The main VBA routine is running, then
2. If sheet "a" exists, then insert in sheet "b," cell $D$1, a formula that refers to the "start" sheet (=start!$F$1).
3. If "a" does not exist but "x" exists, then insert in sheet "z," cell $D$1, a formula that refers to the start sheet (=start!$F$2).
4. Do nothing if sheets "a" and "x" do not exist.
4. Then, continue running and finishing the main routine.
Thank you for your help!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: VBA experts, I need guidance on VBA code to do a few things.
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
VBA Code:
Sub Main()
    '
    '
    '
    '
    InsertX
    '
    '
    '
    '
    
End Sub

Sub InsertX()
    For Each sh In Worksheets
        If sh.Name = "a" Then Worksheets("b").Range("D1").Formula = "=start!$F$1"
        If sh.Name = "x" Then Worksheets("z").Range("D1").Formula = "=start!$F$2"
    Next sh
End Sub
 
Upvote 0
Put cell_check in the main Sub where you want it to run.
VBA Code:
Sub cell_check()
Dim wb As Workbook, sht As Worksheet
Set wb = ThisWorkbook
For Each sht In wb.Sheets
    If sht.Name = "a" Then
        wb.Sheets("b").Range("D1").Formula = "=start!$F$1"
        Exit Sub
    ElseIf sht.Name = "x" Then
        wb.Sheets("z").Range("D1").Formula = "=start!$F$2"
        Exit Sub
    End If
Next sht
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,136
Messages
6,123,251
Members
449,093
Latest member
Vincent Khandagale

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