Copy Destination VBA Error

akjohno

New Member
Joined
Mar 27, 2018
Messages
27
Office Version
  1. 365
Platform
  1. Windows
I am copying the formulas from a range of cells and pasting them in the cells below for a range that is calculated from a CountA value as per the below code. The trouble is that the paste stops in random places and doesn't complete to the correct number of rows. This has had me scratching my head for over a week now but I still can't see where my mistake is. Can someone possibly enlighten me? The copy range is 22 cells that I need to paste down for example 30000 rows below, so the same formula copies down from row. My macro might cut out at row 7251 in one instance, then I run the macro again and it cuts out at 7885 or just in random spots like that, even though my CountA number is 30000. Please help? . Numshots is my CountA value....

VBA Code:
pastearea = "G5:AF" & NumShots + 3              'Assigns the variable pastearea the value of the cells we need to paste to
    Range("G4:AF4").Copy Destination:=ActiveSheet.Range(pastearea)  'Copies the formulas from cells G4 to AF4 to the entire range needed
    Do While Workbooks(wbname).Worksheets("Sheet1").Range("AF" & NumShots + 3) = ""
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Might pay to post ALL of the code, as the error may not be in the area that you think ??
 
Upvote 0
Might pay to post ALL of the code, as the error may not be in the area that you think ??
VBA Code:
Sub CATAN_Convert()

' Conversion Macro
'Variable Declarations
Dim NumShots As Long
Dim wbname As String, wsname As String, fullpath As String, sp As String, coords2 As String
Dim name1 As String, name2 As String, pastearea As String, pastearea2 As String, coords As String
Dim wsnew As Worksheet
Dim a As Variant
Dim i As Long
'Stop screen from flickering while windows change
Application.ScreenUpdating = False

' Asks you where the file you want to convert is located
With Application.FileDialog(msoFileDialogFilePicker) 'Start of picking your file
.AllowMultiSelect = False 'Allows you to only open one file
.Filters.Add "Text Files", "*.dat", 1 'Looks only for .dat files
.Show 'Opens the File Dialog Box
fullpath = .SelectedItems.Item(1) 'Assigns the location of the file to the variable "fullpath"
End With 'Exits the search function

If Right(fullpath, 3) <> "dat" Then 'Error trap in case you don't select a dat file
MsgBox ("You need to select a .dat file!") 'Message box to advise user that a dat file wasn't selected
End If 'End of picking your file

'Assigning variable fullpath to full address of file and imports file data into the dat file correctly delimited
Workbooks.OpenText Filename:= _
fullpath, _
Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, Semicolon:=False, _
Comma:=False, Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), _
Array(2, 1), Array(3, 1), Array(4, 1)), TrailingMinusNumbers:=True

wbname = ActiveWorkbook.Name 'Assigns variable wbname to the name of this workbook
wsname = ActiveSheet.Name 'Assigns variable wsname the name of this worksheet
NumShots = Application.WorksheetFunction.CountA(Range("A:A")) 'Counts the number of survey points in the file
Set wsnew = Sheets.Add(After:=Sheets(wsname)) 'Adds a new worksheet

'Copies the co-ordinate converter section of the main file to this dat file
Windows("CATAN Converter.xlsm").Activate 'Activates main workbook
Sheets("Sheet2").UsedRange.Copy Destination:=Workbooks(wbname).Worksheets("Sheet1").Range("A1") 'Copies conversion data over
Windows(wbname).Activate 'Goes back and selects our new dat workbook
pastearea = "G5:AF" & NumShots + 3 'Assigns the variable pastearea the value of the cells we need to paste to
Range("G4:AF4").Copy Destination:=ActiveSheet.Range(pastearea) 'Copies the formulas from cells G4 to AF4
Do While Workbooks(wbname).Worksheets("Sheet1").Range("AF" & NumShots + 3) = ""
Loop
ActiveWorkbook.Worksheets(1).Activate 'Selects the first worksheet in the workbook
pastearea2 = "A1:B" & NumShots 'Assigns the variable pastearea2 the value of all the cells we need to copy
'Sheets("Sheet1").Range("D4:E" & NumShots + 3).Value = Range(pastearea2).Value 'Copies co-ordinates to conversion sheet
Sheets(wsname).Range(pastearea2).Copy Destination:=Worksheets("Sheet1").Range("D4")
Do While Worksheets(wsname).Range("B" & NumShots) = ""
Loop
Sheets("Sheet1").Select 'Selects our co-ordinate transformation sheet1
Range("A1").Select 'Selects cell A1 to prevent any confusion

