-
Jul 15th, 2014, 01:13 PM
#1
Thread Starter
Member
vba excel run-time error 1004 Application-defined or object-defined error
Hi guys, so I'm trying to copy several rows of a sheet and paste it on a new one.
Code:
Set rng = ActiveSheet.Range(strLignesSelec) ' This is where I get the error
rng.Copy
At this point, strLignesSelec = "1:1,2:3,15:15,21:21,37:37"
What is weird is that if I press F5 again, it'll work, everything will be copied fine. But the first time, I always get this error.
I was thinking, maybe it didn't work because I have hyperlinks in the range? But that would be weird.
Anyone has an idea what's my mistake here? Thanks.
-
Jul 15th, 2014, 03:57 PM
#2
Re: vba excel run-time error 1004 Application-defined or object-defined error
for any assistance, you would probably need to show the preceding code
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jul 15th, 2014, 04:55 PM
#3
Thread Starter
Member
Re: vba excel run-time error 1004 Application-defined or object-defined error
Okay, I'll post it tomorrow, I don't have access to it now
-
Jul 16th, 2014, 06:53 AM
#4
Re: vba excel run-time error 1004 Application-defined or object-defined error
Maybe like this:
Code:
Dim str As String
Dim rng As Range
str = "1:1" & "," & "2:3" & "," & "15:15" & "," & "21:21" & "," & "37:37"
Set rng = ActiveSheet.Range(str)
-
Jul 16th, 2014, 07:55 AM
#5
Thread Starter
Member
Re: vba excel run-time error 1004 Application-defined or object-defined error
vbfbryce, that's kind of what I do, it's just that when I get the run-time error, my string contains "1:1,2:3,15:15,21:21,37:37", which means the error is not there. Here's more of my code:
Code:
Dim nbLignes As Integer, strLignesSelec As String, longueurI As Integer, rng As Range
Dim contenuCel As String
nbLignes = ActiveSheet.Range("A65536").End(xlUp).Row ' To get the number of lines
strLignesSelec = ""
' I run through every row of my sheet, except the last one and check the value contained in the
' column F. I only want to copy the rows, which its F column value is not 0 or nothing.
For i = 2 To nbLignes - 1
contenuCel = Range("F" & i).Text
If Not Trim(contenuCel) = "0" And Not Trim(contenuCel) = "" Then
If strLignesSelec = "" Then
strLignesSelec = i & ":" & i
Else
longueurI = Len(i)
' If the last character of the string is the antecedent of i, replace it by i. So instead of
' having "2:2,3:3,4:4", I have "2:4".
If Right$(strLignesSelec, longueurI) = i - 1 Then
strLignesSelec = Replace(strLignesSelec, ":" & i - 1, ":" & i)
Else
strLignesSelec = strLignesSelec & "," & i & ":" & i
End If
End If
End If
Next
' I want to copy the column headers row too
strLignesSelec = "1:1," & strLignesSelec
Set rng = ActiveSheet.Range(strLignesSelec)
rng.Copy
' Then I create a new sheet and paste the range copied
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Paste
Anyone sees my mistake? I'm sure it's there, screaming for me to find it.
-
Jul 16th, 2014, 08:06 AM
#6
Re: vba excel run-time error 1004 Application-defined or object-defined error
I don't get an error there, but had to guess at what your data was like. Can you zip and attach a sample of the real data?
-
Jul 16th, 2014, 08:29 AM
#7
Thread Starter
Member
Re: vba excel run-time error 1004 Application-defined or object-defined error
Of course, here it is. test.zip
-
Jul 16th, 2014, 08:33 AM
#8
Thread Starter
Member
Re: vba excel run-time error 1004 Application-defined or object-defined error
By the way, you will see in my code that I do the range thing twice. The reason is that actually, what I want to do, is copy the rows that have 1 or higher in the F column and afterwards, copy the rest of the lines at the end of the sheet.
-
Jul 16th, 2014, 09:11 AM
#9
Re: vba excel run-time error 1004 Application-defined or object-defined error
I ran through your code and got no error when setting the range. Why don't you sort by column F first? Then you wouldn't have to select multiple ranges.
-
Jul 16th, 2014, 09:27 AM
#10
Thread Starter
Member
Re: vba excel run-time error 1004 Application-defined or object-defined error
Why do I get the error and you don't? I sort by column A and then by column F, because I want to see the rows in order of the institution which will have attendance (présences), and then show the rows, always in order of the institution which won't have any attendance.
But anyways, even if I only select one range (which I don't actually, I just copy it), I still get that run-time error that you don't seem to have. My question is why?
-
Jul 16th, 2014, 09:51 AM
#11
Re: vba excel run-time error 1004 Application-defined or object-defined error
Can't explain why you get the error...if you debug at that point, what is the value of "activesheet?" Is it what you'd expect? As far as your sort order goes, you could add a column (hidden or otherwise), that would indicate "attendance - yes / no" and sort first by it, then by institution.
-
Jul 16th, 2014, 10:03 AM
#12
Thread Starter
Member
Re: vba excel run-time error 1004 Application-defined or object-defined error
Yes, the activesheet is correct. What I don't understand is that when I debug it it works. Even when I get the error, if I debug and then press F5, it'll work.
-
Jul 16th, 2014, 10:34 AM
#13
Thread Starter
Member
Re: vba excel run-time error 1004 Application-defined or object-defined error
Okay... So it turns out it's my EmailHyperlink procedure that does something wrong, something I don't understand. If I comment it, the code works fine. Anyone understands why it doesn't work because of this procedure?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|