0

My below code does a few things, asks for a name of a new sheet, creates it and enters the name into the next blank C cell.

Cells next to this, D,E,F, and G have relevent data that i call from the new sheets when they are created EG: Project manager, start date etc.

Rather than having these all populated and having an ugly table with "#Ref!" everywhere I thought it would be best to do this whilst a new project is being created.

Basically, depending on the "EMRange" used below, for example the next one empty one being C6, I'd then like D6,E6,F6 and G6 to auto fill the formula from the cells above.

I gather I need to use selection.autofill but I'm unsure on how to get the destination to be those specific cells depending on what the EMRange was in the part of the script previous.

Public Sub CopySheetAndRenameByCell()
    Dim newName As String, Emrange As Range, wsNew As Worksheet, wb As Workbook
    Dim wsIndex As Worksheet
    
    newName = InputBox("Enter the name of the new project", _
                       "Copy worksheet", ActiveCell.Value)
    
    If newName <> "" Then
        Set wb = ThisWorkbook
        wb.Worksheets("Project Sheet BLANK").Copy _
                      After:=wb.Worksheets(wb.Worksheets.Count)
        Set wsNew = wb.Worksheets(wb.Worksheets.Count)
        On Error Resume Next 'ignore error on rename
        wsNew.Name = newName
        On Error GoTo 0     'stop ignoring errors
        
        Set wsIndex = wb.Worksheets("Client Projects Overview")
        Set Emrange = wsIndex.Range("C" & Rows.Count).End(xlUp).Offset(1)
        wsIndex.Hyperlinks.Add Anchor:=Emrange, _
                           Address:="", SubAddress:="'" & wsNew.Name & "'!A1", _
                           TextToDisplay:=wsNew.Name
        'reset font style
        Emrange.Font.Underline = xlUnderlineStyleNone
        Emrange.Font.ColorIndex = xlAutomatic
        Emrange.Font.Name = "Century Gothic"
        Emrange.Font.Size = "10"
        

        If wsNew.Name <> newName Then 'in case sheet could not be renamed....
            MsgBox "Name provided '" & newName & _
                    "' is not valid as a worksheet name!", vbExclamation
        End If
    End If
End Sub

Steve101
  • 51
  • 1
  • 8
  • If Emrange is C6 then `Emrange.offset(,1)` will be D6. Is that what you mean? – SJR Nov 09 '22 at 11:31
  • @SJR I believe so yes, i'll give it a go, but it needs to hit D6, E6,F6 and G6, so would that make it ```EMRange.Offset(, 1), EMRange.Offset(, 4)).Select``` ? – Steve101 Nov 09 '22 at 11:39
  • Or you could use `Emrange.offset(,1).resize(,4)`. – SJR Nov 09 '22 at 11:44
  • 1
    And there's no need to select https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba – SJR Nov 09 '22 at 11:44
  • @SJR I tried this, but it doesn't seem to do anything? ```Set SourceRange = Range("D5:G5") Set FillRange = Emrange.Offset(, 1).Resize(, 4) SourceRange.AutoFill Destination:=FillRange``` I dim'd them both as a range which i assumed was correct? – Steve101 Nov 09 '22 at 12:11
  • The fill range has to include the source range. – SJR Nov 09 '22 at 13:00
  • https://learn.microsoft.com/en-us/office/vba/api/excel.range.autofill – SJR Nov 09 '22 at 13:00
  • Oh.. well in that case i don't think that way of doing it is going to work then.. I tried this but this isn't working either and i am pretty sure the fill range includes the source range in this example? ```Set SourceRange = Emrange.Offset(-1, 1).Resize(, 4) Set FillRange = Emrange.Offset(-1, 1).Resize(1, 4) SourceRange.AutoFill Destination:=FillRange``` – Steve101 Nov 09 '22 at 13:11
  • I think i know why, at the point the script is running EMRange is now the next empty C cell, meaning it is auto filling the blanks next to the newly created one to the cells below.. i think. – Steve101 Nov 09 '22 at 13:19
  • In your second example both ranges are the same. What determines how far it should copy? – SJR Nov 09 '22 at 14:52
  • @SJR the second .resize is 1,4 meaning it the source should be a total of 4 cells and the destination should be 8, 2 rows, no? – Steve101 Nov 10 '22 at 12:05
  • No `Resize(,4)` is the same as `Resize(1,4)`. You need to specify rows/columns only if you want to change the number. https://learn.microsoft.com/en-us/office/vba/api/excel.range.resize – SJR Nov 10 '22 at 12:14
  • Honestly, i still have no idea to work all of this into actually doing what i want it to do on my sheet. – Steve101 Nov 10 '22 at 12:38

1 Answers1

0

Just added an if statement to my formula and auto-filled to all cells rather than trying to do it on a case by case basis in vba.

Ended up looking like this:

=IF(C5<>"",INDIRECT("'"&C5&"'"&"!"&"D3"),"")
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Steve101
  • 51
  • 1
  • 8