简体   繁体   English

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

[英]Selecting a particular row till last column in a loop

I have two workbooks. 我有两个工作簿。 One workbook has the calendar dates(Calendar.xlsm) and the other workbook has only the names(Workingdays.xlsm) of my class students. 一个工作簿具有日历日期(Calendar.xlsm),而另一个工作簿仅具有班级学生的姓名(Workingdays.xlsm)。 What i'm trying to do is to match the names in Workingdays.xlsm to Calendar.xlsx . 我想做的是将Workingdays.xlsm中的名称与Calendar.xlsx进行匹配。 If the match is found then copy the entire row (last filled cell) to Workingdays.xlsm. 如果找到匹配项,则将整行(最后填充的单元格)复制到Workingdays.xlsm。

So far i'm successful in matching the names in the two workbooks but unable to select the entire row for that matched names. 到目前为止,我已经成功地匹配了两个工作簿中的名称,但是无法为匹配的名称选择整行。

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

Failed to copy the entire row (Till last filed cell). 无法复制整行(直到最后提交的单元格)。 Any help would be much appreciated 任何帮助将非常感激

Without more details, I believe you actually want: 没有更多细节,我相信您实际上想要:

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

This is assuming the "calendar_ws" row has 25 columns you want to copy over to "Workdays_ws" starting in column "C". 假设“ calendar_ws”行有25列您要复制到“ Workdays_ws”的列(从“ C”列开始)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM