选择特定行直到循环中的最后一列

时间:2019-08-11 17:46:39

标签: excel vba

我有两个工作簿。一个工作簿具有日历日期(Calendar.xlsm),而另一个工作簿仅具有班级学生的姓名(Workingdays.xlsm)。我想做的是将Workingdays.xlsm中的名称与Calendar.xlsx进行匹配。如果找到匹配项,则将整行(最后填充的单元格)复制到Workingdays.xlsm。

到目前为止,我已经成功地匹配了两个工作簿中的名称,但无法为匹配的名称选择整行。

Sub Obtain_days()

' Open Calendar

Dim calendar_wb As Workbook

Dim calendar_ws As Worksheet

Dim Workdays_ws As Worksheet

Set calendar_wb = Workbooks.Open("C:\Users\XXX1\Desktop\Calendar.xlsx")

Set calendar_ws = calendar_wb.Worksheets("Sheet1")

Set Workdays_ws = Workbooks("Workingdays.xlsm").Worksheets("Sheet1")

' obtain dates

Workdays_ws.Activate

last_rw_Workdays = Workdays_ws.Range("A1000000").End(xlUp).Row

last_rw_calendar = calendar_ws.Range("A1000000").End(xlUp).Row

'last_col_calendar = calendar_ws.Range("XFD3").End(xlToLeft).Column

  ' loop through names <-------------Sucessful in matching names

For i = 3 To last_rw_Workdays

 findval = Workdays_ws.Range("A" & i).Value

  For j = 5 To last_rw_calendar

  If calendar_ws.Range("A" & j).Value = findval Then

  'calendar_ws.Range("C" & last_col_calendar).Copy

  calendar_ws.Cells(j, 32).Resize(1, 25).Copy Destination:=Workdays_ws.Cells(i, 3).Resize(1, 2) '<---failed in this step, copying irrelevant cell reference

  'ActiveSheet.Range((last_rw_calendar, 1),(last_rw_calendar, last_col_calendar)).Copy

  Workdays_ws.Activate

  'Workdays_ws.Range("B1000000").End(xlUp).Offset(1, 0).PasteSpecial

  End If

  Next j

Next i

End Sub

无法复制整行(直到最后提交的单元格)。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

没有更多细节,我相信您实际上是想要的:

calendar_ws.Cells(j, 1).Resize(1, 25).Copy Destination:=Workdays_ws.Cells(i, 3)

这是假设“ calendar_ws”行有25列要复制到“ Workdays_ws”的列(从“ C”列开始)。