关于单元格维度的行和列属性,我有一个非常简单的问题。
对于上下文-我有三张纸,其中一张是母版纸。我正在根据列标题将两个数据表中的每一个数据复制到主数据表中。并非主选项卡中的所有列标题都出现在数据表中。我编写了以下代码,该代码循环遍历第一个数据表(ws2),并将数据列粘贴到主表中的相关列中。
Sub master_sheet_data()
'Variables
Dim ws1_xlRange As Range
Dim ws1_xlCell As Range
Dim ws1 As Worksheet
Dim ws2_xlRange As Range
Dim ws2_xlCell As Range
Dim ws2 As Worksheet
Dim ws3_xlRange As Range
Dim ws3_xlCell As Range
Dim ws3 As Worksheet
Dim valueToFind As String
Dim lastrow As String
'Assign variables to specific worksheets/ranges
'These will need to be updated if changes are made to the file.
Set ws1 = ActiveWorkbook.Worksheets("Refined event data - all")
Set ws1_xlRange = ws1.Range("A1:BJ1")
Set ws2 = Worksheets("Refined event data")
Set ws2_xlRange = ws2.Range("A1:BJ1")
Set ws3 = Worksheets("Refined ID data")
Set ws3_xlRange = ws3.Range("A1:BJ1")
'Loop through all the column headers in the all data tab
For Each ws1_xlCell In ws1_xlRange
valueToFind = ws1_xlCell.Value
'Loop for - Refined event data tab
'check whether column headers match. If so, paste column from event tab to relevant column in all data tab
For Each ws2_xlCell In ws2_xlRange
If ws2_xlCell.Value = valueToFind Then
ws2_xlCell.EntireColumn.Copy
ws1_xlCell.PasteSpecial xlPasteValuesAndNumberFormats
End If
Next ws2_xlCell
'Loop for - Refined MASH data tab
'check whether column headers match. If so, paste column from MASH tab to relevant column in all data tab
'ensure data is pasted below data from previous loop and only pulling data with '0' in col O, 'N' in col Q in MASH tab
For Each ws3_xlCell In ws3_xlRange
If ws3_xlCell.Value = valueToFind Then
ws3_xlCell.End(xlUp).Copy
lastrow = ws1.Cells(rows.Count,"..." ).End(xlUp).Row + 1
Range("..." & lastRow).Select
Selection.PasteSpecial xlPasteValuesAndNumberFormats
End If
Next ws3_xlCell
Next ws1_xlCell
End Sub
遍历第二个数据表(ws3)时出现问题。我需要将这些列粘贴到主表中相关列的下一个空行中。我打算使用lastrow
维来计算此值,但是它需要行/列引用来定义它。不幸的是,列("..."
)被绑在ws1_xlCell
维中,该维随着循环的处理而移动。
是否有一种方法可以从定义为维的单元格中提取列引用?
干杯, 蚂蚁
答案 0 :(得分:0)
感谢@SJR-有关代码的完整行是
lastrow = ws1.Cells(Rows.Count, ws1_xlCell.Column).End(xlUp).Row + 1
Range(ws1_xlCell.Column & lastrow).Select