VBA to separate three tabs form master and paste existing data's in it as "values" (no link to master when separated)

nscaria00

New Member
Joined
Apr 21, 2023
Messages
21
Office Version
  1. 365
Platform
  1. Windows
Good Morning,
I have a master excel document which has couple of tabs (shown in tabs to extract image). I am populating data's in three tabs (component, tools and logistics) from other tabs by using "=" function. But I need to extract these three documents before I send to customer.
I am looking for a VBA code to extract these three tabs from master and create another document (with these three tabs) and save with RFQ number from "Pre-review checklist" B1 cell.
In the extracted files, "=" function should not be there and the datas needs to show as values (i mean not linked any more to master file). Is it doable?
I can share the excel document if needs to.
Thanks,
 

Attachments

  • each tab has some formulas that's linked to other tabs in master excel document.JPG
    each tab has some formulas that's linked to other tabs in master excel document.JPG
    90.8 KB · Views: 23
  • Extract button and RFQ number.JPG
    Extract button and RFQ number.JPG
    44.8 KB · Views: 17
  • tabs to extract.JPG
    tabs to extract.JPG
    23.8 KB · Views: 22

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
This may do what you want. It looks like you want the code to run when you click a button, so the code event should look something like this:
VBA Code:
Sub NameOfButton_Click()

Replace "NameOfButton" with your button name, assuming it is a command button. If it is a form button, then assign the following vba code to it:

VBA Code:
Sub CopySheets()
Dim wb As Workbook
Dim wbNew As Workbook
Dim wbNewPath As String
Dim shNew As Worksheet
Dim arSh() As Variant
Dim i As Long

Set wb = Workbooks("Book1 3-1-2024.xlsm")

wbNewPath = ThisWorkbook.Path & "\" & Sheets("Pre-review Checklist").Range("B1").Value & ".xlsx"

Set wbNew = Workbooks.Add

arSh = Array("Component", "Tools", "Logistics")

On Error Resume Next

For i = 0 To 2
    wb.Sheets(arSh(i)).Cells.Copy
    
    With wbNew.Worksheets

          Set shNew = Nothing
          Set shNew = .Item(arSh(i))

          If shNew Is Nothing Then
              .Add After:=.Item(.Count)
              .Item(.Count).Name = arSh(i)
              Set shNew = .Item(.Count)
          End If
      End With

      With shNew.Range("A1")
          .PasteSpecial (xlPasteColumnWidths)
          .PasteSpecial (xlFormats)
          .PasteSpecial (xlValues)
          .Select
      End With
Next i

Application.DisplayAlerts = False
wbNew.Sheets("Sheet1").Delete
wbNew.SaveAs wbNewPath
wbNew.Close
Application.DisplayAlerts = True

End Sub
 
Upvote 0
It does extracted and saved as per requirement. But no datas - nothing. It's empty. Only colors are there per original document.
Please see attachment.
 

Attachments

  • Trial 1.JPG
    Trial 1.JPG
    131.1 KB · Views: 13
Upvote 0
It does extracted and saved as per requirement. But no datas - nothing. It's empty. Only colors are there per original document.
Please see attachment.
Interesting. It is pasting values for me when I test.. Not sure what else to try but I'll let you know if I come up with something different.
 
Upvote 0
Here it is again with some very minor changes.

VBA Code:
Sub CopySheets()
Dim wb As Workbook
Dim wbNew As Workbook
Dim wbNewPath As String
Dim shNew As Worksheet
Dim arSh() As Variant
Dim i As Long

Set wb = Workbooks("Book1 3-1-2024.xlsm")

wbNewPath = ThisWorkbook.Path & "\" & Sheets("Pre-review Checklist").Range("B1").Value & ".xlsx"

Set wbNew = Workbooks.Add

arSh = Array("Component", "Tools", "Logistics")

On Error Resume Next

For i = 0 To 2
    wb.Sheets(arSh(i)).UsedRange.Copy
    
    With wbNew.Worksheets

          Set shNew = Nothing
          Set shNew = .Item(arSh(i))

          If shNew Is Nothing Then
              .Add After:=.Item(.Count)
              .Item(.Count).Name = arSh(i)
              Set shNew = .Item(.Count)
          End If
      End With

      With shNew.Range("A1")
          .PasteSpecial xlPasteColumnWidths
          .PasteSpecial xlPasteFormats
          .PasteSpecial xlPasteValues
          .Select
      End With
Next i

Application.DisplayAlerts = False
wbNew.Sheets("Sheet1").Delete
wbNew.SaveAs wbNewPath
wbNew.Close
Application.DisplayAlerts = True

