создание нового листа через vba через приватную книгу
Не создается новый рабочий лист в excel через vba из приватной книги, только из личной. Может, кто знает, можно ли создать новый рабочий лист из частной настройки кнопки на ленте?
Моя часть кода:
Private Sub add_new_art(control As IRibbonControl)
Dim selectedRange As Range
Dim concatenatedString As String
Dim newSheet As Worksheet
Dim destinationCell As Range
Dim sheetCounter As Long
Dim sheetName As String
' Check if a range is selected
If TypeName(Selection) <> "Range" Then
MsgBox "Please select a range of cells before running this macro.", vbExclamation
Exit Sub
End If
' Get the selected range
Set selectedRange = Selection
' Loop through each cell in the selected range and concatenate the cell values with commas
For Each cell In selectedRange
concatenatedString = concatenatedString & cell.Value & ","
Next cell
' Remove the trailing comma from the concatenated string
If Right(concatenatedString, 1) = "," Then
concatenatedString = Left(concatenatedString, Len(concatenatedString) - 1)
End If
' Create a new worksheet with a unique name to paste the concatenated result
sheetCounter = 1
Do
sheetName = "ConcatenatedResult" & IIf(sheetCounter > 1, "_" & sheetCounter, "")
On Error Resume Next
Set newSheet = ThisWorkbook.Worksheets.Add
newSheet.Name = sheetName
On Error GoTo 0
sheetCounter = sheetCounter + 1
Loop While newSheet Is Nothing
' Set the destination cell in the new sheet (e.g., A1)
Set destinationCell = newSheet.Range("A1") ' Replace with your desired destination cell
' Paste the concatenated result into the destination cell in the new sheet
destinationCell.Value = concatenatedString
End Sub
Пробовал и другие варианты - просто создать новый рабочий лист, проблема именно в создании нового рабочего листа.