Copy all exist workbooks sheet with range, but exclude 4 know sheet and paste into another closed workbook without knowing sheet name. Read the text!

strinux

New Member
Joined
Nov 18, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello Everyone ! :cool:
I need a little help, The code i have now working and i understand how is working, but i wish something smarter
.
btw: i have got the sharepoint part to work with this code also, but showing localy for now.


The problem is my main sheet need to be added to sharepoint, so can´t use vba any more that way i need it.
So i have made a sheet for my coworks, so the idea is i want real excel data from main sheet to be showing or copied over to the coworkers sheet, so they only see what they need to see.


rules:
1. i never know what my sheet will be named, cause is diffrent stuff all the time, but the row i need copy is all time the same.
2. the 4 sheet their must not be copied has all time the same name.

Idea:
I need something, their can copy all exist sheet with range from "Main.xlsx", but 4 sheet may not be copied and then everything get insert into the closed coworker sheet.
the idea is the code need to be running when im running my excel sheet, mabye with a timer i copy and saving into coworker every 1min or in real time.

i hope you understand my english since im danish and my grammar is not the best.


VBA Code:
Sub test()

    Dim MainBoook As Workbook
    Dim CopyBook As Workbook

    Dim ws As Worksheet
    Dim SheetName As String
    Dim SheetExists As Boolean

    Set Mains = Workbooks.Open("C:\Users\demo\Documents\main.xlsm")
    Set Copys = Workbooks.Open("C:\Users\demo\Documents\copy.xlsm")
   
    
    SheetName = InputBox("Writte the name of the sheet")
    SheetExists = False
    
    With Copys
        'Check if the Sheet exists            
        For Each ws In .Worksheets
            If ws.Name = SheetName Then
                SheetExists = True
            End If
        Next        
        If SheetExists = False Then
                'If the sheet dont exists, create
                .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = SheetName
        End If                        
    End With
    
    With Mains
        'Check if the Sheet exists
        For Each ws In .Worksheets
            If ws.Name = SheetName Then
                SheetExists = True
                
                Mains.Sheets(SheetName).Range("A1:v1").Copy
                Copys.Sheets(SheetName).Range("A1").PasteSpecial Paste:=xlPasteColumnWidths
                Copys.Sheets(SheetName).Range("A1").PasteSpecial Paste:=xlPasteAllExceptBorders
                Copys.Sheets(SheetName).Range("A1").PasteSpecial Paste:=xlPasteAllUsingSourceTheme
                Copys.Sheets(SheetName).Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    
                Mains.Sheets(SheetName).Range("a111:v130").Copy
                Copys.Sheets(SheetName).Range("a2").PasteSpecial Paste:=xlPasteColumnWidths
                Copys.Sheets(SheetName).Range("a2").PasteSpecial Paste:=xlPasteAllExceptBorders
                Copys.Sheets(SheetName).Range("a2").PasteSpecial Paste:=xlPasteAllUsingSourceTheme
                Copys.Sheets(SheetName).Range("a2").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    
                Copys.Close SaveChanges:=True
             End If
        Next
                
    End With  
    
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Forum statistics

Threads
1,215,584
Messages
6,125,669
Members
449,248
Latest member
wayneho98

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