Run-time error '1004': Method 'Rangę' of object '_Global failed

Majlo7

New Member
Joined
Sep 12, 2020
Messages
20
Office Version
  1. 2019
Platform
  1. Windows
I wrote a subroutine to copy different columns from one worksheet and paste to another worksheet. Although the procedure works as expected, every time I execute the procedure I get the message "Runtime Error: 1004". Can anybody help identify the problem that is causing the runtime error in the code below? Thank you very much in advance.


the picture shows the problem and the last one shows what i want to get

when there is "Call listAllFiles" the procedure works badly and when it is not it works fine (good)
when there is "Call listAllFiles", the procedure works badly, and when it is not, everything is fine (good)

VBA Code:
Option Explicit
Dim sheet As Worksheet


Sub listAllFiles()
Sheets("ŚT").Select
Arkusz1_ST.Range("A6:J6").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
ActiveCell.Offset(0, 0).Select

Dim Get_Path As String

With Application.FileDialog(msoFileDialogFolderPicker)

If .Show <> 0 Then
Get_Path = .SelectedItems(1)
End If
Worksheets("ŚT").Cells(2, 3).Value = Get_Path & "\"

End With

Dim objFSO As Scripting.FileSystemObject
Dim objFolder As Scripting.Folder

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Sheets("ŚT").Range("Get_Path").Value)

Call GetFileDetails(objFolder)

End Sub



Function GetFileDetails(objFolder As Scripting.Folder)


Dim objFile As Scripting.File
Dim nextRow As Long
Dim objSubFolder As Scripting.Folder

nextRow = Cells(rows.Count, 1).End(xlUp).row + 1

On Error Resume Next

