Can I split&write data to each individual sheet?

J

Jack

Yes the test macro seems to work because there is no error message for the
"all_data.xls" file. But the other file "Reports.xls" which contains the
general macro and is to retrive data from "all_data.xls" ...when this is
runned it generates the "subscript out of range" error message.
 
D

Dave Peterson

Remember that all_data.xls has to be open before you start the macro. Is it?

This version will yell at you if you don't have it open (remember to change the
sheet name to what you want).

Option Explicit
Sub Tester1()
Dim wb1 As Workbook
Dim wk1 As Worksheet, bk2 As Workbook
Dim sh As Worksheet, cell As Range, rng As Range
Dim rng1 As Range, res As Variant

Set wb1 = Nothing
On Error Resume Next
Set wb1 = Workbooks("all_data.xls")
On Error GoTo 0
If wb1 Is Nothing Then
MsgBox "all_data.xls isn't open!"
Exit Sub
End If

Set wk1 = Nothing
On Error Resume Next
Set wk1 = wb1.Worksheets("sheet1")
On Error GoTo 0
If wk1 Is Nothing Then
MsgBox "all_data.xls doesn't have that sheet"
Exit Sub
End If

Set bk2 = Workbooks("Reports.xls")
Set rng = wk1.Range(wk1.Cells(2, 1), wk1.Cells(2, 1).End(xlDown))
For Each cell In rng
Set sh = bk2.Worksheets(cell.Offset(0, 1).Value)
Set rng1 = sh.Range(sh.Cells(2, "H"), sh.Cells(2, "H").End(xlDown))
res = Application.Match(CLng(cell.Offset(0, 7)), rng1, 0)
If IsError(res) Then
cell.EntireRow.Copy _
Destination:=rng1.Offset( _
rng1.Rows.Count, 0).Resize(1, 1).EntireRow.Cells(1)
End If
Next cell

End Sub


Yes the test macro seems to work because there is no error message for the
"all_data.xls" file. But the other file "Reports.xls" which contains the
general macro and is to retrive data from "all_data.xls" ...when this is
runned it generates the "subscript out of range" error message.
 
J

Jack

Hi Dave,
I know I'm taking too much time of yours. Sorry for beeing unsuccessfull
again. I have changed the sheet name to the one I am using on all_data.xls
and It looks like I am opening the correct workbook and worksheet allright
but keep getting the error message at the command line:
Set sh = bk2.Worksheets(cell.Offset(0, 1).Value)
The problem may reside on the design/formats of the worksheets/cells I am
using thus may I suggest that I send you the files again please? (if you
kindly will provide me a valid email add.)
Thank you
Jack
 
D

Dave Peterson

This sounds like a problem that can be resolved in the newsgroup.

Did you try adding the validation checks into your code?
 
J

Jack

No, I do not know how to do that.


Dave Peterson said:
This sounds like a problem that can be resolved in the newsgroup.

Did you try adding the validation checks into your code?
 
D

Dave Peterson

This is the portion of code (from a few posts ago) that I suggested:

Set wb1 = Nothing
On Error Resume Next
Set wb1 = Workbooks("all_data.xls")
On Error GoTo 0
If wb1 Is Nothing Then
MsgBox "all_data.xls isn't open!"
Exit Sub
End If

Set wk1 = Nothing
On Error Resume Next
Set wk1 = wb1.Worksheets("sheet1")
On Error GoTo 0
If wk1 Is Nothing Then
MsgBox "all_data.xls doesn't have that sheet"
Exit Sub
End If

If you have spelling mistakes or the workbook isn't open, then you'll get a
message box to pop up.

You could go back to the other post and see it in its entirety.


No, I do not know how to do that.
<<snipped>>
 
H

Haldun Alay

Hi,

I guess, One more validation check has to be added to your code as shown below.


.......
.......
For Each cell In rng
on error resume next
Set sh = bk2.Worksheets(cell.Offset(0, 1).Value)
if sh is nothing then
msgbox "There is no sheet called " & cell.Offset(0, 1).Value & in bk2.name
exit sub
end if

.......
.......


Kind regards.





--
Haldun Alay
"Dave Peterson" <[email protected]>, iletide sunu yazdi This is the portion of code (from a few posts ago) that I suggested:

Set wb1 = Nothing
On Error Resume Next
Set wb1 = Workbooks("all_data.xls")
On Error GoTo 0
If wb1 Is Nothing Then
MsgBox "all_data.xls isn't open!"
Exit Sub
End If

Set wk1 = Nothing
On Error Resume Next
Set wk1 = wb1.Worksheets("sheet1")
On Error GoTo 0
If wk1 Is Nothing Then
MsgBox "all_data.xls doesn't have that sheet"
Exit Sub
End If

If you have spelling mistakes or the workbook isn't open, then you'll get a
message box to pop up.

You could go back to the other post and see it in its entirety.


No, I do not know how to do that.
<<snipped>>
 
H

Haldun Alay

Oppps, I missed up something. sorry

The code will be like following.

.......
.......
For Each cell In rng
on error resume next
Set sh = bk2.Worksheets(cell.Offset(0, 1).Value)
if sh is nothing then
msgbox "There is no sheet called " & cell.Offset(0, 1).Value & " in " & bk2.name
exit sub
end if

.......
.......

kind regards.
--
Haldun Alay



"Haldun Alay" <haldunalayATyahooDOTcom>, iletide sunu yazdi Hi,

I guess, One more validation check has to be added to your code as shown below.


......
......
For Each cell In rng
on error resume next
Set sh = bk2.Worksheets(cell.Offset(0, 1).Value)
if sh is nothing then
msgbox "There is no sheet called " & cell.Offset(0, 1).Value & in bk2.name
exit sub
end if

......
......


Kind regards.





--
Haldun Alay
"Dave Peterson" <[email protected]>, iletide sunu yazdi This is the portion of code (from a few posts ago) that I suggested:

Set wb1 = Nothing
On Error Resume Next
Set wb1 = Workbooks("all_data.xls")
On Error GoTo 0
If wb1 Is Nothing Then
MsgBox "all_data.xls isn't open!"
Exit Sub
End If

Set wk1 = Nothing
On Error Resume Next
Set wk1 = wb1.Worksheets("sheet1")
On Error GoTo 0
If wk1 Is Nothing Then
MsgBox "all_data.xls doesn't have that sheet"
Exit Sub
End If

If you have spelling mistakes or the workbook isn't open, then you'll get a
message box to pop up.

You could go back to the other post and see it in its entirety.


No, I do not know how to do that.
<<snipped>>
 
J

Jack

Hi Dave,
I have already tried "all" portions of your code presented on this thread.
As I mentioned earlier, I am not getting any error messages regarding to the
"workbook beeing not opened" or "not having that spesific worksheet". The
"subscript out of range error" is displayed on the:

Set sh = bk2.Worksheets(cell.Offset(0, 1).Value)

line of your code, which I beleive is down passed the control lines you are
reminding.
Hope you & other newsgroup members can help me find out the reason and cure
the problem.
Sincerely
Jack
 
D

Dave Peterson

I'm sorry, I'm out of guesses.


Hi Dave,
I have already tried "all" portions of your code presented on this thread.
As I mentioned earlier, I am not getting any error messages regarding to the
"workbook beeing not opened" or "not having that spesific worksheet". The
"subscript out of range error" is displayed on the:

Set sh = bk2.Worksheets(cell.Offset(0, 1).Value)

line of your code, which I beleive is down passed the control lines you are
reminding.
Hope you & other newsgroup members can help me find out the reason and cure
the problem.
Sincerely
Jack
 
J

Jack

Not many people were interested in solving this problem...
But thank you Dave, you tried to be helpful a lot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top