For loop stops after first iteration in vba? - excel
Believe me or not following code stops copying after first iteration! PLEASE HELP!
Even the NEW AI bot is unable to find issues with below code but for some reason my Excel (Microsoft 365 Latest) is struggling to go past first iteration?
Application.ScreenUpdating = False
Dim FSO As Object
Set FSO = CreateObject("scripting.filesystemobject")
Dim i As Integer
Dim ChkRange As String
Dim Path As String
Dim Path1 As String
Dim Path2 As String
Path1 = "G:\..."
Path2 = "C:\..."
If IsError(Path1) = True Then Path = Path2 Else Path = Path1
Sheets("P C").Select
If Range("J4").Value + 2 = Range("AD4").Value Then GoTo MsgBox
For i = 6 To 21
ChkRange = Range("R" & i).Value + Range("S" & i).Value + Range("T" & i).Value + Range("U" & i).Value + Range("V" & i).Value + Range("W" & i).Value + Range("X" & i).Value + Range("AA" & i).Value
If ChkRange = 0 Or Range("AE" & i).Value = Range("AB26").Value Then GoTo Nexti
Range("AE" & i).Copy
Sheets("Ps").Range("B6").PasteSpecial Paste:=xlPasteValues
If FSO.Folderexists(Path & Sheets("Ps").Range("B6").Value) = False Then
FSO.copyfolder Path & "Template", _
Path & Sheets("Ps").Range("B6").Value
End If
Sheets("E P").Range("A6:F1048576").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("'E P'!Criteria"), Unique:=False
Sheets(Array("E P", "Ps")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=Path & Sheets("Ps").Range("B6").Value & "\Ps\" & Sheets("Ps").Range("K6").Value & ".pdf", OpenAfterPublish:=False
With Worksheets ("DATA")
.Unprotect
.Range("A1:AF1").Copy
.Range("A1").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
.Protect , DrawingObjects:=True, Contents:=True, Scenarios:=True
.Visible = False
End With
RDB_Mail_PDF_Outlook FileNamePDF:=Path & Sheets("Ps").Range("B6").Value & "\Ps\" & Sheets("Ps").Range("K6").Value & ".pdf", _
strTo:=Sheets("Ps").Range("K8").Value, _
strCc:="", _
strBcc:="", _
strSubject:="Re: Ps " & Sheets("Ps").Range("D13").Value, _
Signature:=True, _
Send:=True, _
strBody:="<Body style=font-size:11pt;font-family:Calibri>Hi " & Sheets("Ps").Range("C6").Value & ",<br><br>" & _
"Please find attached " & Sheets("Ps").Range("D13").Value & ".<br><br>" & _
"Let me know if you are having trouble viewing."
Nexti:
Next i
I even tried copying (manually) all data to new fresh spreadsheet... recode everything for hours to fail to debug!
I have unsuccessfully tried elimination process to debug.
Though I witnessed really weired bhaviours during the process of elimination I wouldnt mention it here as it will make no sense! At last after restarting PC I currently have consistant behaviour...
"Range("AE" & i).Copy
Sheets("Ps").Range("B6").PasteSpecial Paste:=xlPasteValues"
within For Loop will only work once, on second iteration it will copy some random number (198.166666666667) which forces the code to crash as it is not the expected value.
Ofcourse, I have checked all Ranges again especially AE6 to AE21.
Related
VBA Macro Vlookup and Index Match function for dynamic number of sheet
I use a macro to run index match and vlookup functions by recording them manually. The problem is that the sheet target is not always available, sometimes only 3 or 5 sheets only so its totally random here's the code that I use Sub Function() Application.AskToUpdateLinks = False Application.DisplayAlerts = False Application.ScreenUpdating = False 'COA Sheets(2).Range("A6").FormulaR1C1 = _ "=IFERROR(INDEX('ACF'!R6C:R2000C,MATCH(RC3,'ACF'!R6C3:R2000C3,0)),IFERROR(INDEX('ACF'!R6C:R2000C,MATCH(RC3,'ACF'!R6C5:R2000C5,0))" & _ ",IFERROR(INDEX('BIGBIKE'!R6C:R2000C,MATCH(RC3,'BIGBIKE'!R6C3:R2000C3,0)),IFERROR(INDEX('BIGBIKE'!R6C:R2000C,MATCH(RC3,'BIGBIKE'!R6C5:R2000C5,0))" & _ ",IFERROR(INDEX('BOD'!R6C:R2000C,MATCH(RC3,'BOD'!R6C3:R2000C3,0)),IFERROR(INDEX('BOD'!R6C:R2000C,MATCH(RC3,'BOD'!R6C5:R2000C5,0))" & _ ",IFERROR(INDEX('CORPSEC'!R6C:R2000C,MATCH(RC3,'CORPSEC'!R6C3:R2000C3,0)),IFERROR(INDEX('CORPSEC'!R6C:R2000C,MATCH(RC3,'CORPSEC'!R6C5:R2000C5,0))" & _ ",IFERROR(INDEX('DIGITAL'!R6C:R2000C,MATCH(RC3,'DIGITAL'!R6C3:R2000C3,0)),IFERROR(INDEX('DIGITAL'!R6C:R2000C,MATCH(RC3,'DIGITAL'!R6C5:R2000C5,0))" & _ ",IFERROR(INDEX('GDG'!R6C:R2000C,MATCH(RC3,'GDG'!R6C3:R2000C3,0)),IFERROR(INDEX('GDG'!R6C:R2000C,MATCH(RC3,'GDG'!R6C5:R2000C5,0))" & _ ",IFERROR(INDEX('HC3'!R6C:R2000C,MATCH(RC3,'HC3'!R6C3:R2000C3,0)),IFERROR(INDEX('HC3'!R6C:R2000C,MATCH(RC3,'HC3'!R6C5:R2000C5,0))" & _ ",IFERROR(INDEX('HRGA'!R6C:R2000C,MATCH(RC3,'HRGA'!R6C3:R2000C3,0)),IFERROR(INDEX('HRGA'!R6C:R2000C,MATCH(RC3,'HRGA'!R6C5:R2000C5,0))" & _ ",IFERROR(INDEX('IT'!R6C:R2000C,MATCH(RC3,'IT'!R6C3:R2000C3,0)),IFERROR(INDEX('IT'!R6C:R2000C,MATCH(RC3,'IT'!R6C5:R2000C5,0))" & _ ",IFERROR(INDEX('LDD'!R6C:R2000C,MATCH(RC3,'LDD'!R6C3:R2000C3,0)),IFERROR(INDEX('LDD'!R6C:R2000C,MATCH(RC3,'LDD'!R6C5:R2000C5,0))" & _ ",IFERROR(INDEX('MCD'!R6C:R2000C,MATCH(RC3,'MCD'!R6C3:R2000C3,0)),IFERROR(INDEX('MCD'!R6C:R2000C,MATCH(RC3,'MCD'!R6C5:R2000C5,0))" & _ ",IFERROR(INDEX('MSD'!R6C:R2000C,MATCH(RC3,'MSD'!R6C3:R2000C3,0)),IFERROR(INDEX('MSD'!R6C:R2000C,MATCH(RC3,'MSD'!R6C5:R2000C5,0))" & _ ",IFERROR(INDEX('NPD'!R6C:R2000C,MATCH(RC3,'NPD'!R6C3:R2000C3,0)),IFERROR(INDEX('NPD'!R6C:R2000C,MATCH(RC3,'NPD'!R6C5:R2000C5,0))" & _ ",IFERROR(INDEX('PARTS'!R6C:R2000C,MATCH(RC3,'PARTS'!R6C3:R2000C3,0)),IFERROR(INDEX('PARTS'!R6C:R2000C,MATCH(RC3,'PARTS'!R6C5:R2000C5,0))" & _ ",IFERROR(INDEX('PURCHASING'!R6C:R2000C,MATCH(RC3,'PURCHASING'!R6C3:R2000C3,0)),IFERROR(INDEX('PURCHASING'!R6C:R2000C,MATCH(RC3,'PURCHASING'!R6C5:R2000C5,0))" & _ ",IFERROR(INDEX('TSD'!R6C:R2000C,MATCH(RC3,'TSD'!R6C3:R2000C3,0)),INDEX('TSD'!R6C:R2000C,MATCH(RC3,'TSD'!R6C5:R2000C5,0))" & _ ")))))))))))))))))))))))))))))))" Application.AskToUpdateLinks = True Application.DisplayAlerts = True Application.AskToUpdateLinks = False Application.DisplayAlerts = False 'Status Sheets(2).Range("D6").FormulaR1C1 = _ "=IFERROR(VLOOKUP(RC3,'ACF'!R6C3:R2000C12,4,0),IFERROR(VLOOKUP(RC3,'ACF'!R6C5:R2000C12,2,0)" & _ ",IFERROR(VLOOKUP(RC3,'BIGBIKE'!R6C3:R2000C12,4,0),IFERROR(VLOOKUP(RC3,'BIGBIKE'!R6C5:R2000C12,2,0)" & _ ",IFERROR(VLOOKUP(RC3,'BOD'!R6C3:R2000C12,4,0),IFERROR(VLOOKUP(RC3,'BOD'!R6C5:R2000C12,2,0)" & _ ",IFERROR(VLOOKUP(RC3,'CORPSEC'!R6C3:R2000C12,4,0),IFERROR(VLOOKUP(RC3,'CORPSEC'!R6C5:R2000C12,2,0)" & _ ",IFERROR(VLOOKUP(RC3,'DIGITAL'!R6C3:R2000C12,4,0),IFERROR(VLOOKUP(RC3,'DIGITAL'!R6C5:R2000C12,2,0)" & _ ",IFERROR(VLOOKUP(RC3,'GDG'!R6C3:R2000C12,4,0),IFERROR(VLOOKUP(RC3,'GDG'!R6C5:R2000C12,2,0)" & _ ",IFERROR(VLOOKUP(RC3,'HC3'!R6C3:R2000C12,4,0),IFERROR(VLOOKUP(RC3,'HC3'!R6C5:R2000C12,2,0)" & _ ",IFERROR(VLOOKUP(RC3,'HRGA'!R6C3:R2000C12,4,0),IFERROR(VLOOKUP(RC3,'HRGA'!R6C5:R2000C12,2,0)" & _ ",IFERROR(VLOOKUP(RC3,'IT'!R6C3:R2000C12,4,0),IFERROR(VLOOKUP(RC3,'IT'!R6C5:R2000C12,2,0)" & _ ",IFERROR(VLOOKUP(RC3,'LDD'!R6C3:R2000C12,4,0),IFERROR(VLOOKUP(RC3,'LDD'!R6C5:R2000C12,2,0)" & _ ",IFERROR(VLOOKUP(RC3,'MCD'!R6C3:R2000C12,4,0),IFERROR(VLOOKUP(RC3,'MCD'!R6C5:R2000C12,2,0)" & _ ",IFERROR(VLOOKUP(RC3,'MSD'!R6C3:R2000C12,4,0),IFERROR(VLOOKUP(RC3,'MSD'!R6C5:R2000C12,2,0)" & _ ",IFERROR(VLOOKUP(RC3,'NPD'!R6C3:R2000C12,4,0),IFERROR(VLOOKUP(RC3,'NPD'!R6C5:R2000C12,2,0)" & _ ",IFERROR(VLOOKUP(RC3,'PARTS'!R6C3:R2000C12,4,0),IFERROR(VLOOKUP(RC3,'PARTS'!R6C5:R2000C12,2,0)" & _ ",IFERROR(VLOOKUP(RC3,'PURCHASING'!R6C3:R2000C12,4,0),IFERROR(VLOOKUP(RC3,'PURCHASING'!R6C5:R2000C12,2,0)" & _ ",IFERROR(VLOOKUP(RC3,'TSD'!R6C3:R2000C12,4,0),VLOOKUP(RC3,'TSD'!R6C5:R2000C12,2,0)" & _ ")))))))))))))))))))))))))))))))" Application.AskToUpdateLinks = True Application.DisplayAlerts = True end sub is there any way to make the formula run only available sheets on the workbook? because if I run it as is, it always asks to update the formula for the sheet that isn't available on the workbook.
You have to iterate over the sheets in your workbook. Sub insertCOAandStatusFormulas() 'Pattern of each formula part - $1 as placeholder for sheetname Dim strPartCOA As String, strPartStatus As String strPartCOA = "IFERROR(INDEX('$1'!R6C:R2000C,MATCH(RC3,'$1'!R6C3:R2000C3,0)),IFERROR(INDEX('$1'!R6C:R2000C,MATCH(RC3,'$1'!R6C5:R2000C5,0)), " strPartStatus = "IFERROR(VLOOKUP(RC3,'$1'!R6C3:R2000C12,4,0),IFERROR(VLOOKUP(RC3,'$1'!R6C5:R2000C12,2,0), " Dim wsTarget As Worksheet Set wsTarget = ThisWorkbook.Worksheets("Tabelle1") '--> adjust this to your needs 'build sheet-specific part per formula Dim strFormulaCOA As String, strFormulaStatus As String Dim cntSheets As Long, i As Long Dim ws As Worksheet For Each ws In ThisWorkbook.Worksheets If ws.Name <> wsTarget.Name Then strFormulaCOA = strFormulaCOA & Replace(strPartCOA, "$1", ws.Name) & vbCrLf strFormulaStatus = strFormulaStatus & Replace(strPartStatus, "$1", ws.Name) & vbCrLf cntSheets = cntSheets + 1 End If Next 'add equal-sign, remove last comma and add closing brackets If LenB(strFormulaCOA) > 0 Then 'XXXX this part is not working XXXX - you would have to find out, how many brackets you have to add and at which position they have to go strFormulaCOA = "=" & Left(strFormulaCOA, Len(strFormulaCOA) - 4) & ", ""no value found""" & String((cntSheets * 2), ")") strFormulaStatus = "=" & Left(strFormulaStatus, Len(strFormulaStatus) - 4) & ", ""no value found""" & String((cntSheets + 2) + 1, ")") With wsTarget .Range("A6").FormulaR1C1 = strFormulaCOA .Range("D6").FormulaR1C1 = strFormulaStatus End With End If End Sub You have to make adjustments regarding the brackets - see comment in code
How do I call a row number by using a variable in VBA?
I recorded a macro, which I want to modify in order to use it automatically in multiple ranges in the Excel worksheet. Here's the code: Sub Macro1() For i = 6 To 22370 Step 5 ActiveWorkbook.SaveAs Filename:= _ "tute.xlsm" _ , FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False Rows(i:i).Select Range("D" & i).Activate Selection.Insert Shift:=xlDown Range("D" & i).Select ActiveCell.FormulaR1C1 = "xyz" Range("A"&"i-1":"C"&"i-1").Select Selection.Copy Range("A" & i).Select ActiveSheet.Paste Range("E" & i).Select Application.CutCopyMode = False Application.CutCopyMode = False ActiveCell.FormulaR1C1 = "=R[-1]C*R[4]C" Range("E" & i).Select Selection.AutoFill Destination:=Range("E37:AO37"), Type:=xlFillDefault Range("E"&i:"AO"&i).Select Range("D" & i).Select Next End Sub I would like to use the variable "i" to call specific rows, as in the case of Rows(i:i).Select or a range such as Range("E"&i:"AO"&i).Select, but I get an error message: "Expected: list separator or )" Can you help pls? Thank you in advance
Besides my comments above, here's a quick rewrite to get rid of all of the superfluous .Activate and .Select lines. Those are for humans, VBA doesn't need to select something before acting on it. It can just act on it directly. Sub Macro1() 'This line shouldn't be in your for loop otherwise you save this workbook like 4000 times ActiveWorkbook.SaveAs Filename:="tute.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False For i = 6 To 22370 Step 5 'No reason to select the row since we just go ahead and activate a particular cell immediately afterwords 'Rows(i:i).Select 'No reason to "Activate" the cell. We can just shift it down without highlighting the thing for the user 'Range("D" & i).Activate Range("D" & i).Insert Shift:=xlDown 'No need to .Select. Just change the formula directly. 'Range("D" & i).Select Range("D" & i).FormulaR1C1 = "xyz" 'Again, no need to .Select. And we can do the copy/paste in one line 'Range("A"&"i-1":"C"&"i-1").Select 'Selection.Copy 'Range("A" & i).Select 'ActiveSheet.Paste Range("A" & i-1 & ":C" & i-1).Copy Destination:=Range("A" & i) 'Removing superfluous select again 'Range("E" & i).Select 'Also superfluous code that isn't needed 'Application.CutCopyMode = False 'Application.CutCopyMode = False Range("E" & i).FormulaR1C1 = "=R[-1]C*R[4]C" 'Range("E" & i).Select Range("E" & i).AutoFill Destination:=Range("E37:AO37"), Type:=xlFillDefault 'Superfluous selects 'Range("E"&i:"AO"&i).Select 'Range("D" & i).Select Next End Sub And then cleaned up to remove all of that: Sub Macro1() ActiveWorkbook.SaveAs Filename:="tute.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False For i = 6 To 22370 Step 5 'Shift column D for this line down a row (add a new empty cell for this line) Range("D" & i).Insert Shift:=xlDown 'Change the value to xyz of column D for this line (the new cell) Range("D" & i).FormulaR1C1 = "xyz" 'Copy three lines in column A:C and paste 1 line down Range("A" & i-1 & ":C" & i-1).Copy Destination:=Range("A" & i) 'Change the formula in column E for this line Range("E" & i).FormulaR1C1 = "=R[-1]C*R[4]C" 'Not sure if this is what you are actually after here. Perhaps that should be `Range("E" & i & ":AO" & i)?` Range("E" & i).AutoFill Destination:=Range("E37:AO37"), Type:=xlFillDefault Next End Sub
Separate some Data
I have an excel macro that can separate and save files per column. My problem is this, the cells from the column I want to be separated cannot be saved as ".xls" if cell has dot "." in the text. For example: DEF. corp File when downloaded in my computer What should I change in my VBA code below to make it work? Dim MyFile, NewFile As Variant Dim sort_data As String Dim last_row, tfiles, start_row, ktr As Long 'Sort data Range(Separate.left_column & Separate.last, Separate.right & last_row).Select Selection.sort Key1:=Range(Separate.sort & Separate.last + 1), Order1:=xlAscending, Header:=True, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom 'Initiate variable tfiles = 0 'Loop through data start_row = Separate.last + 1 For ktr = Separate.last + 1 To last_row 'Identify data sort_data = Trim(UCase(Range(Separate.sort & start_row).Value)) 'End of same data If sort_data <> Trim(UCase(Range(Separate.sort & ktr).Value)) Then 'Copy and paste header Range(Separate.left_column & Separate.first, Separate.right & Separate.last).Copy Workbooks.Add ActiveSheet.Paste NewFile = ActiveWorkbook.Name Windows(MyFile & "xlsx").Activate 'Copy and paste data Range(Separate.left_column & start_row, Separate.right & ktr - 1).Copy Windows(NewFile).Activate Range(Separate.left_column & Separate.last + 1).Select ActiveSheet.Paste 'Bold header rows Rows(Separate.first).Select Selection.Font.Bold = True Range(Separate.left_column & last + 1).Select 'Auto fit Cells.Select Cells.EntireColumn.AutoFit Cells.EntireRow.AutoFit 'Save workbook Range(Separate.left_column & Separate.last + 1).Select new_file = IIf(Separate.current_file = True, MyFile & _ " ", "") & IIf(Len(Trim(Separate.prefix)) > 0, _ Separate.prefix & " ", "") & sort_data & _ IIf(Len(Trim(Separate.suffix)) > 0, _ " " & Separate.suffix, "") '& "xlsx" ActiveWorkbook.SaveAs Filename:=new_file, FileFormat:= _ xlNormal, Password:="", WriteResPassword:="", _ ReadOnlyRecommended:=False, CreateBackup:=False 'Close workbook and return to original ActiveWorkbook.Close tfiles = tfiles + 1 start_row = ktr
Use a replace to modify your string variable new_file to be an underscore _ or something. Underscore is one of the better choices in that it allows for double click full selection of a file name (vs using a dash) and is usually accepted by websites and other file hosting systems. Just add the below line before your ActiveWorkbook.SaveAs procedure. new_file = replace(new_file,".","_")
How can I skip non-numeric values when copy and paste using loop in VBA?
I would like to copy and paste a formula from column P to column C using a loop in VBA. The code should only copy and paste for numeric values in column P and do nothing when cell is blank. Sub TestAll() For i = 10 To 91 Worksheets("Hello").Range("P" & i).Formula = "=" & "MRound(" & Range("C" & i).Value & "+$C$7" & ",0.125)" Next i Application.CutCopyMode = False Range("P10:P91").Select Selection.Copy Range("C10").Select Selection.PasteSpecial Paste:=xlPasteFormulasAndNumberFormats, Operation:= _ xlNone, SkipBlanks:=False, Transpose:=False End Sub
Since you're already using a for loop, you can directly copy the data there. To check if it's a numeric data, you can use the IsNumeric( Expression ) function and the code could be something like this: Sub TestAll() For i = 10 To 91 Worksheets("Hello").Range("P" & i).Formula = "=" & "MRound(" & Range("C" & i).Value & "+$C$7" & ",0.125)" If (IsNumeric(Worksheets("Hello").Range("P" & i).Value)) Then Worksheets("Hello").Range("C" & i).Value = Worksheets("Hello").Range("P" & i).Value End If Next i End Sub Note: Please note that this check is redundant, since the formula will give you always the same result over and over. Hope this help.
Sub TestAll() For i = 10 To 91 If (IsEmpty(Worksheets("Hello").Range("C" & i).Value)) Then Worksheets("Hello").Range("P" & i).Value = "" ElseIf (IsNumeric(Worksheets("Hello").Range("C" & i).Value)) Then Worksheets("Hello").Range("P" & i).Formula = "=" & "MRound(" & Range("C" & i).Value & "+$C$7" & ",0.125)" Else Worksheets("Hello").Range("P" & i).Value = "CALIBRATED" End If Next i Application.CutCopyMode = False Range("P10:P91").Select Selection.Copy Range("C10").Select Selection.PasteSpecial Paste:=xlPasteFormulasAndNumberFormats, Operation:= _ xlNone, SkipBlanks:=False, Transpose:=False End Sub
I want to copy column D:I,K,O,AG,AH,AA but it currently copies D:AH
Sub AutofillData() Dim wkbkSource As Workbook Dim strPath As String Dim myRange As Range Dim i As Integer Dim c As Range Dim wkbkTarget As Workbook Application.ScreenUpdating = False strPath = "\\temp\" Set wkbkA = ThisWorkbook Set wkbkB = Workbooks.Open(strPath & Range("E8").Value) Set myRange = wkbkA.Sheets("Stand-up Request").Range("B13:B25") offs = 0 For Each c In myRange i = c.Value wkbkB.Worksheets("Main Data").Range("D" & i & ":AH" & i).Copy wkbkA.Sheets("Stand-up Request").Range("C13").Offset(offs, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _ SkipBlanks:=True, Transpose:=False Application.CutCopyMode = False offs = offs + 1 On Error Resume Next Next wkbkB.Close savechanges:=False Application.ScreenUpdating = True End Sub
If you don't mind the column order, you should only change one row: wkbkB.Worksheets("Main Data").Range("D" & i & ":I" & i & ",K" & i & ",O" & i & ",AG" & i & ",AH" & i & ",AA" & i).Copy Note that this way AA gets pasted before AG. If you mind the column order, you should split your range and copy/paste them separately: wkbkB.Worksheets("Main Data").Range("D" & i & ":I" & i & ",K" & i & ",O" & i & ",AG" & i & ",AH" & i).Copy wkbkA.Sheets("Stand-up Request").Range("C13").Offset(offs, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _ SkipBlanks:=True, Transpose:=False wkbkB.Worksheets("Main Data").Range("AA" & i).Copy wkbkA.Sheets("Stand-up Request").Range("C13").Offset(offs, 10).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _ SkipBlanks:=True, Transpose:=False Note that this is a static code, if you change the columns to copy, you should also set the column value of the second paste's offset accordingly. If you want a dynamic code, you should create another range object that you will copy and you can get the dimensions of that range. Also it might be better avoiding copy/paste altogether, using code like: wkbkA.Sheets("Stand-up Request").Range("C13").Offset(offs, 0).Resize(1, 6).Value = wkbkB.Worksheets("Main Data").Range("D" & i & ":I" & i).Value But it needs to be done for each sub-range, so it gets more complicated. Also note that your code worked fine for me on ActiveSheet without On Error Resume Next, so you better check your code again :)