For Each objFile In objFolder.Files
Cells(nextRow, 1) = objFile.Name
Cells(nextRow, 2) = objFile.Path
Cells(nextRow, 3) = objFile.Type
Cells(nextRow, 4) = "=VLOOKUP(RC[-2],metadaneexport1!R3C2:R34932C7,2,0)"
Cells(nextRow, 4).NumberFormat = "m/d/yyyy h:mm"
Cells(nextRow, 5) = "=VLOOKUP(RC[-3],metadaneexport1!R3C2:R34932C7,3,0)"
Cells(nextRow, 5).NumberFormat = "m/d/yyyy h:mm"
Cells(nextRow, 6) = "=VLOOKUP(RC[-4],metadaneexport1!R3C2:R34932C7,4,0)"
Cells(nextRow, 6).NumberFormat = "m/d/yyyy h:mm"
Cells(nextRow, 7) = "=VLOOKUP(RC[-5],metadaneexport1!R3C2:R34932C7,5,0)"
Cells(nextRow, 7).NumberFormat = "m/d/yyyy h:mm"
Cells(nextRow, 8) = "=VLOOKUP(RC[-6],metadaneexport1!R3C2:R34932C7,6,0)"
Cells(nextRow, 8).NumberFormat = "m/d/yyyy h:mm"
ActiveSheet.Hyperlinks.Add Cells(nextRow, 9), objFile.Path, TextToDisplay:=objFile.Name
Cells(nextRow, 10) = "=HYPERLINK(SUBSTITUTE(RC[-8],""\""&RC[-9],""""))"
nextRow = nextRow + 1
Next

For Each objSubFolder In objFolder.SubFolders
Call GetFileDetails(objSubFolder)
Next
Sheets("ŚT").Select
Arkusz1_ST.Range("D6:H6").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End Function


Sub komendyCall()

Application.ScreenUpdating = False
USkorosztu:

For Each sheet In Application.Worksheets
If sheet.Name = "ŚT" Or sheet.Name = "metadaneexport1" Or sheet.Name = "" Then
If Application.Sheets.Count > 2 Then
Sheets("ŚT").Select
ActiveSheet.Move Before:=Sheets(1)
' Arkusz2.Visible = True
Sheets("metadaneexport1").Visible = True
Sheets("metadaneexport1").Select
ActiveSheet.Move Before:=Sheets(2)
Sheets("metadaneexport1").Visible = False

End If
Else
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Sheets(3).Activate
Sheets(3).Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End If
Next sheet

Dim row, row1, row2 As Long


Call listAllFiles



Proces2:
Sheets("ŚT").Select
'Dim row As String
Dim zakres As String
Range("A5").Select
row1 = Selection.End(xlDown).row
Range("D5").Select
row2 = Selection.End(xlDown).row
zakres = "" & "A5:A" & row1 & ",D5:H" & row2 & ""
Range(zakres).Select

End Sub
 

Attachments

  • 1004.png
    1004.png
    2.4 KB · Views: 17
  • Przechwytywanie.JPG
    Przechwytywanie.JPG
    28.3 KB · Views: 27
  • Przechwytywanie2.JPG
    Przechwytywanie2.JPG
    103.1 KB · Views: 23
Last edited by a moderator:

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Unfortunately, the forum rules won't allow that. You can post the wb directly to this site (maybe?) or provide a link to the wb at a dropbox site. See this site's info to see how to proceed. HTH. Dave


are you still there
 
Upvote 0
Just got home from Easter supper... I'll take a look at your wb. Maybe you should provide an outline of what you are trying to achieve with your code. Dave
 
Upvote 0
Yikes! That's a recorded code nightmare. What language is that? Maybe start from the beginning. What does the wb do. It seems to be a data base of thousands of various file types created by extracting file info from multiple folders. Is this a legit or nefarious endeavor? Dave
 
Upvote 0
Maybe I'll just stick with the query on hand. You can trial this code (untested). I'm not real sure about your sheet naming. Please keep a back up workbook before testing. HTH. Dave
Code:
Sub listAllFiles()
Dim Get_Path As String, LastRow As Long
Dim objfso As Object, objfolder As Object

With Sheets("ŒT")
LastRow = .Cells(.Rows.Count, 1).End(xlUp).row 
.Range("A6:J" & LastRow).ClearContents
End With

With Application.FileDialog(msoFileDialogFolderPicker)
If .Show <> 0 Then
Get_Path = .SelectedItems(1)
End If
Worksheets("ŒT").Cells(2, 3).Value = Get_Path & "\"
'Worksheets("ŒT").Range("Get_Path").Value = Get_Path & "\"
End With

Set objfso = CreateObject("Scripting.FileSystemObject")
Set objfolder = objfso.GetFolder(Sheets("ŒT").Range("Get_Path").Value)
Call getfiledetails(objfolder)
Set objfolder = Nothing
Set objfso = Nothing
End Sub

Function getfiledetails(objfolder As Object)
Dim objfile As Object, NextRow As Long, objsubfolder As Object
With Sheets("ŒT")
NextRow = .Cells(.Rows.Count, 1).End(xlUp).row + 1
For Each objfile In objfolder.Files
.Cells(NextRow, 1) = objfile.Name
.Cells(NextRow, 2) = objfile.Path
.Cells(NextRow, 3) = objfile.Type
.Cells(NextRow, 4) = "=VLOOKUP(RC[-2],metadaneexport1!R3C2:R34932C7,2,0)"
.Cells(NextRow, 4).NumberFormat = "m/d/yyyy h:mm"
.Cells(NextRow, 5) = "=VLOOKUP(RC[-3],metadaneexport1!R3C2:R34932C7,3,0)"
.Cells(NextRow, 5).NumberFormat = "m/d/yyyy h:mm"
.Cells(NextRow, 6) = "=VLOOKUP(RC[-4],metadaneexport1!R3C2:R34932C7,4,0)"
.Cells(NextRow, 6).NumberFormat = "m/d/yyyy h:mm"
.Cells(NextRow, 7) = "=VLOOKUP(RC[-5],metadaneexport1!R3C2:R34932C7,5,0)"
.Cells(NextRow, 7).NumberFormat = "m/d/yyyy h:mm"
.Cells(NextRow, 8) = "=VLOOKUP(RC[-6],metadaneexport1!R3C2:R34932C7,6,0)"
.Cells(NextRow, 8).NumberFormat = "m/d/yyyy h:mm"
.Hyperlinks.Add .Cells(NextRow, 9), objfile.Path, TextToDisplay:=objfile.Name
.Cells(NextRow, 10) = "=HYPERLINK(SUBSTITUTE(RC[-8],""\""&RC[-9],""""))"
NextRow = NextRow + 1
Next objfile
End With
For Each objsubfolder In objfolder.SubFolders
Call getfiledetails(objsubfolder)
Next objsubfolder

Sheets("ŒT").Range("D6:H:" & NextRow).Copy
Sheets("ŒT").Range("D6").PasteSpecial Paste:=xlPasteValues, _
         Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False

Set objsubfolder = Nothing
Set objfile = Nothing
End Function
 
Upvote 0
Maybe I'll just stick with the query on hand. You can trial this code (untested). I'm not real sure about your sheet naming. Please keep a back up workbook before testing. HTH. Dave
Code:
Sub listAllFiles()
Dim Get_Path As String, LastRow As Long
Dim objfso As Object, objfolder As Object

