VBA - Copy cell contents to create new "sub"

ewoeckel

New Member
Joined
Mar 7, 2013
Messages
22
Hi all!

I am not sure if this is possible, but thought I would ask some more fluent folks in VBA. I have a workbook that has "templates" which allows users to load default projects by clicking a button. Probably not the BEST way to do this but an example of this would be:
VBA Code:
Sub CustomProj1()

Application.ScreenUpdating = False

Worksheets("Job1").Range("J4").Value = "0"

Worksheets("Job1").Range("B15").Value = "1"
Worksheets("Job1").Range("C15").Value = "8"

Worksheets("Job1").Range("L4").Value = "1"
 
Worksheets("Job1").Range("C17").Value = "1"
Worksheets("Job1").Range("E17").Value = "8"
Worksheets("Job1").Range("D17").Value = "8"
  
Worksheets("Labor").Range("C9").Value = "1"
Worksheets("Labor").Range("C10").Value = "Engineer"
Worksheets("Labor").Range("C11").Value = "8"
 
End Sub

I am curious if someone decides to load their own values into the cells and not use one of my templates, is there a way to "copy" their selections and add it to a new sub / macro and prompt them on what to name it?

Thanks!
E
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi,
I would suggest rather than hardcode each project in a seperate sub, place all project values in a worksheet (which can be hidden) and give users the ability to choose which project data to load to your templates which can be done with common code like following


VBA Code:
Sub CustomProj()
    Dim TemplateRange(1 To 2)   As Range
    Dim wsProjects              As Worksheet
    Dim m                       As Variant, arr As Variant
    Dim ProjectChoice           As Variant
    Dim Default                 As String
    Const Title                 As String = "Enter Project"
    Dim cell                    As Range
    Dim i                       As Integer, r As Integer
    
    With ThisWorkbook
        Set wsProjects = .Worksheets("Projects")
        Set TemplateRange(1) = .Worksheets("Job1").Range("J4,B15,C15,L4,C17,E17,D17,D17")
        Set TemplateRange(2) = .Worksheets("Labor").Range("C9:C11")
    End With
    
    Do
        ProjectChoice = InputBox("Enter Project", Title, Default)
        'cancel pressed
        If StrPtr(ProjectChoice) = 0 Then Exit Sub
        If Len(ProjectChoice) > 0 Then
            m = Application.Match(ProjectChoice, wsProjects.Rows(1), 0)
            If Not IsError(m) Then Exit Do
            Default = "Project Not Found"
        End If
    Loop
    
    
    arr = wsProjects.Cells(2, CLng(m)).Resize(10, 1).Value
    
    For i = 1 To 2
        For Each cell In TemplateRange(i).Cells
            r = r + 1
            cell.Value = arr(r, 1)
        Next cell
        r = 0
    Next i
    
End Sub

This code only retrieves values from existing projects in the table - you would need to develop idea to add new values entered by users.

Dave

02-03-2021.xls
ABCDEFGHIJK
1Proj1Proj2Proj3Proj4Proj5Proj6Proj7Proj8Proj9Proj10Proj11
21471013161922252831
32581114172023262932
43691215182124273033
547101316192225283134
658111417202326293235
769121518212427303336
8710131619222528313437
9811141720232629323538
10912151821242730333639
111013161922252831343740
Projects
 
Upvote 0
Hi,
I would suggest rather than hardcode each project in a seperate sub, place all project values in a worksheet (which can be hidden) and give users the ability to choose which project data to load to your templates which can be done with common code like following


VBA Code:
Sub CustomProj()
    Dim TemplateRange(1 To 2)   As Range
    Dim wsProjects              As Worksheet
    Dim m                       As Variant, arr As Variant
    Dim ProjectChoice           As Variant
    Dim Default                 As String
    Const Title                 As String = "Enter Project"
    Dim cell                    As Range
    Dim i                       As Integer, r As Integer
   
    With ThisWorkbook
        Set wsProjects = .Worksheets("Projects")
        Set TemplateRange(1) = .Worksheets("Job1").Range("J4,B15,C15,L4,C17,E17,D17,D17")
        Set TemplateRange(2) = .Worksheets("Labor").Range("C9:C11")
    End With
   
    Do
        ProjectChoice = InputBox("Enter Project", Title, Default)
        'cancel pressed
        If StrPtr(ProjectChoice) = 0 Then Exit Sub
        If Len(ProjectChoice) > 0 Then
            m = Application.Match(ProjectChoice, wsProjects.Rows(1), 0)
            If Not IsError(m) Then Exit Do
            Default = "Project Not Found"
        End If
    Loop
   
   
    arr = wsProjects.Cells(2, CLng(m)).Resize(10, 1).Value
   
    For i = 1 To 2
        For Each cell In TemplateRange(i).Cells
            r = r + 1
            cell.Value = arr(r, 1)
        Next cell
        r = 0
    Next i
   
End Sub

This code only retrieves values from existing projects in the table - you would need to develop idea to add new values entered by users.

Dave

02-03-2021.xls
ABCDEFGHIJK
1Proj1Proj2Proj3Proj4Proj5Proj6Proj7Proj8Proj9Proj10Proj11
21471013161922252831
32581114172023262932
43691215182124273033
547101316192225283134
658111417202326293235
769121518212427303336
8710131619222528313437
9811141720232629323538
10912151821242730333639
111013161922252831343740
Projects

Ooooooh. Very interesting. I am not 100% sure how the code here works, but I am definietely going to play with this to see if I can get it to work. Thank you Dave!
 
Upvote 0
Ooooooh. Very interesting. I am not 100% sure how the code here works, but I am definietely going to play with this to see if I can get it to work. Thank you Dave!

Its only a suggestion & idea will need developing to meet project need

Rich (BB code):
Set TemplateRange(1) = .Worksheets("Job1").Range("J4,B15,C15,L4,C17,E17,D17,D17")

If not already seen it, you will need to correct my double entry of address D17

Dave
 
Upvote 0

Forum statistics

Threads
1,215,050
Messages
6,122,868
Members
449,097
Latest member
dbomb1414

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