Help adding detection and an error handler

jvoss

Board Regular
Joined
Jun 13, 2015
Messages
66
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Good day,

i have a bit of code that was made. currently the scrip works great. little dirty but it works with a few exceptions that have came up.

need to add a section for detecting if the name for the new sheet already exist before the creating of the new sheet if possible. (this may take care of the error handling section)

then some of the code i just don't understand. (explain if possible) see remarks in code block.

also this code is only ran when the command button is activated, and only on the main sheet "SPMIG",
the copy sheet is named "MASTER_SPMIG" and the
renaming is from a $A (and what ever row it is active)

any questions please ask.

VBA Code:
Sub AddWorksheetsFromSelection()
    Dim CurSheet As Worksheet
    Dim Source As Range
    Dim c As Range
Sheets("SPMIG").Select
 ActiveSheet.Cells(ActiveCell.Row, ActiveCell.CurrentRegion.Columns(1).Column).Select'<---------- moves to first column of the active cell in SPMIG work sheet
    
    Set CurSheet = ActiveSheet
    Set Source = Selection.Cells
    Application.ScreenUpdating = False  '<-------  what is this???
    For Each c In Source   '<-------  why loop when only doing 1 sheet.
        sName = Trim(c.Text)
        If Len(sName) > 0 Then
                Sheets("Master_SPMIG").Copy after:=Sheets("SPMIG")
                ActiveSheet.Name = sName + "_SPMIG"
        End If
    Next c
    CurSheet.Activate
    Application.ScreenUpdating = True
End Sub

example of data
HQ2Fwhat ever is here not of concern
HE3Fwhat ever is herenot of concern
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I've added a couple of lines to your code to check if the sheet exists before creating it.
For the 2 lines that you say you don't understand, the first one stops the screen from refreshing constantly, it reduces screen flicker while the code is running, but more importantly, it makes it run much faster. The second line is not looping through the sheets, it is looping through the cells that you selected on the SPMIG sheet. You might not need it, but looping on a single cell makes no difference, not looping with more than 1 cell will cause errors, so the line acts as it's own error handle.
VBA Code:
Sub AddWorksheetsFromSelection()
    Dim CurSheet As Worksheet
    Dim Source As Range
    Dim c As Range
Sheets("SPMIG").Select
 ActiveSheet.Cells(ActiveCell.Row, ActiveCell.CurrentRegion.Columns(1).Column).Select '<---------- moves to first column of the active cell in SPMIG work sheet
    
    Set CurSheet = ActiveSheet
    Set Source = Selection.Cells
    Application.ScreenUpdating = False  '<-------  what is this???
    For Each c In Source   '<-------  why loop when only doing 1 sheet.
        sname = Trim(c.Text)
        If Len(sname) > 0 Then
            On Error Resume Next
                If Not Worksheets(sname + "_SPMIG").Name = sname + "_SPMIG" Then
                    Sheets("Master_SPMIG").Copy after:=Sheets("SPMIG")
                    ActiveSheet.Name = sname + "_SPMIG"
                End If
            On Error GoTo 0
        End If
    Next c
    CurSheet.Activate
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,557
Latest member
richa mishra

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