With Sheets("ŒT")
LastRow = .Cells(.Rows.Count, 1).End(xlUp).row 
.Range("A6:J" & LastRow).ClearContents
End With

With Application.FileDialog(msoFileDialogFolderPicker)
If .Show <> 0 Then
Get_Path = .SelectedItems(1)
End If
Worksheets("ŒT").Cells(2, 3).Value = Get_Path & "\"
'Worksheets("ŒT").Range("Get_Path").Value = Get_Path & "\"
End With

Set objfso = CreateObject("Scripting.FileSystemObject")
Set objfolder = objfso.GetFolder(Sheets("ŒT").Range("Get_Path").Value)
Call getfiledetails(objfolder)
Set objfolder = Nothing
Set objfso = Nothing
End Sub

Function getfiledetails(objfolder As Object)
Dim objfile As Object, NextRow As Long, objsubfolder As Object
With Sheets("ŒT")
NextRow = .Cells(.Rows.Count, 1).End(xlUp).row + 1
For Each objfile In objfolder.Files
.Cells(NextRow, 1) = objfile.Name
.Cells(NextRow, 2) = objfile.Path
.Cells(NextRow, 3) = objfile.Type
.Cells(NextRow, 4) = "=VLOOKUP(RC[-2],metadaneexport1!R3C2:R34932C7,2,0)"
.Cells(NextRow, 4).NumberFormat = "m/d/yyyy h:mm"
.Cells(NextRow, 5) = "=VLOOKUP(RC[-3],metadaneexport1!R3C2:R34932C7,3,0)"
.Cells(NextRow, 5).NumberFormat = "m/d/yyyy h:mm"
.Cells(NextRow, 6) = "=VLOOKUP(RC[-4],metadaneexport1!R3C2:R34932C7,4,0)"
.Cells(NextRow, 6).NumberFormat = "m/d/yyyy h:mm"
.Cells(NextRow, 7) = "=VLOOKUP(RC[-5],metadaneexport1!R3C2:R34932C7,5,0)"
.Cells(NextRow, 7).NumberFormat = "m/d/yyyy h:mm"
.Cells(NextRow, 8) = "=VLOOKUP(RC[-6],metadaneexport1!R3C2:R34932C7,6,0)"
.Cells(NextRow, 8).NumberFormat = "m/d/yyyy h:mm"
.Hyperlinks.Add .Cells(NextRow, 9), objfile.Path, TextToDisplay:=objfile.Name
.Cells(NextRow, 10) = "=HYPERLINK(SUBSTITUTE(RC[-8],""\""&RC[-9],""""))"
NextRow = NextRow + 1
Next objfile
End With
For Each objsubfolder In objfolder.SubFolders
Call getfiledetails(objsubfolder)
Next objsubfolder

Sheets("ŒT").Range("D6:H:" & NextRow).Copy
Sheets("ŒT").Range("D6").PasteSpecial Paste:=xlPasteValues, _
         Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False

Set objsubfolder = Nothing
Set objfile = Nothing
End Function


nothing these fixes have changed still the same error
 
Upvote 0
Well I guess we're back to posts #13 & #14. You will have to provide more info about what you are trying to achieve with this wb. It seems that a specific folder is required to test the code so I can't trial it for errors. Before that however, your code won't compile. You have used "row" as a variable which is a specific XL term and should not be used. You have also frequently not set it to any value or not declared it in the sub that is using it. So, in the VBE, where the code is, select Debug and then compile. You will need to change all of those "row" variables (the ones that aren't actually being used correctly) to some other name. You will also have to declare them correctly so that they will be available either locally (within 1 sub) or more broadly so that they will be available to all subs. I'm guessing your error is actually occurring when you call the Process1 sub after completion of the ListAllFiles sub. It starts with...
Code:
Sub Proces1()
Dim row As String
Range("A5").Select
row = Selection.End(xlDown).row
Which is bad because as mentioned "row" has a specific XL meaning and you have also declared it as a string but the Selection.End(xlDown).row returns a value which will cause the same error that you indicated.
Dave
 
Upvote 0
Well I guess we're back to posts #13 & #14. You will have to provide more info about what you are trying to achieve with this wb. It seems that a specific folder is required to test the code so I can't trial it for errors. Before that however, your code won't compile. You have used "row" as a variable which is a specific XL term and should not be used. You have also frequently not set it to any value or not declared it in the sub that is using it. So, in the VBE, where the code is, select Debug and then compile. You will need to change all of those "row" variables (the ones that aren't actually being used correctly) to some other name. You will also have to declare them correctly so that they will be available either locally (within 1 sub) or more broadly so that they will be available to all subs. I'm guessing your error is actually occurring when you call the Process1 sub after completion of the ListAllFiles sub. It starts with...
Code:
Sub Proces1()
Dim row As String
Range("A5").Select
row = Selection.End(xlDown).row
Which is bad because as mentioned "row" has a specific XL meaning and you have also declared it as a string but the Selection.End(xlDown).row returns a value which will cause the same error that you indicated.
Dave
legal because I do not share it with anyone
 
Upvote 0
Well I guess we're back to posts #13 & #14. You will have to provide more info about what you are trying to achieve with this wb. It seems that a specific folder is required to test the code so I can't trial it for errors. Before that however, your code won't compile. You have used "row" as a variable which is a specific XL term and should not be used. You have also frequently not set it to any value or not declared it in the sub that is using it. So, in the VBE, where the code is, select Debug and then compile. You will need to change all of those "row" variables (the ones that aren't actually being used correctly) to some other name. You will also have to declare them correctly so that they will be available either locally (within 1 sub) or more broadly so that they will be available to all subs. I'm guessing your error is actually occurring when you call the Process1 sub after completion of the ListAllFiles sub. It starts with...
Code:
Sub Proces1()
Dim row As String
Range("A5").Select
row = Selection.End(xlDown).row
Which is bad because as mentioned "row" has a specific XL meaning and you have also declared it as a string but the Selection.End(xlDown).row returns a value which will cause the same error that you indicated.
Dave

I have this data, I want my procedure to be performed


Nazwa Pliku Ścieżka Pliku Typ Pliku Data utworzenia Data modyfikacji Utworzenie zawartości Data ostatniego zapisania Data
Alzheimer - Dziadku to ja.avi E:\Świat Telewizji\do selekcji\Filmy (serią,cyklem) odcinków\P11 (Dokupy Filmy długie, podzielone)\a\razem\Alzheimer\Alzheimer - Dziadku to ja.avi VLC media file (.avi) 17.05.2020 00:04 13.06.2015 13:35 13.06.2015 13:35 13.06.2015 13:35 13.06.2015 13:35
Alzheimer - Poszukiwania cz.1.avi E:\Świat Telewizji\do selekcji\Filmy (serią,cyklem) odcinków\P11 (Dokupy Filmy długie, podzielone)\a\razem\Alzheimer\Alzheimer - Poszukiwania cz.1.avi VLC media file (.avi) 17.05.2020 00:04 13.06.2015 13:36 13.06.2015 13:36 13.06.2015 13:36 13.06.2015 13:36
Alzheimer - Poszukiwania cz.2.avi E:\Świat Telewizji\do selekcji\Filmy (serią,cyklem) odcinków\P11 (Dokupy Filmy długie, podzielone)\a\razem\Alzheimer\Alzheimer - Poszukiwania cz.2.avi VLC media file (.avi) 17.05.2020 00:04 13.06.2015 13:37 13.06.2015 13:37 13.06.2015 13:37 13.06.2015 13:37
Alzheimer - Taśmy zapomnianych zdarzeń.avi E:\Świat Telewizji\do selekcji\Filmy (serią,cyklem) odcinków\P11 (Dokupy Filmy długie, podzielone)\a\razem\Alzheimer\Alzheimer - Taśmy zapomnianych zdarzeń.avi VLC media file (.avi) 17.05.2020 00:03 13.06.2015 13:38 13.06.2015 13:38 13.06.2015 13:38 13.06.2015 13:38


Zeszyt1
ABCDEFGHIJ
1Nazwa PlikuŚcieżka PlikuTyp PlikuData utworzeniaData modyfikacjiUtworzenie zawartościData ostatniego zapisaniaData
2Alzheimer - Dziadku to ja.aviE:\Świat Telewizji\do selekcji\Filmy (serią,cyklem) odcinków\P11 (Dokupy Filmy długie, podzielone)\a\razem\Alzheimer\Alzheimer - Dziadku to ja.aviVLC media file (.avi)17.05.2020 00:0413.06.2015 13:3513.06.2015 13:3513.06.2015 13:3513.06.2015 13:35
3Alzheimer - Poszukiwania cz.1.aviE:\Świat Telewizji\do selekcji\Filmy (serią,cyklem) odcinków\P11 (Dokupy Filmy długie, podzielone)\a\razem\Alzheimer\Alzheimer - Poszukiwania cz.1.aviVLC media file (.avi)17.05.2020 00:0413.06.2015 13:3613.06.2015 13:3613.06.2015 13:3613.06.2015 13:36
4Alzheimer - Poszukiwania cz.2.aviE:\Świat Telewizji\do selekcji\Filmy (serią,cyklem) odcinków\P11 (Dokupy Filmy długie, podzielone)\a\razem\Alzheimer\Alzheimer - Poszukiwania cz.2.aviVLC media file (.avi)17.05.2020 00:0413.06.2015 13:3713.06.2015 13:3713.06.2015 13:3713.06.2015 13:37
5Alzheimer - Taśmy zapomnianych zdarzeń.aviE:\Świat Telewizji\do selekcji\Filmy (serią,cyklem) odcinków\P11 (Dokupy Filmy długie, podzielone)\a\razem\Alzheimer\Alzheimer - Taśmy zapomnianych zdarzeń.aviVLC media file (.avi)17.05.2020 00:0313.06.2015 13:3813.06.2015 13:3813.06.2015 13:3813.06.2015 13:38
Arkusz1



Nazwa PlikuŚcieżka PlikuTyp PlikuData utworzeniaData modyfikacjiUtworzenie zawartościData ostatniego zapisaniaData
Alzheimer - Dziadku to ja.aviE:\Świat Telewizji\do selekcji\Filmy (serią,cyklem) odcinków\P11 (Dokupy Filmy długie, podzielone)\a\razem\Alzheimer\Alzheimer - Dziadku to ja.aviVLC media file (.avi)17.05.2020 00:0413.06.2015 13:3513.06.2015 13:3513.06.2015 13:3513.06.2015 13:35
Alzheimer - Poszukiwania cz.1.aviE:\Świat Telewizji\do selekcji\Filmy (serią,cyklem) odcinków\P11 (Dokupy Filmy długie, podzielone)\a\razem\Alzheimer\Alzheimer - Poszukiwania cz.1.aviVLC media file (.avi)17.05.2020 00:0413.06.2015 13:3613.06.2015 13:3613.06.2015 13:3613.06.2015 13:36
Alzheimer - Poszukiwania cz.2.aviE:\Świat Telewizji\do selekcji\Filmy (serią,cyklem) odcinków\P11 (Dokupy Filmy długie, podzielone)\a\razem\Alzheimer\Alzheimer - Poszukiwania cz.2.aviVLC media file (.avi)17.05.2020 00:0413.06.2015 13:3713.06.2015 13:3713.06.2015 13:3713.06.2015 13:37
Alzheimer - Taśmy zapomnianych zdarzeń.aviE:\Świat Telewizji\do selekcji\Filmy (serią,cyklem) odcinków\P11 (Dokupy Filmy długie, podzielone)\a\razem\Alzheimer\Alzheimer - Taśmy zapomnianych zdarzeń.aviVLC media file (.avi)17.05.2020 00:0313.06.2015 13:3813.06.2015 13:3813.06.2015 13:3813.06.2015 13:38


i want to do something like this and here i get the error

Sub test1()
Dim Wier1, Wier2 As String
Range("I6").Select
Wier2 = Cells(Rows.Count, 1).End(xlUp).Row + 1
Wier1 = ActiveCell.Row
Do While Wier1 <> Wier2


ActiveSheet.Hyperlinks.Add Cells(Wier1, 9), Range("B" & Wier1).Value, TextToDisplay:=Range("A" & Wier1).Value
Wier1 = Wier1 + 1
Loop
Range("A6").Select
Range(Range(ActiveCell.Address, Selection.End(xlDown)).Address & ", " & "D5:H" & Selection.End(xlDown).Row).Select



End Sub
 

Attachments

  • Dane.JPG
    Dane.JPG
    75.2 KB · Views: 5
Upvote 0
Code:
Dim Wier1 as Long, Wier2 As Long
This....
Code:
 Cells(Rows.Count, 1).End(xlUp).Row + 1
returns a value which causes an error because you have declared Wier2 as a string. I have no idea what your response "legal because I do not share it with anyone" means? Again, I do not have a good understanding of what broadly you are trying to achieve? Your code has been recorded (which always includes a lot of useless clutter), has no comments and uses a language foreign to me. So, I am unable to further assist you unless you are able to respond clearly to the many requests I have made for further information. Good luck. Dave
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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