How do I insert a variable range using VBA?

I’m creating a template for work, and I need to be able to insert a variable range from an input table which sits in “Inputs” into a form sheet, which sits in “Form”. The input table currently has a large preset array so that the user doesn’t have to change the range themselves (I work with those some might call “unskilled” at excel), and their inputs could range from anywhere from 2-75+ rows.

Again, these inputs need to then be inserted into “Form”, part of which is a printed form. Every cell above row 15 is hard coded and needs to be preserved, similarly, every cell below row 18 is hard coded and also needs to be preserved. That leaves rows 16 and 17 to be the basis for insertion of the data. My thinking is that this will help prevent errors, however if there is a better solution please advise.

To make things more complicated, I only need to draw from certain parts of the table. In this situation: Columns 1, 2, 3, and 7 (“Inputs”, Columns B, C, D, H). Respectively, these columns will be inserted into “Form”, columns A, B, D, E.

Lastly, I need to perform two operations on this variable range in “Form” Column F, where F multiplies column E by a constant.

In my attempts, I’ve only been trying to insert the variables and have failed thus far. I’ve added in some error handling to help troubleshoot but it isn’t really working. When I run the code, I get a return message stating the data has been inserted into “Form” but nothing populates.

Below is a reference image for aid.

Below is my code:

Sub InsertDataIntoForm()
    Dim wsInputs As Worksheet
    Dim wsForm As Worksheet
    Dim lastRow As Long, numRows As Long
    Dim i As Long
    
    ' Set worksheet objects
    Set wsInputs = ThisWorkbook.Worksheets("Inputs")
    Set wsForm = ThisWorkbook.Worksheets("Form")
    
    ' Find the last row with data in column B of "Inputs"
    lastRow = wsInputs.Cells(wsInputs.Rows.Count, "B").End(xlUp).Row
    
    ' Calculate number of rows to copy
    numRows = lastRow - 15  ' Start from row 16
    
    ' Copy data from Inputs to Form
    For i = 1 To numRows
        ' Copy columns B, C, D, and H from Inputs to columns A, B, D, and E in Form
        wsForm.Cells(i + 15, "A").Value = wsInputs.Cells(i + 15, "B").Value  ' Column B in Inputs to Column A in Form
        wsForm.Cells(i + 15, "B").Value = wsInputs.Cells(i + 15, "C").Value  ' Column C in Inputs to Column B in Form
        wsForm.Cells(i + 15, "D").Value = wsInputs.Cells(i + 15, "D").Value  ' Column D in Inputs to Column D in Form
        wsForm.Cells(i + 15, "E").Value = wsInputs.Cells(i + 15, "H").Value  ' Column H in Inputs to Column E in Form
    Next i
    
    ' Notify user that data has been inserted
    MsgBox "Data has been inserted into the Form sheet."
End Sub

I’m not the most experienced VBA programmer – I’m self taught and this is my second month using it. I’ve been using a composite of youtube, microsoft support research, and ChatGPT to try and help. Unfortunately, this is the best I can muster thus far.