'Copy converted co-ordinates back to final sheet
coords = "AD4:AE" & NumShots + 3 'Assigns the variable coords the value of all the converted co-ordinate cells we need to copy
With Sheets("Sheet1").Range(coords)
Worksheets(wsname).Range(pastearea2).Value = .Value
End With

Range("A1").Select 'Selects cell A1 to prevent any confusion
Sheets("Sheet1").Select 'Selects Sheet1
Application.DisplayAlerts = False 'Turns off Display of box asking to accept sheet deletion
ActiveWindow.SelectedSheets.Delete 'Deletes Sheet1
Application.DisplayAlerts = True 'Turns display alerts back on

' Converts Feature Codes to CATAN usable format
With Range("D1", Range("D" & Rows.Count).End(xlUp))
a = .Value
For i = 1 To UBound(a)
Select Case a(i, 1)
Case 700: a(i, 1) = vbNullString
Case 400: a(i, 1) = "%po"
Case Else: a(i, 1) = "%sp"
End Select
Next i
.Value = a
End With

'Saving of new file for CATAN
name1 = InStrRev(fullpath, ".") 'Counts the number of characters in front of the . in the file name
name2 = Left(fullpath, name1) 'Grabs the name of the file using the character count above
'Uses the filename from above and puts csv after it so that it saves to the same location as a different file type
ActiveWorkbook.SaveAs Filename:= _
name2 & "csv", FileFormat:=xlCSV, CreateBackup:=False
MsgBox "Created " & name2 & "csv for use in CATAN." & vbNewLine & "File location same as original file location" 'Advises user the file location
ActiveWorkbook.Close 'Closes this file
Workbooks("CATAN Converter.xlsm").Close savechanges:=False 'Closes the Master workbook
Exit Sub

End Sub
 
Upvote 0
First off I'd change the CountA function to only embrace the used cells in "A:A"
VBA Code:
dim lr as long
lr = wsname.cells(rows.count,"A").end(xlup).row
NumShots = Application.WorksheetFunction.CountA(Range("A1:A" & lr))
 
Upvote 0
Which line of code causes the crash, when it crashes?
 
Upvote 0
Which line of code causes the crash, when it crashes?
it gets to the area below and as it doesn't paste all the values is just gets hung up in a loop (Do While) because it is looking for all the cells to be populated in the range area
VBA Code:
pastearea = "G5:AF" & NumShots + 3 'Assigns the variable pastearea the value of the cells we need to paste to
Range("G4:AF4").Copy Destination:=ActiveSheet.Range(pastearea) 'Copies the formulas from cells G4 to AF4
Do While Workbooks(wbname).Worksheets("Sheet1").Range("AF" & NumShots + 3) = ""
 
Upvote 0
Ok, if I understand correctly you are getting stuck in this empty loop?

VBA Code:
Do While Workbooks(wbname).Worksheets("Sheet1").Range("AF" & NumShots + 3) = ""
Loop

I'm guessing that it works fine until for whatever reason, the row of data you copy has an empty last (AF) cell. What is the purpose of this loop? It does not appear to have a function other than to cause trouble. If you need to test the value of Worksheets("Sheet1").Range("AF" & NumShots + 3) and take action there are better ways.
 
Upvote 0
Ok, if I understand correctly you are getting stuck in this empty loop?

VBA Code:
Do While Workbooks(wbname).Worksheets("Sheet1").Range("AF" & NumShots + 3) = ""
Loop

I'm guessing that it works fine until for whatever reason, the row of data you copy has an empty last (AF) cell. What is the purpose of this loop? It does not appear to have a function other than to cause trouble. If you need to test the value of Worksheets("Sheet1").Range("AF" & NumShots + 3) and take action there are better ways.
At first I thought the error was that the macro was moving to the next step before the computer had time to finish pasting all the values, so I put that loop in to make sure it didn't move on until all the cells where populated. Once I get the code running correct it can be removed. What I am currently getting is the macro stops pasting at say cell AF7500 instead of stopping pasting at cell AF30000, even though the range says stop at cell AF30000. Does that make sense, I am not sure I am doing a good job of explaining:) It is stopping the paste side of the Copy/Destination way too early for some unknown reason.
 
Upvote 0
Did you check the lr value based on post #4
 
Upvote 0
Did you check the lr value based on post #4
So in the example I am running now, lr value is 29449; NumShots is 29449; pastearea is G5:AF29452 (cause I add 3 to it) and this time it has stopped pasting at cell M7409. I now get a Run-time error '1004': Copy Method of Range Class Failed too.
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,822
Members
449,469
Latest member
Kingwi11y

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top