Excel VBA循环遍历格式化的表单并将数据粘贴到表格中。

huangapple go评论55阅读模式
英文:

Excel VBA Loop through a formatted form and paste the data into a table

问题

I have two worksheets in Excel, one is a formatted form named INPUT and that I want a user to enter their training plan and press a button to paste the records into a table on another worksheet named INPUT DATA. The process will repeat for new hires by adding them to the table without overwriting.
Example

Can someone please explain how to loop the code? Here is the VBA code that I have:

Sub SubmitPlan()

    'NAME
        Sheets("Input").Select
        Range("D7").Select
        Selection.Copy
        Sheets("Input Data").Select
        Range("A1").Select
        Selection.End(xlDown).Select
        ActiveCell.Offset(1).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    'HIREDATE
        Sheets("Input").Select
        Range("G7:H7").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Input Data").Select
        ActiveCell.Offset(0,1).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    'TRAINEETYPE
        Sheets("Input").Select
        Range("D10").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Input Data").Select
        ActiveCell.Offset(0,1).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    'VERIFY
        Sheets("Input").Select
        Range("B15").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Input Data").Select
        Range("D1").Select
        Selection.End(xlDown).Select
        ActiveCell.Offset(1).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    'LOOP REMAINING COLUMNS
	For each cell in rng    
	    Sheets("Input").Select
	    ActiveCell.Offset(0,1).Select
	    Application.CutCopyMode = False
	    Selection.Copy
	    Sheets("Input Data").Select
	    ActiveCell.Offset(0,1).Select
	    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
		:=False, Transpose:=False
End Sub

Thanks for your help:)

I've tried so many things but can't figure it out

英文:

I have two worksheets in Excel, one is a formatted form named INPUT and that I want a user to enter their training plan and press a button to paste the records into a table on another worksheet named INPUT DATA. The process will repeat for new hires by adding them to the table without overwriting.
Example

Can someone please explain how to loop the code? Here is the VBA code that I have:

Sub SubmitPlan()

    'NAME
        Sheets("Input").Select
        Range("D7").Select
        Selection.Copy
        Sheets("Input Data").Select
        Range("A1").Select
        Selection.End(xlDown).Select
        ActiveCell.Offset(1).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    'HIREDATE
        Sheets("Input").Select
        Range("G7:H7").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Input Data").Select
        ActiveCell.Offset(0,1).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    'TRAINEETYPE
        Sheets("Input").Select
        Range("D10").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Input Data").Select
        ActiveCell.Offset(0,1).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    'VERIFY
        Sheets("Input").Select
        Range("B15").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Input Data").Select
        Range("D1").Select
        Selection.End(xlDown).Select
        ActiveCell.Offset(1).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    'LOOP REMAINING COLUMNS
	For each cell in rng    
	    Sheets("Input").Select
	    ActiveCell.Offset(0,1).Select
	    Application.CutCopyMode = False
	    Selection.Copy
	    Sheets("Input Data").Select
	    ActiveCell.Offset(0,1).Select
	    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
		:=False, Transpose:=False
End Sub

Thanks for your help:)

I've tried so many things but can't figure it out

答案1

得分: 1

根据截图,我会这样做:

Public Sub SubmitPlan()
    Dim wsInput As Worksheet: Set wsInput = ThisWorkbook.Worksheets("Input")
    Dim wsData As Worksheet: Set wsData = ThisWorkbook.Worksheets("Input Data")
    Dim rowData As Long: rowData = wsData.Cells(wsData.Rows.Count, 1).End(xlUp).Row
    Dim rowInput As Long: For rowInput = 15 To wsInput.Cells(wsInput.Rows.Count, 1).End(xlUp).Row
        rowData = rowData + 1
        wsData.Cells(rowData, "A").Value = wsInput.Range("B7").Value ' 名称
        wsData.Cells(rowData, "B").Value = wsInput.Range("G7").Value ' 入职日期
        wsData.Cells(rowData, "C").Value = wsInput.Range("D10").Value ' 培训类型
        wsData.Cells(rowData, "D").Value = wsInput.Cells(rowInput, "B").Value ' 已验证
        wsData.Cells(rowData, "E").Value = wsInput.Cells(rowInput, "C").Value ' 课程编号
        wsData.Cells(rowData, "F").Value = wsInput.Cells(rowInput, "D").Value ' 课程标题
        wsData.Cells(rowData, "G").Value = wsInput.Cells(rowInput, "E").Value ' 工作坊时长
        wsData.Cells(rowData, "H").Value = wsInput.Cells(rowInput, "F").Value ' 项目执行
        wsData.Cells(rowData, "I").Value = wsInput.Cells(rowInput, "G").Value ' 工作坊开始日期
        wsData.Cells(rowData, "J").Value = wsInput.Cells(rowInput, "H").Value ' 项目开始日期
    Next
End Sub

希望这对你有所帮助!

英文:

Based on the screenshot, I would do it like this:

Public Sub SubmitPlan()
    Dim wsInput As Worksheet: Set wsInput = ThisWorkbook.Worksheets("Input")
    Dim wsData As Worksheet: Set wsData = ThisWorkbook.Worksheets("Input Data")
    Dim rowData As Long: rowData = wsData.Cells(wsData.Rows.Count, 1).End(xlUp).Row
    Dim rowInput As Long: For rowInput = 15 To wsInput.Cells(wsInput.Rows.Count, 1).End(xlUp).Row
        rowData = rowData + 1
        wsData.Cells(rowData, "A").Value = wsInput.Range("B7").Value ' Name
        wsData.Cells(rowData, "B").Value = wsInput.Range("G7").Value ' Hire Date
        wsData.Cells(rowData, "C").Value = wsInput.Range("D10").Value ' Trainee Type
        wsData.Cells(rowData, "D").Value = wsInput.Cells(rowInput, "B").Value ' Verified
        wsData.Cells(rowData, "E").Value = wsInput.Cells(rowInput, "C").Value ' Course Number
        wsData.Cells(rowData, "F").Value = wsInput.Cells(rowInput, "D").Value ' Course Title
        wsData.Cells(rowData, "G").Value = wsInput.Cells(rowInput, "E").Value ' Workshop Duration
        wsData.Cells(rowData, "H").Value = wsInput.Cells(rowInput, "F").Value ' Project Execution
        wsData.Cells(rowData, "I").Value = wsInput.Cells(rowInput, "G").Value ' Workshop Start Date
        wsData.Cells(rowData, "J").Value = wsInput.Cells(rowInput, "H").Value ' Project Start Date
    Next
End Sub

huangapple
  • 本文由 发表于 2023年6月9日 03:21:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定