In my attempts I’ve tried changing all the variables, I’ve tried creating new variables for the ranges, I’ve thought about creating new methods to do this and I have one in mind (assigning a preset range to the form sheet then using VBA to remove empty rows, however I think I would like to try and make this work for my education’s sake. If the challenge is too difficult, I understand however.

If I have left out any important/relevant/pertinent information, please feel free to ask. I will do my best to respond in a timely manner.

EDIT 7/17/2024

Below is revised code to reflect updates (Thanks to @Blackcat). This code works well for inserting the data, however I’m still needing to address the issue of removing unnecessary rows that are empty from the sheet.

Sub InsertDataIntoForm()
    Dim wsInputs As Worksheet
    Dim wsForm As Worksheet
    Dim lastRow As Long, numRows As Long
    Dim i As Long
    
    ' Set worksheet objects
    Set wsInputs = ThisWorkbook.Worksheets("Inputs")
    Set wsForm = ThisWorkbook.Worksheets("Form")
    
    ' Find the last row with data in column B of "Inputs"
    lastRow = wsInputs.Cells(wsInputs.Rows.Count, "B").End(xlUp).Row
    
    ' Calculate number of rows to copy
    numRows = lastRow - 15  ' Start from row 16
    Dim mulconst As Double
    mulconst = 1.2   'the constant to multiply
    
    ' Copy data from Inputs to Form
    For i = 1 To numRows
    
        ' Copy columns B, C, D, and H from Inputs to columns A, B, D, and E in Form
        If i > 2 Then
          wsForm.Rows(i + 15).Insert   'insert row to make space for the new values
        End If
        
        wsForm.Cells(i + 15, "A").Value = wsInputs.Cells(i + 7, "B").Value  ' Column B in Inputs to Column A in Form
        wsForm.Cells(i + 15, "B").Value = wsInputs.Cells(i + 7, "C").Value  ' Column C in Inputs to Column B in Form
        wsForm.Cells(i + 15, "D").Value = wsInputs.Cells(i + 7, "D").Value  ' Column D in Inputs to Column D in Form
        wsForm.Cells(i + 15, "E").Value = wsInputs.Cells(i + 7, "H").Value  ' Column H in Inputs to Column E in Form
        wsForm.Cells(i + 15, "F").Value = wsInputs.Cells(i + 7, "E") * mulconst   'calculate and set the value for column F

    Next i
    
    ' Notify user that data has been inserted
    MsgBox "Data has been inserted into the Form sheet."
End Sub

3

This mod will insert rows in the Form sheet to keep the hardcoded values from row 18. Also make a calculation for the column F.

    numRows = lastRow - 15  ' Start from row 16
    Dim mulconst as Double
    mulconst = 1.2   'the constant to multiply
    ' Copy data from Inputs to Form
    For i = 1 To numRows
        ' Copy columns B, C, D, and H from Inputs to columns A, B, D, and E in Form
        If i > 2 Then   
          wsForm.rows(i + 15).Insert   'insert row to make space for the new values
        End If
        wsForm.Cells(i + 15, "A").value = wsInputs.Cells(i + 15, "B").value  ' Column B in Inputs to Column A in Form
        wsForm.Cells(i + 15, "B").value = wsInputs.Cells(i + 15, "C").value  ' Column C in Inputs to Column B in Form
        wsForm.Cells(i + 15, "D").value = wsInputs.Cells(i + 15, "D").value  ' Column D in Inputs to Column D in Form
        wsForm.Cells(i + 15, "E").value = wsInputs.Cells(i + 15, "H").value  ' Column H in Inputs to Column E in Form
        wsForm.Cells(i + 15, "F").value = wsForm.Cells(i + 15, "E") * mulconst   'calculate and set the value for column F

    Next i

1

Solved

Sub InsertDataIntoForm()
    Dim wsInputs As Worksheet
    Dim wsForm As Worksheet
    Dim lastRow As Long, numRows As Long
    Dim i As Long

    ' Set worksheet objects
    Set wsInputs = ThisWorkbook.Worksheets("Inputs")
    Set wsForm = ThisWorkbook.Worksheets("Form")

    ' Find the last row with data in column B of "Inputs"
    lastRow = wsInputs.Cells(wsInputs.Rows.Count, "B").End(xlUp).Row

    ' Calculate number of rows to copy
    numRows = lastRow - 15  ' Start from row 16
    Dim mulconst As Double
    mulconst = 1.2   ' the constant to multiply

    ' Copy data from Inputs to Form
    For i = 1 To numRows

        ' Copy columns B, C, D, and H from Inputs to columns A, B, D, and E in Form
        If i > 2 Then
          wsForm.Rows(i + 15).Insert   ' insert row to make space for the new values
        End If

        wsForm.Cells(i + 15, "A").Value = wsInputs.Cells(i + 7, "B").Value  ' Column B in Inputs to Column A in Form
        wsForm.Cells(i + 15, "B").Value = wsInputs.Cells(i + 7, "C").Value  ' Column C in Inputs to Column B in Form
        wsForm.Cells(i + 15, "D").Value = wsInputs.Cells(i + 7, "D").Value  ' Column D in Inputs to Column D in Form
        wsForm.Cells(i + 15, "E").Value = wsInputs.Cells(i + 7, "H").Value  ' Column H in Inputs to Column E in Form
        wsForm.Cells(i + 15, "F").Value = wsInputs.Cells(i + 7, "E") * mulconst   ' calculate and set the value for column F

    Next i

    ' Delete rows with empty cells in column A in range 16-100 on wsForm
    For i = 100 To 16 Step -1
        If IsEmpty(wsForm.Cells(i, "A").Value) Then
            wsForm.Rows(i).Delete
        End If
    Next i

    ' Notify user that data has been inserted
    MsgBox "Data has been inserted into the Form sheet."
End Sub

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật