excel macro why don't it work

kiril0

New Member
Joined
May 10, 2018
Messages
6
Sub datatransfer()


Dim j As Integer
Dim k As Integer
Dim n As Integer




Sheets("Master").Activate
sheetcounter = 2
k = 24
j = 1


Do


Do Until Cells(j, 1).Value = Worksheets(sheetcounter).Cells(15, 5).Value
j = j + 1
Loop


n = j + 1
Do
Worksheets(sheetcounter).Cells(k, 4).Value = Cells(n, 3).Value
Worksheets(sheetcounter).Cells(k, 2).Value = Cells(n, 2).Value
If Cells(n, 8).Value = "" Then
Worksheets(sheetcounter).Cells(k, 31).Value = Cells(n, 6).Value
Else
Worksheets(sheetcounter).Cells(k, 30).Value = Cells(n, 8).Value
Worksheets(sheetcounter).Cells(k, 31).Value = Cells(n, 6).Value

End If
k = k + 1
n = n + 1
Loop While Cells(n, 2).Value <> ""


sheetcounter = sheetcounter + 1
Loop Until sheetcounter = 500
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
It would help to know why it "don't work".

What is it doing, if anything? Is it doing part of what you expect, and then stopping?....or is it doing part of what you expect and then doing something wrong?

One thing I can see as a possibility is "sheetcounter". If that is a string (you don't dimension that one, so it could be), I'm not sure "WorkSheets(sheetcounter)" will do what you expect. And I'm also not overly familiar with the usage of "Do...Loop", but I don't think "Loop While" is the syntax you're wanting. Maybe something like:

Code:
While
........
.......
.......
Wend

I think. But like I said, don't take my work for that part as I'm just not that familiar.
 
Upvote 0
I whan to transfer data from sheet "master" to multiple sheets base on the value of Cells(15, 5) in the resiving and the value of cell in the "master" sheet.
The master sheet contains several tables but for each table the content of cell(j,1) is uniquely.
I get run time error 9 for the line

Do Until Cells(j, 1).Value = Worksheets(sheetcounter).Cells(15, 5).Value
 
Upvote 0
How many sheets do you have in your workbook ? Do you have 500 or more because the "sheetcounter" loops until 500 which I think is the cause of the error you are getting in case you have less than 500 sheets

Code:
Loop Until sheetcounter = 500
 
Upvote 0
How many sheets do you have in your workbook ? Do you have 500 or more because the "sheetcounter" loops until 500 which I think is the cause of the error you are getting in case you have less than 500 sheets

Code:
Loop Until sheetcounter = 500


Thanks
the sheets are 494
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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