VBA to copy sheet, rename sheet, copy and paste cells into newly created sheet

Short_bus

New Member
Joined
Jun 30, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Daily I will pull “Bulk Data” from my company’s server, and it will provide me with multiple columns of data. I will paste that bulk data into one sheet (Bulk Data) of my workbook. The number of rows of data will vary greatly, 1-50 (customers) depending on the day.

The next sheet (Corrected names) will clean up the data (combine first name with middle name and last name).

The third sheet is a template of a company form that needs to be printed for each of the different customers.

Requesting some help with an Excel VBA that will do multiple things. 1. For each row (customer), I need to copy the template form, rename the form by the customer’s name, then I need to copy 4 cells (from the second sheet) and paste the information into the newly created form, and repeat the process for all customer rows that have information and then print them off. I would like to do all of this from one command button.

Once I have created, renamed, copied/pasted and printed, I am thinking about creating a second command button to delete all the newly created company forms (sheets) and leave the 3 original sheets to repeat the next day.

I have been able to create a VBA that will create the company form and rename them with the customer’s name. However, when I attempt to copy the information that I am pulling from the "Corrected names" sheet, it will stop the function and only copy one company form will get created and nothing gets copied/pasted. I know my looping is the issue, but not sure how to correct. Copying and pasting into the 4 required cells into a sheet that is going to be created is really stumping me. Can I do this all in 1 sub, or does it need to be multiple? Using Excel 365.

I am a VBA novice and am just trying to optimize my time and productivity. I appreciate your assistance.


Macro to copy company form, and rename:


'Name macro

Sub CreateSheets()

Application.ScreenUpdating = False

'Dimension variables and declare data types

Dim rng As range

Dim cell As range



'Enable error handling

On Error GoTo Errorhandling



'Show inputbox to user and prompt for a cell range

Set rng = Application.InputBox(Prompt:="Select cell range:", _

Title:="Create sheets", _

Default:=Selection.Address, Type:=8)



'Iterate through cells in selected cell range

For Each cell In rng



'Check if cell is not empty

If cell <> "" Then

Set sourceSheet = ActiveSheet

ActiveCell.Copy

Sheets("Report template").Copy Before:=Sheets(1) 'Copy the Company form

ActiveSheet.Name = cell



End If



'Continue with next cell in cell range

Next cell



'Go here if an error occurs

Errorhandling:

Application.ScreenUpdating = True

'Stop macro

End Sub
 

Attachments

  • Bulk Data screenshot.jpeg
    Bulk Data screenshot.jpeg
    72.8 KB · Views: 11
  • Company generated form.jpeg
    Company generated form.jpeg
    66.7 KB · Views: 11
  • Corrected names screenshot.jpeg
    Corrected names screenshot.jpeg
    115.3 KB · Views: 12

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I am able to get the cells to populate into the correct spots, but the order is not correct. Anyone have any suggestions on how to get the copied cells to match the created sheets? The copied row information should match the sheet name.

'Name macro
Sub CreateSheets()
Application.ScreenUpdating = False
'Dimension variables and declare data types
Dim rng As range
Dim cell As range

'Enable error handling
On Error GoTo Errorhandling

'Show inputbox to user and prompt for a cell range
Set rng = Application.InputBox(Prompt:="Select the names of the subjects you need forms for:", _
Title:="Create sheets", _
Default:=Selection.Address, Type:=8)

'Iterate through cells in selected cell range
For Each cell In rng

'Check if cell is not empty
If cell <> "" Then
Set sourceSheet = ActiveSheet
ActiveCell.Copy
Sheets("Report template").Copy After:=Sheets(3) 'Copy the template form
ActiveSheet.Name = cell

End If

'Continue with next cell in cell range
Next cell

' Loop Worksheets

Dim WS_Count As Integer
Dim I As Integer


WS_Count = ActiveWorkbook.Worksheets.Count

For I = 2 To WS_Count - 1

ActiveWorkbook.Worksheets(I).Activate


range("AA62") = ThisWorkbook.Sheets("Corrected names").range("A" & I + 2) 'name not matching up with the correct sheet
range("CN59") = ThisWorkbook.Sheets("Corrected names").range("D" & I + 2)
range("Q65") = ThisWorkbook.Sheets("Corrected names").range("C" & I + 2)
range("CG65") = ThisWorkbook.Sheets("Corrected names").range("B" & I + 2)

Next I

'Go here if an error occurs
Errorhandling:
Application.ScreenUpdating = True
'Stop macro
End Sub
 

Attachments

  • Incorrect information for sheet.PNG
    Incorrect information for sheet.PNG
    17.9 KB · Views: 5
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,951
Members
449,095
Latest member
nmaske

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