VBA copy

Damo10

Active Member
Joined
Dec 13, 2010
Messages
460
Hi,

I am trying to get the following code to work when i have another sheet active, it works when i am on the Data sheet but not when I have another selected and how can the copy to destination line be changed to only copy the values and not the formulas?

Regards

Code:
Sub test()
Dim rng As Range
Dim c As Range
Dim dest As Long
Dim check As String
Dim wsto As Worksheet
Dim wsfrom As Worksheet
Set wsto = Sheets("Data")
Set wsfrom = Sheets("Raw")
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
With wsfrom
Set rng = Range(Range("C5"), Range("C" & Rows.Count).End(xlUp))
dest = 2
For Each c In rng
If c.Offset(, -1).Text = "Completed" Then GoTo Finish
If c.Value = Range("A1").Value Then
c.Resize(, 9).Copy Destination:=wsto.Range("B" & dest)
dest = dest + 1
check = c.Text
End If
Finish:
Next c
End With
If check = "" Then MsgBox ("No data found") & Chr(10) & Chr(10) & "Please check machine number" & Chr(10), vbOKOnly + vbExclamation, ""
With Application
.Calculation = xlAutomatic
.ScreenUpdating = True
End With
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi,

You need to set a reference to the workbook as the sheets collection is a member of the workbook class. To get the values only use pastespecial. I have amended the code (untested)...
Code:
Sub test()
Dim rng As Range
Dim c As Range
Dim dest As Long
Dim check As String
Dim wbk As Workbook
Dim wsto As Worksheet
Dim wsfrom As Worksheet
Set wbk = ThisWorkbook
Set wsto = wbk.Sheets("Data")
Set wsfrom = wbk.Sheets("Raw")
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
With wsfrom
Set rng = Range(Range("C5"), Range("C" & Rows.Count).End(xlUp))
dest = 2
For Each c In rng
If c.Offset(, -1).Text = "Completed" Then GoTo Finish
If c.Value = Range("A1").Value Then
c.Resize(, 9).Copy
wsto.Range("B" & dest).PasteSpecial xlPasteValues
Application.CutCopyMode = False
dest = dest + 1
check = c.Text
End If
Finish:
Next c
End With
If check = "" Then MsgBox ("No data found") & Chr(10) & Chr(10) & "Please check machine number" & Chr(10), vbOKOnly + vbExclamation, ""
With Application
.Calculation = xlAutomatic
.ScreenUpdating = True
End With
End Sub

Hope that helps.
 
Upvote 0
Hi,

I have tried your code and the copying values part works fine but it is still not taking the data from the RAW sheet if another sheet is selected, it is still taking the values from the active sheet.

Regards
 
Upvote 0
Sorry missed the unqualified references to the range object. I have amended the code...
Code:
Sub test()
Dim rng As Range
Dim c As Range
Dim dest As Long
Dim check As String
Dim wbk As Workbook
Dim wsto As Worksheet
Dim wsfrom As Worksheet
Set wbk = ThisWorkbook
Set wsto = wbk.Sheets("Data")
Set wsfrom = wbk.Sheets("Raw")
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
With wsfrom
Set rng = .Range(.Range("C5"), .Range("C" & .Rows.Count).End(xlUp))
dest = 2
For Each c In rng
If c.Offset(, -1).Text = "Completed" Then GoTo Finish
If c.Value = .Range("A1").Value Then
c.Resize(, 9).Copy
wsto.Range("B" & dest).PasteSpecial xlPasteValues
Application.CutCopyMode = False
dest = dest + 1
check = c.Text
End If
Finish:
Next c
End With
If check = "" Then MsgBox ("No data found") & Chr(10) & Chr(10) & "Please check machine number" & Chr(10), vbOKOnly + vbExclamation, ""
With Application
.Calculation = xlAutomatic
.ScreenUpdating = True
End With
End Sub
Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,224,519
Messages
6,179,263
Members
452,902
Latest member
Knuddeluff

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