End Sub
 
Upvote 0
Unfortunately same issue. Attached pictures for your reference.

I wish I can send excel file for reference.
 

Attachments

  • Trail 2- Logistics- after VBA code.JPG
    Trail 2- Logistics- after VBA code.JPG
    30.6 KB · Views: 14
  • Trail 2- Logistics- from part master.JPG
    Trail 2- Logistics- from part master.JPG
    123.3 KB · Views: 13
  • Trial 2-Component - From part master.JPG
    Trial 2-Component - From part master.JPG
    206.8 KB · Views: 14
  • Trial 2-Component-after VBA code.JPG
    Trial 2-Component-after VBA code.JPG
    151.1 KB · Views: 14
  • Trial 2-Tools- after VBA code.JPG
    Trial 2-Tools- after VBA code.JPG
    35.2 KB · Views: 18
  • Trial 2-Tools- from part master.JPG
    Trial 2-Tools- from part master.JPG
    90.3 KB · Views: 22
Upvote 0
You could upload the book to a file share and post the link.
 
Upvote 0
Added to wetransfer: Link below:

 
Upvote 0
Alright, give this a try:
VBA Code:
Sub CopySheets()
Dim wb As Workbook
Dim wbNew As Workbook
Dim wbNewPath As String
Dim shNew As Worksheet
Dim arSh() As Variant
Dim i As Long

Set wb = Workbooks("Part Master.xlsm")

wbNewPath = ThisWorkbook.Path & "\" & Sheets("Pre Review Checklist").Range("B1").Value & ".xlsx"

Set wbNew = Workbooks.Add

arSh = Array("Component", "Tools", "Logistics")

On Error Resume Next

For i = 0 To 2
    With wbNew.Worksheets

          Set shNew = Nothing
          Set shNew = .item(arSh(i))

          If shNew Is Nothing Then
              .Add After:=.item(.Count)
              .item(.Count).Name = arSh(i)
              Set shNew = .item(.Count)
          End If
      End With
      wb.Sheets(arSh(i)).UsedRange.Copy
      With shNew.Range("A1")
        .PasteSpecial xlPasteValues
        .PasteSpecial xlPasteColumnWidths
        .PasteSpecial xlPasteFormats
        .Select
      End With
Next i

Application.DisplayAlerts = False
wbNew.Sheets("Sheet1").Delete
wbNew.SaveAs wbNewPath
wbNew.Close
Application.DisplayAlerts = True

End Sub

It's not going to copy the image on the "Component" page and the row heights are default. They can be adjusted if you need them to be, but its not as easy as column widths.
 
Upvote 0
Solution
Alright, give this a try:
VBA Code:
Sub CopySheets()
Dim wb As Workbook
Dim wbNew As Workbook
Dim wbNewPath As String
Dim shNew As Worksheet
Dim arSh() As Variant
Dim i As Long

Set wb = Workbooks("Part Master.xlsm")

wbNewPath = ThisWorkbook.Path & "\" & Sheets("Pre Review Checklist").Range("B1").Value & ".xlsx"

Set wbNew = Workbooks.Add

arSh = Array("Component", "Tools", "Logistics")

On Error Resume Next

For i = 0 To 2
    With wbNew.Worksheets

          Set shNew = Nothing
          Set shNew = .item(arSh(i))

          If shNew Is Nothing Then
              .Add After:=.item(.Count)
              .item(.Count).Name = arSh(i)
              Set shNew = .item(.Count)
          End If
      End With
      wb.Sheets(arSh(i)).UsedRange.Copy
      With shNew.Range("A1")
        .PasteSpecial xlPasteValues
        .PasteSpecial xlPasteColumnWidths
        .PasteSpecial xlPasteFormats
        .Select
      End With
Next i

Application.DisplayAlerts = False
wbNew.Sheets("Sheet1").Delete
wbNew.SaveAs wbNewPath
wbNew.Close
Application.DisplayAlerts = True

End Sub

It's not going to copy the image on the "Component" page and the row heights are default. They can be adjusted if you need them to be, but its not as easy as column widths.
That works well. Thank you so much. Just one more questions. If I need to keep Customer cells intact (meaning leave the formulas as such in the copied document as well similar to the ones we extracted from), is that possible? All the cells other than white and yellow color are Customer filled formulas. Attached image as example. I am sorry asking this.
 

Attachments

  • Formulas on Customer sheet.JPG
    Formulas on Customer sheet.JPG
    107.6 KB · Views: 14
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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