macro doesn't work

kiril0

New Member
Joined
May 10, 2018
Messages
6
Code:
Sub datatrasfer()


Dim k As Integer
Dim j As Integer
Dim l As Integer
Dim n As Integer
 
 Sheets("sheet6").Activate
 j = 1
 k = j + 1
 l = 9
 n = l + 1
   
   Do Until j = 15000
     If Cells(j, 1).Value = "" And Cells(k, 1).Value <> "" Then
          Sheets(Sheet1).Cells(l, 19).Value = Cells(k, 1).Value
          Sheets(Sheet1).Cells(n, 19).Value = Cells(k, 1).Value
          j = j + 1
          l = l + 2
     Else
          j = j + 1
     End If
   Loop
   
          
End Sub


I want to transfer data from sheet 6 to sheet 1 if the condition in sheet 6 meet the condition in my "if" statements.
I tried with "and" instead of 2 "if" but it return error 1004.
Now it goes to sheet 6 and nothing happens no error no nothing.
Any idea how to solve the problem.
 
Last edited by a moderator:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Possibly:-
Code:
    Sheets[B][COLOR=#FF0000]("Sheet1").[/COLOR][/B]Cells(l, 19).Value = Cells(k, 1).Value
    Sheets[B][COLOR=#FF0000]("Sheet1")[/COLOR][/B].Cells(n, 19).Value = Cells(k, 1).Value
 
Upvote 0
The reason your code doesn't work properly, is because you never change the values of k or n.
Try
Code:
Sub datatrasfer()
   Dim j As Integer
   Dim l As Integer
 
   Sheets("sheet6").Activate
   j = 1
   l = 9
   
   Do Until j = 15000
      If Cells(j, 1).Value = "" And Cells(j + 1, 1).Value <> "" Then
         Sheets("Sheet1").Cells(l, 19).Value = Cells(j + 1, 1).Value
         Sheets("Sheet1").Cells(l + 1, 19).Value = Cells(j + 1, 1).Value
         j = j + 1
         l = l + 2
      Else
         j = j + 1
      End If
   Loop
   
          
End Sub
 
Last edited:
Upvote 0
I think your first problem is that k never changes once you have initialised it .... and (maybe my lack of knowledge) I don't think cells(j,1).value returns anything at all
 
Upvote 0
Can I suggest you never use l (little L) as an index counter it is far too easy to mix it up with 1 (one) , more or less whatever the font
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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