简体   繁体   中英

creating new worksheet from workbook in VBA (Excel)

If WSheetFound Then 'if WSheetFound = True

    'copy and paste the record to the relevant worksheet, in the next available row
    internal_numberName.Offset(0, -3).Resize(1, 10).Copy Destination:=Worksheets(internal_numberName.Value).Range("A1").End(xlDown).Offset(1, 0)

    Else 'if WSheetFound = False
    
    Set NewWSheet = Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)) ' insert a new Worksheet
    NewWSheet.Name = internal_numberName 'named after that branch
    
    DataWSheet.Range("A1", DataWSheet.Range("A1").End(xlToRight)).Copy Destination:=NewWSheet.Range("A1") 'and copy the headings to it
    
    internal_numberName.Offset(0, -3).Resize(1, 10).Copy Destination:=NewWSheet.Range("A2") ' then copy and paste the record to i
    
End If

while Heading is getting created, its failing when trying to add content from A2, can anyone help me on this

Adding a New Worksheet If It Doesn't Exist

  • The only error I could produce was when internal_numberName contained a number, then the first line in the 'If' statement would fail which is easily avoided with ...Worksheets(CStr(internal_numberName.Value))... .
  • These 'unexplained' errors often happen when there is an On Error Resume Next placed somewhere before the code. If that is the case, remove it because it 'hides' one or several errors previously occurring.
  • If the posted code doesn't help, you will need to reveal more of the code, best all of it.
  • The following code eliminates the need for a function or whatever you're using to determine if the destination worksheet already exists.
Option Explicit

Sub Test()
    
    Dim DataWSheet As Worksheet ' Source Worksheet
    Set DataWSheet = ThisWorkbook.Worksheets("Sheet1")
    Dim internal_numberName As Range
    Set internal_numberName = DataWSheet.Range("D2")
    
    ' The above is irrelevant for your code, it's just for testing purposes.
    
    Dim NewName As String: NewName = CStr(internal_numberName.Value)
    
    Dim NewWSheet As Worksheet ' Destination Worksheet
    Dim srg As Range ' Source Range
    Dim dfCell As Range ' Destination First Cell
    
    ' Reference the destination worksheet.
    
    ' Attempt to reference the worksheet.
    Set NewWSheet = Nothing ' necessary if in a loop
    On Error Resume Next ' defer error trapping
        Set NewWSheet = ThisWorkbook.Worksheets(NewName)
    On Error GoTo 0 ' stop error trapping
    
    If NewWSheet Is Nothing Then ' worksheet doesn't exist
        Set NewWSheet = ThisWorkbook.Worksheets _
            .Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
        NewWSheet.Name = NewName
        DataWSheet.Range("A1", DataWSheet.Cells(1, DataWSheet.Columns.Count) _
            .End(xlToLeft)).Copy Destination:=NewWSheet.Range("A1")
    'Else ' worksheet exists; do nothing
    End If
    
    ' Copy
    
    Set srg = internal_numberName.Offset(0, -3).Resize(1, 10)
    Set dfCell = NewWSheet.Cells(NewWSheet.Rows.Count, "A") _
        .End(xlUp).Offset(1, 0)
    
    srg.Copy dfCell
    
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM