1

I am facing run time errors 1004 with this piece of code. It is strange because this code worked in another module, but when I placed this in a userform sheet, it doesn't work.

I found the issue occurring on this line when using breakpoints

Set SourceRange = WorkBk.Worksheets(1).Range(Cells(FindRow + 2, FindCol), Cells(LastRow, FindCol)) 

I have researched and tried everything such as

Set SourceRange = WorkBk.Worksheets(1).Range(Cells(FindRow + 2, FindCol)).Resize(LastRow)

Set SourceRange = WorkBk.Worksheets(1).Range(Cells(FindRow + 2, FindCol)).Resize(LastRow, LastCol) 
With WorkBk.Worksheets(1) 
.Range(.Cells(FindRow + 2, FindCol), .Cells(FindRow + 2, FindCol)) 
End With

And none seemed to work. Again this code worked before on another module. I don't know why it's not working when I put in under the command button sub for userform.

Please help

Full Code:

Dim FileName As String 
Dim SummarySheet As Worksheet 
Dim WorkBk As Workbook 
Dim FolderPath As String 
Dim LastRow As Long 
Dim LastCol As Long 
Dim NRow As Long 
Dim NCol As Long 
Dim SourceRange As Range 
Dim DestRange As Range 


' Create a new workbook and set a variable to the first sheet.
Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1) 

' Set Worksheet Name
ActiveSheet.Name = "BTS1 DL_HARQ" 

' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName = Dir(FolderPath & "*BTS1_PHYMAC(DL_HARQ).csv*") 

' Initialize column to 1
NCol = 1 


' Loop until Dir returns an empty string.
Do While FileName <> "" 

' NRow keeps track of where to insert new rows in the destination workbook.
NRow = 1 

' Open a workbook in the folder
Set WorkBk = Workbooks.Open(FolderPath & FileName) 

' Set the cell in row 1 to be the file name.
SummarySheet.Cells(1, NCol) = FileName 

'Find the last row to be copied
LastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row 

'Find the last row to be copied
LastCol = ActiveSheet.Cells(13, Columns.Count).End(xlToLeft).Column 

' Set the source range to be K14 to last row
' Modify this range for your workbooks.
' It can span multiple rows.
' Set SourceRange = WorkBk.Worksheets(1).Range("K14:K" & Lastrow)

Dim rFind As Range 
Dim ColCount As Long 
Dim FindRow As Long 
Dim FindCol As Long 

For ColCount = 1 To LastCol 
With Range(Cells(1, ColCount), Cells(LastRow, ColCount)) 
Set rFind = .Find(What:="Tx Throughput [kbps]", LookIn:=xlValues, LookAt:=xlWhole) 
If Not rFind Is Nothing Then 
FindRow = rFind.Row 
FindCol = rFind.Column 
End If 
End With 
Next ColCount 

Set SourceRange = WorkBk.Worksheets(1).Range(Cells(FindRow + 2, FindCol), Cells(LastRow, FindCol)) 

' Set the destination range to start at row 2 and
' be the same size as the source range.
Set DestRange = SummarySheet.Cells(NRow + 1, NCol) 
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _ 
SourceRange.Columns.Count) 

' Copy over the values from the source to the destination.
DestRange.Value = SourceRange.Value 

' Increase NRow so that we know where to copy data next.
NRow = NRow + DestRange.Rows.Count 

' Close the source workbook without saving changes.
WorkBk.Close savechanges:=False 

' Use Dir to get the next file name.
FileName = Dir() 

' Increase NCol to copy the next file on the next column
NCol = NCol + 1 
Loop 

End Sub
0m3r
  • 12,286
  • 15
  • 35
  • 71
Lalaluye
  • 71
  • 1
  • 9
  • Hardcoding `Set SourceRange = WorkBk.Worksheets(1).Range(Cells(FindRow + 2, FindCol), Cells(LastRow, FindCol)) ` to `Set SourceRange = WorkBk.Worksheets(1).Range("K14:K" & Last Row) ` works though. But that's not what I want. – Lalaluye May 20 '15 at 21:38
  • Error 1004 sometimes means that you've got too large a number for rows or columns. Have you checked the value of the variables `FindRow`, `FindCol` etc? When you use the Cells method it is always a good idea to fully reference it with a workbook and a worksheet. – ChipsLetten May 20 '15 at 21:46
  • They are both 0. I tried to fully reference them by `Set SourceRange = WorkBk.Worksheets(1).Range(WorkBk.Worksheets(1).Cells(FindRow + 2, FindCol), WorkBk.Worksheets(1).Cells(LastRow, FindCol)) ` No luck – Lalaluye May 20 '15 at 22:37
  • Sounds like the Find isn't finding the text. Does the text exist? Try changing this line `With Range(Cells(1, ColCount), Cells(LastRow, ColCount)) ` to be fully referenced. Is this `ActiveSheet` or is it `WorkBk.Worksheets(1)`? Also, why are you looping `For ColCount = 1 To LastCol`? You could just use the Find method on the whole range. Something like `WorkBk.Worksheets(1).Range("A1").CurrentRegion.Find...` – ChipsLetten May 20 '15 at 22:51
  • Also, this `Set rFind = .Find(What:="Tx Throughput [kbps]", LookIn:=xlValues, LookAt:=xlWhole) ` expects to find a cell that only contains the text "Tx Throughput [kbps]" and nothing else. – ChipsLetten May 20 '15 at 22:52
  • Hi I modified the code as you suggested like so: `With Range(WorkBk.Worksheets(1).Cells(1, ColCount), WorkBk.Worksheets(1).Cells(LastRow, ColCount)) Set rFind = WorkBk.Worksheets(1).Range("A1").CurrentRegion.Find(What:="Tx Throughput [kbps]", LookIn:=xlValues, LookAt:=xlWhole) If Not rFind Is Nothing Then FindRow = rFind.Row FindCol = rFind.Column End If End With` But get a 1004 error on line `With Range(WorkBk.Worksheets(1).Cells(1, ColCount), WorkBk.Worksheets(1).Cells(LastRow, ColCount))` – Lalaluye May 20 '15 at 23:13
  • The text definitely exists as I copied it directly from the file. I redid it multiple times to make sure. This code worked on another module but just doesn't now on the userform. (Thanks so much for your help) – Lalaluye May 20 '15 at 23:14
  • Wow! Thanks so much! I used the CurrentRegion method and it worked – Lalaluye May 20 '15 at 23:42

1 Answers1

4

I believe you need to reference your worksheet when using Cells:

Set SourceRange = WorkBk.Worksheets(1).Range(WorkBk.Worksheets(1).Cells(FindRow + 2, FindCol), WorkBk.Worksheets(1).Cells(LastRow, FindCol))  

It will help a lot if you create a variable for this worksheet, will make it much easier to read and modify.

TMH8885
  • 888
  • 6
  • 15
  • I tried this without luck. I believe the error is in FindRow and FindCol because they are both assigned 0 when the error occurs. They shouldn't be 0 since the text exists in the sheet. I'm not sure why though... – Lalaluye May 20 '15 at 22:34
  • Based on your code, I would guess it's because it's not finding the string you specified: "Tx Throughput [kbps]" – TMH8885 May 21 '15 at 13:36