I am attempting to write code that runs through column G, and adds a new sheet for every unique value with out creating duplicates; However from what I have so so it creates duplicates
Public Sub AddSheet()
Worksheets("Dataset").Select
Range("A1", Range("A1").End(xlToRight)).Name = "Title"
Range("A2", Range("G1").End(xlDown)).Name = "Data"
Range("H2", Range("H1").End(xlDown)).Name = "Physician"
Dim i As Integer, lastrow As Integer
lastrow = Worksheets("Dataset").Cells(Worksheets("Dataset").Rows.Count, "H").End(xlUp).Row
With Range("Physician")
For i = 1 To lastrow
If i.Value = Worksheetexists = False Then
Sheet.Add
ActiveSheet.Name = Worksheets("Dataset").Cells(i, 1).Value
Else
GoTo NextStep:
End If
Next
End With
End Sub
something like this will iterate and create a new sheet for every unique value in Column A.
Public Sub AddSheet()
With Worksheets("Dataset")
Dim lastrow As Long
lastrow = .Cells(.Rows.Count, "H").End(xlUp).Row
Dim i As Long
For i = 1 To lastrow
If IsError(Application.Evaluate("'" & .Cells(i, 1).Value & "'!A1")) And .Cells(i, 1) <> "" Then
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets.Add
ws.Name = .Cells(i, 1).Value
End If
Next i
End With
End Sub
Something like this should work and be a bit safer as you may need to check the edge case that a sheet you are adding already exists. I'm using a dictionary to keep track of unique sheet names first, then adding to that based on the unique values in column H.
Sub SOExample()
Dim DataSheet As Excel.Worksheet
Dim ws As Excel.Worksheet
Dim lastRow As Long
Dim i As Long
Dim dict As Object
Dim SheetName As String
Set dict = CreateObject("Scripting.Dictionary")
Set DataSheet = ThisWorkbook.Worksheets("Dataset")
lastRow = DataSheet.Cells(DataSheet.Rows.Count, "H").End(xlUp).Row
'Add existing sheets into the dictionary first
'in case a sheet already exists with that name
For Each ws In ThisWorkbook.Worksheets
dict.Add ws.Name, ws.Name
Next
'Loop the range and add new sheets
For i = 1 To lastRow
SheetName = DataSheet.Cells(i, 8).Value 'Column H is index 8
If Not dict.exists(SheetName) Then
dict.Add SheetName, SheetName
Set ws = ThisWorkbook.Worksheets.Add
ws.Name = SheetName
End If
Next
End Sub
Related
Fairly new to VBA Excel. I want to copy and paste a specific cell[B11 and so on) into a specific cell [E9 and so on] on my target sheet when conditions are met (when C column is equal to No). So far I was able to copy and paste the data on my target sheet. Having trouble when I run the command again. I don't want to overwrite my previous data. How can this be done? `
Private Sub CommandButton1_Click()
Dim RowGCnt As Long, CShtRow As Long
Dim LastRow As Long
Dim CellG As Range
'paste the first result to the 9th row
CShtRow = 9
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For RowGCnt = 11 To LastRow
If Range("C" & RowGCnt).Value = "No" Then
MsgBox (CShtRow)
'Review Criteria
Worksheets("SHEET1").Range("B" & RowGCnt).Copy
Worksheets("REPORT").Range("E" & CShtRow).PasteSpecial xlPasteValues
CShtRow = CShtRow + 1
End If
Next RowGCnt
Application.CutCopyMode = False
End Sub
Untested:
Private Sub CommandButton1_Click()
Dim shtSrc As Worksheet '<< source sheet
Dim RowGCnt As Long
Dim LastRow As Long
Dim cDest As Range '<< copy destination
Set shtSrc = Worksheets("SHEET1")
'paste the first result to the first open row
With Worksheets("REPORT")
Set cDest = .Cells(.Rows.Count, "E").End(xlUp).Offset(1, 0) '<<EDIT
If cDest.Row < 9 Then Set cDest = .Range("E9")
End With
LastRow = shtSrc.Range("A" & shtSrc.Rows.Count).End(xlUp).Row
For RowGCnt = 11 To LastRow
If shtSrc.Range("C" & RowGCnt).Value = "No" Then
cDest.Value = shtSrc.Range("B" & RowGCnt).Value
Set cDest = cDest.Offset(1, 0)
End If
Next RowGCnt
End Sub
Using Tim Williams code. I got a workaround
Private Sub CommandButton1_Click()
Dim shtSrc As Worksheet '<< source sheet
Dim RowGCnt As Long
Dim LastRow As Long
Dim cDest As Range '<< copy destination
Dim vLastRow As Integer
Set shtSrc = Worksheets("SHEET1")
'paste the first result to the first open row
With Worksheets("REPORT")
Set cDest = .Cells(.Rows.Count, "E").End(xlUp)
If cDest.Row < 9 Then
Set cDest = .Range("E9")
Else
vLastRow = .Cells(.Rows.Count, 5).End(xlUp).Row
Set cDest = .Cells(vLastRow + 1, 5)
End If
End With
LastRow = shtSrc.Range("A" & shtSrc.Rows.Count).End(xlUp).Row
For RowGCnt = 11 To LastRow
If shtSrc.Range("C" & RowGCnt).Value = "No" Then
cDest.Value = shtSrc.Range("B" & RowGCnt).Value
Set cDest = cDest.Offset(1, 0)
End If
Next RowGCnt
End Sub
I got stuck in the below-mentioned code, what I want to do is to get the value from Range("C4:C" & LastRow) in worksheets X2 that will b changing every time and compare each value with all open workbooks name. If match found then search that value in A column of worksheet X1 and copy all those rows.
The final objective is to paste those rows into those open workbooks which have the same value. For eg: Range C4 has TW00 then the code will search workbooks which have name "TW00.xlsx" and copy all the rows from worksheet X1 which have TW00 value in column A in the worksheet named TW00.xlsx.
Dim BookNames()
ReDim BookNames(Windows.Count)
n = 1
For n = 1 To Windows.Count
BookNames(n) = Workbooks(n).Name
If Workbooks(n).Name = Workbooks("A.xlsx").Worksheets("X2").Range("C4:C" & LastRow).Value Then
Set Rng = Workbooks("A.xlsx").Worksheets("X1").Range("A2:A50000")
For Each c In Rng.Cells
If c.Value = Workbooks("A.xlsx").Worksheets("X2").Range("C4").Value Then
If Not CopyRng Is Nothing Then
Set CopyRng = Application.Union(CopyRng,
Workbooks("A.xlsx").Worksheets("X1").Rows(c))
Else
Set CopyRng = Workbooks("A.xlsx").Worksheets("X1").Rows(c)
End If
End If
Next c
CopyRng.Copy
Workbooks(n).Activate
Worksheets.Add
ActiveSheet.Name = "X1"
ActiveSheet.Paste
End If
Next n
is that code help you?
Sub test()
Dim lastRow As Long
dim sheetName as string
Dim sourceDataSheet As worksheet
Dim sourceSheetsName as worksheet
dim targetDataSheet as worksheet
Dim wkb As Variant
set sourceDataSheet = ActiveWorkbook.Worksheets("X2")
set sourceSheetsName = ActiveWorkbook.Worksheets("X1")
With sourceSheetsName
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
sheetName = .Cells(lastRow, "A")
For Each wkb In Application.Workbooks
If wkb.Name <> .Name And wkb.Name = sheetName Then
set targetDataSheet = wkb.Worksheets.Add
with sourceDataSheet
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
for i = 1 to lastRow
if .Cells(i, "A").Value = sheetName then
.Cells(i, "A").EntireRow.Copy
targetDataSheet.Cells(i, "A").PasteSpecial Paste:=xlPasteValues
end if
next i
end with
End If
Next wkb
End With
End Sub
I'm trying to loop through a column(A) that contains date and create an arbitrary column(lastcolumn+1) and store only the month value from the column(A) which contains the date. Please help me!
Code: what my code is doing is copying the column and paste it the specified can someone help me to improve my code?
Public Sub Selection()
Dim file1 As Excel.Workbook
Dim Sheet1 As Worksheet
Dim serviceIDRng As Range
Dim lngLastRow As Long
Dim rngSheet1 As Range
Dim NextRow As Long
Dim LastRow As Long
Dim LastCol As Long
Dim c As Long
Set Sheet1 = Workbooks.Open(TextBox1.Text).Sheets(1)
'lngLastRow = Sheet1.Range("A" & Sheet1.Rows.Count).End(xlUp).Row
'Set serviceIDRng = Sheet1.Range("T1:T" & lngLastRow)
Application.ScreenUpdating = False
With Sheet1
NextRow = .Cells(.Rows.Count, "E").End(xlUp).Row + 1
End With
With Sheet1
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
For c = 1 To LastCol
LastRow = .Cells(.Rows.Count, c).End(xlUp).Row
Set rngSheet1 = .Range(.Cells(3, c), .Cells(LastRow, c))
rngSource.Copy Sheet1.Range("E" & NextRow)
NextRow = NextRow + rngSheet1.Rows.Count
Next c
End With
Application.ScreenUpdating = True
MsgBox "Succes!", vbExclamation
End Sub
To extract the month from column "E" to a new column:
Public Sub Selection()
Dim ws As Worksheet, data(), i&
Set ws = Workbooks.Open(TextBox1.text).sheets(1)
' load the data from column E
data = Intersect(ws.Columns("E"), ws.UsedRange)
'set the title
data(1, 1) = "Month"
' extract the month
For i = 2 To UBound(data)
If VarType(data(i, 1)) = vbDate Then
data(i, 1) = Month(data(i, 1))
End If
Next
' write the data back to the sheet
ws.UsedRange.Columns(ws.UsedRange.Columns.count + 1) = data
MsgBox "Succes!", vbExclamation
End Sub
I have been a silent reader on here for a few months but have been struggling with this code for a week now, so thought i would see if anyone can help.
I have a worksheet where sheet 1 contains information for users to input data.
Column A ask a question, column C is where the user will type in an answer.
Row 4 asks how many configurations there will be. depending on what number they input depends on how many cells light up to the right ie if 1 then D4 goes yellow, if 2 then D4 and E4 go yellow (using conditional formatting)
The user will then enter the title into the highlighted cell (D4,E4 ,F4 etc)
I want to create a new sheet at the end of the sheet for each configuration.
then NAME the new sheet by the text entered in D4, E4 etc.
the code I have so far is:
Option Explicit
Sub InsertSupplierSheet()
Dim ws As Worksheet
Dim tmpSht As Worksheet
Dim Lastcol As Integer, i As Integer, j As Integer
Dim DESCRANGE As Range
'~~> Change Sheet1 to the sheet which has all the data
Set ws = ThisWorkbook.Worksheets(1)
With ws
Lastcol = ActiveSheet.Cells(4, Columns.Count).End(xlToLeft).Column
If Lastcol < 4 Then Exit Sub
For i = 3 To Lastcol
If DoesSheetExist(ActiveSheet.Cells(4 & i).Value) Then
Set tmpSht = ActiveSheet.Cells(4 & i).Value
Else
Sheets.Add After:=Sheets(Sheets.Count)
Set tmpSht = ActiveSheet
tmpSht.Name = "NEWSHEET"
End If
.Rows("1:3").Copy tmpSht.Rows(1)
For j = 1 To 4
tmpSht.Columns(j).ColumnWidth = .Columns(j).ColumnWidth
Next j
.Rows(i).Copy tmpSht.Rows(4)
Next
End With
End Sub
Function DoesSheetExist(Sht As String) As Boolean
Dim ws As Worksheet
On Error Resume Next
Set ws = Sheets(ws)
On Error GoTo 0
If Not ws Is Nothing Then DoesSheetExist = True
End Function
I put in "NEWSHEET" to see if even creates a new sheet, but it still fails. I just cant see where I am going wrong.
Any help /advise is welcomed.
EDIT .
I cant work out why though.
The last col will be H4 so lastcol would be "8" .
Then for i = 4 to 8 run the loop. there are descriptions in each of the cells in row 4 so i don't see why it would work for 2 instantness and then fail ?
I dont know if this would make it easier but I have the number of sheets i want to create in cell C4 so i could use this rather than looking up populated cells. so if C4 is 2 then I want to add 2 sheets named as the content of D4, E4. if C4 is 3 then I want to add 3 sheets names as content of D3,E3,F3. Am I making this harder than I need too ?
UPDATE
I figured out the copying over of info is affecting this loop. and amended the code to this.
Sub InsertSupplierSheet()
Dim ws As Worksheet
Dim tmpSht As Worksheet
Dim Lastcol As Integer, i As Integer, j As Integer
Dim DESCRANGE As Range
Dim sShtName As String 'Dimension sheet name variable
'~~> Change Sheet1 to the sheet which has all the data
Set ws = ThisWorkbook.Worksheets(1)
With ws
Lastcol = ActiveSheet.Cells(4, Columns.Count).End(xlToLeft).Column
If Lastcol < 4 Then Exit Sub
For i = 4 To Lastcol
sShtName = ActiveSheet.Cells(4, i).Value2 'Set sShtName to cell value within loop
If DoesSheetExist(sShtName) Then 'Use sShtName to call "Exists" function
Set tmpSht = Sheets(sShtName)
Else
Sheets.Add After:=Sheets(Sheets.Count)
Set tmpSht = ActiveSheet
tmpSht.Name = sShtName 'Change name to sShtName
End If
.Rows("1:3").Copy tmpSht.Rows(1)
.Rows(13).Copy tmpSht.Rows(4)
Next
End With
End Sub
Function DoesSheetExist(Sht As String) As Boolean
Dim ws As Worksheet
On Error Resume Next
Set ws = Sheets(Sht)
On Error GoTo 0
If Not ws Is Nothing Then DoesSheetExist = True
End Function
this is doing what i want it to do with a couple of small exceptions.
the Sheets are being named by the cells in D1 , then E13,F13,G13,H13 So i need to figure out where that info is coming from.
the final bit is that due to my conditional formatting in the First sheet, I am getting text on black backgrounds in the copy cells, but that is the very least of my worries !
UPDATE
Found the error -
sShtName = ActiveSheet.Cells(4, i).Value2
should be
sShtName = Worksheets(1).Cells(4, i).Value2
You are calling your cells incorrectly. Use (4, i) instead of (4 & i).
The way you were calling it concatenated it to 43, which resulted in you checking cell AQ1 (AQ being the 43rd column) for the sheet reference.
Edit: I just walked through it a bit and found a couple of other errors. You need to set the sheet name to sht in your 'exists' function, and I'm assuming you want to set tmpSht to a sheet, so you need to encase it in sheets(). Try this:
Sub InsertSupplierSheet()
Dim ws As Worksheet
Dim tmpSht As Worksheet
Dim Lastcol As Integer, i As Integer, j As Integer
Dim DESCRANGE As Range
Dim sShtName As String 'Dimension sheet name variable
'~~> Change Sheet1 to the sheet which has all the data
Set ws = ThisWorkbook.Worksheets(1)
With ws
Lastcol = ActiveSheet.Cells(4, Columns.Count).End(xlToLeft).Column
If Lastcol < 4 Then Exit Sub
For i = 4 To Lastcol
sShtName = ActiveSheet.Cells(4, i).Value2 'Set sShtName to cell value within loop
If DoesSheetExist(sShtName) Then 'Use sShtName to call "Exists" function
Set tmpSht = Sheets(sShtName)
Else
Sheets.Add After:=Sheets(Sheets.Count)
Set tmpSht = ActiveSheet
tmpSht.Name = sShtName 'Change name to sShtName
End If
.Rows("1:3").Copy tmpSht.Rows(1)
For j = 1 To 4
tmpSht.Columns(j).ColumnWidth = .Columns(j).ColumnWidth
Next j
.Rows(i).Copy tmpSht.Rows(4)
Next
End With
End Sub
Function DoesSheetExist(Sht As String) As Boolean
Dim ws As Worksheet
On Error Resume Next
Set ws = Sheets(Sht)
On Error GoTo 0
If Not ws Is Nothing Then DoesSheetExist = True
End Function
Instead of adding the new sheet and then setting the activesheet to the tmpsht you could use a shorter way (see below). And why did you set the ws if you don't use it....
Sub InsertSupplierSheet()
Dim ws As Worksheet
Dim tmpSht As Worksheet
Dim Lastcol As Integer, i As Integer, j As Integer
Dim DESCRANGE As Range
Set ws = ThisWorkbook.Worksheets(1)
With ws
Lastcol = .Cells(4, .Columns.Count).End(xlToLeft).Column
If (Lastcol < 4) Then
Exit Sub
End If
For i = 4 To Lastcol
If (DoesSheetExist(.Cells(4, i).Value2) = True) Then
Set tmpSht = Sheets(.Cells(4, i).Value)
Else
Set tmpSht = Sheets.Add After:=Sheets(Sheets.Count)
tmpSht.Name = "NEWSHEET"
End If
.Rows("1:3").Copy tmpSht.Rows(1)
For j = 1 To 4
tmpSht.Columns(j).ColumnWidth = .Columns(j).ColumnWidth
Next j
.Rows(i).Copy tmpSht.Rows(4)
Next i
End With
End Sub
Function DoesSheetExist(Sht As String) As Boolean
Dim ws As Worksheet
On Error Resume Next
Set ws = Sheets(Sht)
On Error GoTo 0
If Not ws Is Nothing Then
DoesSheetExist = True
Else
DoesSheetExist = False
End If
End Function
This was my final code. There were a few tweaks, Firstly I added a formula in row 6 to shorten the name of row 4 to a 10 character name as I found the tab names were too long (hence the code for the naming refers to row 6. I also added some custom text to add into each sheet and some formatting
Option Explicit
Sub InsertSupplierSheet()
Dim ws As Worksheet
Dim tmpSht As Worksheet
Dim Lastcol As Integer
Dim i As Integer
Dim j As Integer
Dim DESCRANGE As Range
Dim sShtName As String 'Dimension sheet name variable
'~~> Change Sheet1 to the sheet which has all the data
Set ws = ThisWorkbook.Worksheets(1)
With ws
Lastcol = ActiveSheet.Cells(4, Columns.Count).End(xlToLeft).Column ' work with the template sheet
If Lastcol = 3 Then Exit Sub 'repeat these steps from the first config to the last
For i = 4 To Lastcol
sShtName = Worksheets(1).Cells(6, i).Value2 'Set sShtName to cell value within loop
If DoesSheetExist(sShtName) Then 'Use sShtName to call "Exists" function
Set tmpSht = Sheets(sShtName)
Else
Sheets.Add After:=Sheets(Sheets.Count)
Set tmpSht = ActiveSheet
tmpSht.Name = sShtName 'Change name to sShtName tmpSht.Name = sShtName
End If
.Rows("1:3").Copy tmpSht.Rows(1) ' Format the cell width in the new sheet
.Rows(13).Copy tmpSht.Rows(4)
tmpSht.Range("A1").Value = Worksheets(1).Cells(4, i).Value2
Range("A1").ColumnWidth = 30
Range("B1").ColumnWidth = 0
Range("C1").ColumnWidth = 30
Range("D1:K1").ColumnWidth = 10
Range("D4:J4").Font.Color = vbWhite ' format the colour of the text in the new sheet
Range("C1") = " " ' Negate info in cell C1
With Range("A1:M5") ' add borders
'Clear existing
.Borders.LineStyle = xlNone
'Apply new borders
.BorderAround xlContinuous
.Borders(xlInsideHorizontal).LineStyle = xlContinuous
.Borders(xlInsideVertical).LineStyle = xlContinuous
End With
With Range("A1:C4") ' set colours for the new sheet
.Font.Color = vbBlack
.Interior.Color = vbWhite
End With
Range("D4:J4").Font.Color = vbWhite ' set colour of the numbers to white to show on black background
Range("A5") = "Unit cost in " & Worksheets(1).Cells(17, 3).Value2
Range("A6") = "CUSTOM TEXT ONE."
Range("A7") = "NOTE if quantity " & Range("D4").Value2 + 5 & " is ordered then total cost will be your unit cost for " & Range("D4").Value2 & " multiplied by " & Range("D4").Value2 + 5 & " .This applies up to the quantity of " & Range("E4").Value2 - 1
Range("A8") = "CUSTOM TEXT 2"
Next i
End With
End Sub
Function DoesSheetExist(Sht As String) As Boolean
Dim ws As Worksheet
On Error Resume Next
Set ws = Sheets(Sht)
On Error GoTo 0
If Not ws Is Nothing Then DoesSheetExist = True
End Function
I have a workbook containing 23 work sheets. I have to apply Macro auto-filter to filter required data from the 23 work sheets and save those data as a work book with filtered data in those 23 work sheets..
Sub Switch_Filter()
Dim j As Integer, k As Integer, k1 As Integer
Dim LastRow As Integer, i As Integer, erow As Integer
Dim s As Variant, s1 As Variant
j = Worksheets.Count
s = InputBox("Enter Switch id")
s1 = s & "*"
If s <> vbNullString Then
For k = 1 To 20
If (k <> 1) And (k <> 4) And (k <> 7) Then
With Worksheets(k)
.UsedRange.AutoFilter field:=3, Criteria1:=s1
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 3 To LastRow
Range(Cells(i, 1), Cells(i, 36)).Select
Selection.Copy
Workbooks.Open Filename:="C:\Users\takyar\Documents\salesmaster-new.xlsx"
Worksheets(k).Select
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Cells(erow, 1).Select
ActiveSheet.Paste
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.CutCopyMode = False
Next i
End With
End If
Next k
End If
End Sub
Almost Completed, but it save's the filtered data in the same sheet of the new workbook,here i have attached the code:-
Sub Switch_Filter()
Dim j As Integer, k As Integer
Dim LastRow As Integer, i As Integer
Dim s As Variant, s1 As Variant
Dim MyWorkbook As Workbook, newWork As Workbook
Set MyWorkbook = ThisWorkbook
j = Worksheets.Count
s = InputBox("Enter Switch id")
s1 = s & "*"
If s <> vbNullString Then
For k = 1 To 20
With Worksheets(k)
Set MyWorkbook = ThisWorkbook
If (k <> 1) And (k <> 4) And (k <> 7) Then
.AutoFilterMode = False
With Worksheets(k).UsedRange
.AutoFilter
.AutoFilter Field:=3, Criteria1:=s1
End With
End If
MyWorkbook.Sheets(k).Rows("1:65000").Copy
Set newWork = Workbooks.Open("E:\spreed sheet\sample1.xlsx")
With newWork.Worksheets(k)
Range("A2").PasteSpecial Paste:=xlPasteAll
newWork.Close
End With
End With
Next k
End If
End Sub
pls suggest me a solution.
Thanks in Advance....!!!
Not even sure if this is executing, you didn't really say where your error is occurring.
Without that information, I think the biggest problem is that you are copying one row at a time and you are opening and closing a workbook every time you want to copy that row.
If you want the new workbook to contain all separate sheets just with the filtered data then you might want to consider creating a new worksheet within the old document with just the filtered data and cutting/moving it to the new document a whole sheet at a time. - That is something that you could learn the code from recording a macro and manually doing it.
Otherwise, if you can store this data all in 1 sheet/table I'd recommend loading each worksheet with its filtered data into one single array and then opening the new workbook and writing all of the information from that array. This option would most likely be the quickest.
Finally Got the Answer
Sub Switch_Filter()
Dim j As Integer, k As Integer
Dim LastRow As Integer, i As Integer
Dim s As Variant, s1 As Variant
Dim MyWorkbook As Workbook, newWork As Workbook
Dim ws As Worksheet, ws1 As Worksheet
Dim name As String
Set MyWorkbook = ThisWorkbook
j = Worksheets.Count
s = InputBox("Enter Switch id")
s1 = s & "*"
If s <> vbNullString Then
For k = 1 To j
With Worksheets(k)
Set MyWorkbook = ThisWorkbook
'.UsedRange.AutoFilter Field:=3, Criteria1:=s1
If (k <> 1) And (k <> 4) And (k <> 7) And (k < 20) Then
.AutoFilterMode = False
With Worksheets(k).UsedRange
.AutoFilter
.AutoFilter Field:=3, Criteria1:=s1
End With
End If
'LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
MyWorkbook.Sheets(k).Rows("1:65000").Copy
Set newWork = Workbooks.Open("E:\spreed sheet\sample1.xlsx")
Set ws = Sheets.Add
name = ws.name
With newWork.Sheets(name)
Range("A2").PasteSpecial Paste:=xlPasteAll
newWork.Close
End With
End With
Next k
End If
End Sub