I am consolidating the daily report sent by my team in a single file named as "Master file" it will have each sheet separately for each of my team members. i need to find the cell contains today's date in a report sent by my team member and copy the corresponding cells and paste it in the "Master file" Here is the code
Sub Copy_data()
Sheets("Daily Report").Select
Range("A7").Select
Dim mydate As Date
mydate = Range("B1")
For i = 1 To 4 'this is sample actually i have 38 sheets
Dim filename As Variant
ActiveCell.Offset(1, 0).Select
filename = ActiveCell.Value
Workbooks.Open "C:\Users\test\Desktop\AP\" & filename
Application.Wait (Now + TimeValue("0:00:02"))
Sheets("Dashboard").Select
Cells.Find(What:=mydate, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate ' this is where i get an error as "object variable or with block variable not set"
ActiveCell.Offset(0, 2).Select
Dim currentcell As Integer
currentcell = ActiveCell.Row
Range(Selection, Cells(currentcell, 10)).Copy
Windows("Agent Performance.xls").Activate
Dim sheetname As String
sheetname = ActiveCell.Offset(0, 1).Value
Sheets(sheetname).Select
'Here again i have to find the cell with today's date and paste the data which i copied
Next i
End Sub
Note :- It was working fine in the earlier stage. After making few changes in the format and appearance, also added all the sheets in the "master file" after then i am getting this error !! Also i am beginner to VBA, kindly pardon me for any flaws.
Going out on a limb, I tried to fix up your code and avoid all the .Select
/ .Activate
, which can cause some headaches.
In your OP I don't see where you go to paste, so made an educated guess at the end, and noted as such.
Step through this with F8
to make sure it's working properly, as you can then follow it one line at a time.
Sub Copy_data()
Dim newWB As Workbook, currentWB As Workbook, agentWB As Workbook
Dim dailyWS As Worksheet, dashWS As Worksheet
Dim i As Long
Dim foundCell As Range
Dim currentcell As Integer
Dim destSheetname As String
Set currentWB = ThisWorkbook
Set dailyWS = currentWB.Sheets("Daily Report")
Dim mydate As Date
mydate = dailyWS.Range("B1")
For i = 1 To 4 'this is sample actually i have 38 sheets
Dim filename As Variant
filename = dailyWS.Range("A7").Offset(1, 0).Value
Set newWB = Workbooks.Open("C:\Users\test\Desktop\AP\" & filename)
Application.Wait (Now + TimeValue("0:00:02"))
Set dashWS = newWB.Sheets("Dashboard")
Set foundCell = dashWS.Cells.Find(What:=mydate, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
currentcell = foundCell.Offset(0, 2).Row
dashWS.Range(foundCell.Offset(0, 2), dashWS.Cells(currentcell, 10)).Copy
Set agentWB = Workbooks("Agent Performance.xls")
destSheetname = agentWB.Sheets(ActiveSheet).Range("A1").Offset(0, 1).Value 'Do you know the activesheet name? If so use it here instead.
agentWB.Sheets(destSheetname).Activate
''' Is this where you want to paste??
agentWB.Sheets(destSheetname).Range("A1").Paste
'Here again i have to find the cell with today's date and paste the data which i copied
Next i
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.