Copy data from one sheet to another, provided

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
559
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Hello. I tried to create this code, but it is very slow. I do not know why. Can you help me to solve this problem or replace it with another one that does the same task?


VBA Code:
Sub data()

Set WS = Worksheets("SHEET1")
Set WS1 = Worksheets("SHEET2")
Application.ScreenUpdating = False



On Error Resume Next
If WS.Range("D5").Value = "" Then: Exit Sub


Dim Lr As Long
Dim Mh As Long
Dim iCont As Integer
Dim r As Integer
Dim c As Integer
Dim ii As Double

ii = WS.Range("D5").Value

With WS1
    Lr = .Cells(.Rows.Count, "b").End(xlUp).Row + 1
    Mh = WorksheetFunction.Match(ii, .Range("D5:D" & Lr), 0) + 8
    iCont = WorksheetFunction.CountIf(.Range("D5:D" & Lr), ii)
End With
X = 3
For c = 2 To 2
WS.Cells(X, 4) = WS1.Cells(Mh, c).Value
WS.Cells(X + 1, 4) = WS1.Cells(Mh, c + 1).Value
WS.Cells(X + 3, 4) = WS1.Cells(Mh, c + 3).Value

WS.Cells(X + 1, 6) = WS1.Cells(Mh, c + 15).Value
WS.Cells(X + 3, 6) = WS1.Cells(Mh, c + 17).Value
WS.Cells(X + 2, 6) = WS1.Cells(Mh, c + 16).Value
WS.Cells(3, 6) = WS1.Cells(Mh, c + 14).Value

WS.Cells(3, 2) = WS1.Cells(Mh, c + 10).Value
WS.Cells(4, 2) = WS1.Cells(Mh, c + 11).Value
WS.Cells(5, 2) = WS1.Cells(Mh, c + 12).Value
WS.Cells(6, 2) = WS1.Cells(Mh, c + 13).Value

X = X + 1
Next
Application.ScreenUpdating = False
For r = 1 To iCont
       For c = 1 To 5
      WS.Cells(r + 8, c + 1) = WS1.Range("F" & Mh).Cells(r, c).Value
 Next
  
 Next
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hello. I tried to create this code, but it is very slow. I do not know why. Can you help me to solve this problem or replace it with another one that does the same task?


VBA Code:
Sub data()

Set WS = Worksheets("SHEET1")
Set WS1 = Worksheets("SHEET2")
Application.ScreenUpdating = False



On Error Resume Next
If WS.Range("D5").Value = "" Then: Exit Sub


Dim Lr As Long
Dim Mh As Long
Dim iCont As Integer
Dim r As Integer
Dim c As Integer
Dim ii As Double

ii = WS.Range("D5").Value

With WS1
    Lr = .Cells(.Rows.Count, "b").End(xlUp).Row + 1
    Mh = WorksheetFunction.Match(ii, .Range("D5:D" & Lr), 0) + 8
    iCont = WorksheetFunction.CountIf(.Range("D5:D" & Lr), ii)
End With
X = 3
For c = 2 To 2
WS.Cells(X, 4) = WS1.Cells(Mh, c).Value
WS.Cells(X + 1, 4) = WS1.Cells(Mh, c + 1).Value
WS.Cells(X + 3, 4) = WS1.Cells(Mh, c + 3).Value

WS.Cells(X + 1, 6) = WS1.Cells(Mh, c + 15).Value
WS.Cells(X + 3, 6) = WS1.Cells(Mh, c + 17).Value
WS.Cells(X + 2, 6) = WS1.Cells(Mh, c + 16).Value
WS.Cells(3, 6) = WS1.Cells(Mh, c + 14).Value

WS.Cells(3, 2) = WS1.Cells(Mh, c + 10).Value
WS.Cells(4, 2) = WS1.Cells(Mh, c + 11).Value
WS.Cells(5, 2) = WS1.Cells(Mh, c + 12).Value
WS.Cells(6, 2) = WS1.Cells(Mh, c + 13).Value

X = X + 1
Next
Application.ScreenUpdating = False
For r = 1 To iCont
       For c = 1 To 5
      WS.Cells(r + 8, c + 1) = WS1.Range("F" & Mh).Cells(r, c).Value
 Next
 
 Next
Application.ScreenUpdating = True
End Sub
Hello.Is there a solution to my problem
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,787
Members
452,942
Latest member
VijayNewtoExcel

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