Copy data from one sheet to another, provided

sofas

Active Member
Joined
Sep 11, 2022
Messages
468
Office Version
  1. 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

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
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,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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