我需要将数据复制到模板中,但是我不确定如何创建包含所有范围和单元格的一行以使代码更小。现在,我使用13行填充模板中的20种产品之一。有人能帮忙吗?非常感谢
Dim FileName As String
FileName = ""
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select File"
.Filters.Add "Excel File", "*.xls?"
.AllowMultiSelect = False
If .Show Then
FileName = .SelectedItems(1)
End If
End With
If Len(FileName) < 4 Then Exit Sub 'No file selected
Dim TempWorkbook As Workbook, currentSheet As Worksheet
Set currentSheet = ActiveSheet 'Store the ActiveSheet, it will change
Set TempWorkbook = Workbooks.Open(FileName, ReadOnly:=True)
For Index = 8 To 11
currentSheet.Range("T" & Index).FormulaR1C1 = "=" & TempWorkbook.Worksheets("FINAL FORM").Cells((Index + 10), 2).Address(True, True, xlR1C1, True)
currentSheet.Range("U" & Index).FormulaR1C1 = "=" & TempWorkbook.Worksheets("FINAL FORM").Cells((Index + 10), 4).Address(True, True, xlR1C1, True)
currentSheet.Range("V" & Index).FormulaR1C1 = "=" & TempWorkbook.Worksheets("FINAL FORM").Cells((Index + 10), 9).Address(True, True, xlR1C1, True)
currentSheet.Range("W" & Index).FormulaR1C1 = "=" & TempWorkbook.Worksheets("FINAL FORM").Cells((Index + 10), 13).Address(True, True, xlR1C1, True)
currentSheet.Range("X" & Index).FormulaR1C1 = "=" & TempWorkbook.Worksheets("FINAL FORM").Cells((Index + 10), 17).Address(True, True, xlR1C1, True)
currentSheet.Range("Y" & Index).FormulaR1C1 = "=" & TempWorkbook.Worksheets("FINAL FORM").Cells((Index + 10), 21).Address(True, True, xlR1C1, True)
currentSheet.Range("Z" & Index).FormulaR1C1 = "=" & TempWorkbook.Worksheets("FINAL FORM").Cells((Index + 10), 25).Address(True, True, xlR1C1, True)
Next
新编辑:
Dim FileName As String
FileName = ""
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select File"
.Filters.Add "Excel File", "*.xls?"
.AllowMultiSelect = False
If .Show Then
FileName = .SelectedItems(1)
End If
End With
If Len(FileName) < 4 Then Exit Sub 'No file selected
Dim TempWorkbook As Workbook, currentSheet As Worksheet
Set currentSheet = ActiveSheet 'Store the ActiveSheet, it will change
Set TempWorkbook = Workbooks.Open(FileName, ReadOnly:=True)
Dim TempSheet As Worksheet: Set TempSheet = TempWorkbook.Worksheets("FINAL FORM")
Dim i As Double
Dim Index As Double
Dim arrz As Variant
arrz = Array(2, 4, 9, 13, 17, 21, 25, 29, 30, 36, 37, 38, 39)
For Index = 8 To 11
For i = 20 To 32
currentSheet.Cells(Index, i).FormulaR1C1 = "=" & TempSheet.Cells((Index + 10), arrz(i - 39)).Address(True, True, xlR1C1, True)
currentSheet.Cells((Index + 7), i).FormulaR1C1 = "=" & TempSheet.Cells((Index + 21), arrz(i - 39)).Address(True, True, xlR1C1, True)
Next i
Next Index
End Sub
答案 0 :(得分:1)
这应该可以满足您的要求,看起来更干净:
Dim arr() As Variant, arr2() As Variant
arr = Array(2, 4, 9, 13, 17, 21, 25)
For cl = 20 To 26
For rw = 8 To 11
currentSheet.Cells(rw, cl).FormulaR1C1 = "=" & TempWorkbook.Worksheets("FINAL FORM").Cells((rw + 10), arr(cl - 20)).Address(True, True, xlR1C1, True)
Next
For rw = 15 To 18
currentSheet.Cells(rw, cl).FormulaR1C1 = "=" & TempWorkbook.Worksheets("FINAL FORM").Cells((rw + 14), arr(cl - 20)).Address(True, True, xlR1C1, True)
Next
Next
答案 1 :(得分:0)
我选择在一个循环中完成两个模块,而不是用一些数学运算,并且还使工作表变暗以便它们可以与我的测试一起使用。显然,您将这些更改为所需的工作表。
Sub Copy()
Dim FileName As String
FileName = ""
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select File"
.Filters.Add "Excel File", "*.xls?"
.AllowMultiSelect = False
If .Show Then
FileName = .SelectedItems(1)
End If
End With
If Len(FileName) < 4 Then Exit Sub 'No file selected
Dim TempWorkbook As Workbook, currentSheet As Worksheet
Set currentSheet = ActiveSheet 'Store the ActiveSheet, it will change
Set TempWorkbook = Workbooks.Open(FileName, ReadOnly:=True)
Dim TempSheet As Worksheet: Set TempSheet = TempWorkbook.Worksheets("FINAL FORM")
Dim i As Double
Dim Index As Double
Dim arrz As Variant
arrz = Array(2, 4, 9, 13, 17, 21, 25, 29, 33, 36, 37, 38, 39)
For Index = 8 To 11
For i = 20 To 32
currentSheet.Cells(Index, i).FormulaR1C1 = "=" & TempSheet.Cells((Index + 10), arrz(i - 20)).Address(True, True, xlR1C1, True)
currentSheet.Cells((Index + 7), i).FormulaR1C1 = "=" & TempSheet.Cells((Index + 21), arrz(i - 20)).Address(True, True, xlR1C1, True)
Next i
Next Index
End Sub
根据评论进行编辑:为此添加了文件选择器和正确的工作表引用。经过测试,可以在我的机器上工作。