我必须从一个 Excel 工作表中选择数据并复制到另一个工作表中,但是在复制数据的过程中我需要实现以下目标:
对于原始工作表的每一行,按列选择单元格(我可以预定义,可能使用数组或其他东西)。
操作数据以更改其在新工作表中的方向。请参阅下面的屏幕截图。
很难准确解释我的意思,所以我希望我的截图能传达我的需要。
对于每一行都有一个通道值,我需要按通道排序和压缩所有结果。还需要根据限制检查结果,但在解决此问题后我可以越过它。
我在下面有我的代码,我很欣赏可能存在错误,因为这是我的第一个脚本。没关系按通道排序数据,到目前为止,我什至都在努力选择我想要的列并将它们完全复制到新工作表中。
代码的第一部分是检查并创建一个新的工作表。之后,它继续定义我可以预定义我想要的列的变量和数组。它以一个循环结束,该循环检查 x 行数(尽管我确实希望它迭代尽可能多的行),并且在其中每一行都有另一个循环,根据我预定义的列抓取单元格。
调试时,它在循环内底部的工作表复制功能上显示为对象或应用程序错误。我不确定我哪里出错了。我哪里错了,有没有更好的方法来解决这个问题?
Sub Process_Results()
'User defines the worksheets for this script
sourcedatasheet_name = InputBox("Enter the customer data sheet name: ", "Enter Worksheet Name")
For rep = 1 To (Worksheets.Count)
If LCase(Sheets(rep).Name) = LCase(sourcedatasheet_name) Then
Exit For
ElseIf (rep = Worksheets.Count) And (LCase(Sheets(rep).Name) <> LCase(sourcedatasheet_name)) Then
MsgBox "This sheet does not exist!"
Exit Sub
End If
Next
destinationdatasheet_name = InputBox("Enter the destination worksheet name to write the data to: ", "Enter Destination Worksheet Name")
For rep = 1 To (Worksheets.Count)
If LCase(Sheets(rep).Name) = LCase(destinationdatasheet_name) Then
MsgBox "This sheet already exists!"
Exit Sub
End If
Next
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(ActiveSheet.Name).Name = destinationdatasheet_name
'These are the variables for referencing data sets in the source sheet
Dim source_testmodel
Dim source_testcasename
Dim source_measurementname
Dim source_carrierfrequency
Dim source_limitlow
Dim source_limithigh
Dim source_measuredresult
Dim source_measurementunit
'These are the variables for referencing data set columns in the processed data sheet
Dim destination_testmodel
Dim destination_testcasename
Dim destination_measurementname
Dim destination_carrierfrequency_bottomchannel
Dim destination_carrierfrequency_middlechannel
Dim destination_carrierfrequency_topchannel
Dim destination_measuredresult
'Define the column number and cell column reference for each data set that will be used to retrieve information from the source sheet
source_testmodel = 9
source_testname = 11
source_measurementname = 12
source_measuredcarrierfrequency = 13
source_measurementlimitlow = 15
source_measurementlimithigh = 16
source_measuredresult = 17
source_measurementunit = 18
Dim array_source_fields(8) As Variant
array_source_fields(1) = source_testmodel
array_source_fields(2) = source_testname
array_source_fields(3) = source_measurementname
array_source_fields(4) = source_measuredcarrierfrequency
array_source_fields(5) = source_measurementlimitlow
array_source_fields(6) = source_measurementlimithigh
array_source_fields(7) = source_measuredresult
array_source_fields(8) = source_measurementunit
'Define the column number and cell column reference for each data set that will be used to write information to the processing sheet
destination_testmodel = 1
destination_testname = 2
destination_measurementname = 3
destination_channelbottom = 4
destination_channelmiddle = 5
destination_channeltop = 6
Dim array_processed_fields(6) As Variant
array_processed_fields(1) = destination_testmodel
array_processed_fields(2) = destination_testname
array_processed_fields(3) = destination_measurementname
array_processed_fields(4) = destination_channelbottom
array_processed_fields(5) = destination_channelmiddle
array_processed_fields(6) = destination_channeltop
'Start processing data
Dim y As Variant
Dim lastrow As Long
For x = 1 To 100 'row 'lastrow=activesheet.usedrange.specialcells(xlCellTypeLastCell)
For Each y In array_source_fields 'y = LBound(Application.Transpose(array_source_fields)) To UBound(Application.Transpose(array_source_fields))
Sheets(sourcedatasheet_name).Cells(x, y).Copy Destination:=Sheets(destinationdatasheet_name).Cells(x, y)
Next y
Next x
End Sub