Copy "A1 Current Region" of WS named "Data" and paste to every worksheet that cell A1 is blank.

VBAProIWish

Well-known Member
Joined
Jul 6, 2009
Messages
1,027
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I hope the title was clear enough but here are the logical steps I have in mind to accomplish this...

1. Go to the "Data" WS
2. Select cell "A1"
3. CTRL+A to select the Current Region
4. Copy the Current Region
5. Go to the next worksheet

6. If cell A1 is blank
Paste the data (Current Region from the "Data" WS)

If cell A1 is not blank
Do nothing

7. Repeat 5 and 6 until all worksheets are checked.


Please note that both the number and name of worksheets are dynamic and can change so I can't refer to any worksheets by name.


Thanks much!
 
I'm not sure if this helps or not, but I need to save this code in the master workbook that I keep all my other modules and subs in, so I can't have this code in the workbook that I want to run the code against.

In other words
I want to run the code from a hidden workbook named "Codeit", but actually apply the code to the newly created and active workbook.
 
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
I'm not sure if this helps or not, but I need to save this code in the master workbook that I keep all my other modules and subs in, so I can't have this code in the workbook that I want to run the code against.

In other words
I want to run the code from a hidden workbook named "Codeit", but actually apply the code to the newly created and active workbook.
Where does the Data tab live? In the Codeit or the new workbook?
 
Upvote 1
So I confirmed that is the issue. I pasted your code into the new workbook, ran the code from there and it worked!
So the question now is how can we make that code run from my hidden "Codeit" workbook but execute the code in the active workbook?

Also, even though all the data was indeed pasted into each blank worksheet as intended, it didn't keep any of the column widths, row heights, etc. Is there a way to keep all that when the macro pastes the data into each worksheet?

Thanks so much!
 
Upvote 0
I figured out part of it!

I changed...
VBA Code:
"Set wb = ThisWorkbook"
to
VBA Code:
"Set wb = activeworkbook"

The only issue left is to keep all the formatting when pasting...
 
Upvote 0
Change this
VBA Code:
sourceRng.Copy Destination:=ws.Range("A1")

to
VBA Code:
sourceRng.Copy
ws.Range("A1").PasteSpecial xlPasteAll
Application.CutCopyMode = False
 
Upvote 1
Here's an alternative. Run the code from "Codeit", file browser popups for selection for the workbook you want to perform on.

VBA Code:
Sub RunMacroOnNewWorkbook()
    Dim FileDialog As FileDialog
    Dim SelectedFile As Variant
    Dim NewWb As Workbook
    Dim sourceRng As Range
    Dim sourceSheet As Worksheet
    Dim ws As Worksheet

    Set FileDialog = Application.FileDialog(msoFileDialogFilePicker)

    If FileDialog.Show = -1 Then
        SelectedFile = FileDialog.SelectedItems(1)
    Else
        Exit Sub
    End If

    If SelectedFile <> "" Then
        Set NewWb = Workbooks.Open(SelectedFile)
        Set wb = NewWb
        Set sourceSheet = wb.Sheets("Data")
        Set sourceRng = sourceSheet.Range("A1").CurrentRegion

        ' Loop through sheets in the opened workbook and copy data, column widths, and row heights.
        For Each ws In wb.Sheets
            If (ws.Name <> "Data") And (ws.Range("A1") ="") Then
                sourceRng.Copy
                ws.Range("A1").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                sourceRng.Copy
                ws.Range("A1").PasteSpecial Paste:=xlPasteColumnWidths
                sourceSheet.Rows.RowHeight = ws.Rows.RowHeight
                sourceSheet.Columns.ColumnWidth = ws.Columns.ColumnWidth
            End If
        Next ws

        NewWb.Close SaveChanges:=True
    Else
        MsgBox "No file selected."
    End If
End Sub
 
Upvote 1
Thanks for all that code, but I don't want to have to browse to anything. I just want to run the macro on the active workbook and be done. It will always be on the active workbook.

With big assistance from you, BBB (Thank you!), we managed to get the code working (see below) for everything except for 2 things...
How can the code below be modified to...
1. Freeze panes on the first row on every worksheet that the data from the "Data" ws was pasted to.
2. Zoom each worksheet that the data was pasted to, to 80% (it defaults to "100%")


VBA Code:
Dim wb As Workbook
Dim sourceRng As Range
Dim sourceSheet As Worksheet
Dim ws As Worksheet

Set wb = activeworkbook

Set sourceRng = wb.Sheets("Data").Range("A1").CurrentRegion

    For Each ws In wb.Sheets
        If (ws.Name <> "Data") And (ws.Range("A1") = "") Then
   sourceRng.Copy
   
ws.Range("A1").PasteSpecial xlPasteColumnWidths
ws.Range("A1").PasteSpecial xlPasteAll

Application.CutCopyMode = False
                
        End If
    Next ws

End Sub

Thanks much!
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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