help with copying value and not formula

will1128

Board Regular
Joined
Aug 6, 2010
Messages
70
I'm trying to copy the value of a formula and not the formula from one worksheet to another if and only if the cell is found. I've been able to copy the the column, but I get a #REF error on the 2nd worksheet.

Here's the code:
<code>
Public Function copy_col_to_new_sheet(Col As String) As Range
'Dim Col As String
Dim FoundCell As Range
FindString = Col 'value sent in from the function

If Trim(FindString) <> "" Then
With ActiveWorkbook.Sheets(1).Range("A1:IV65536")
'With ThisWorkbook.Sheets(1).Range("A1:IV65536")
Set FoundCell = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
'if my cell is found copy to 2nd worksheet
If Not FoundCell Is Nothing Then
Application.Goto FoundCell, True
MsgBox FoundCell.Address & FoundCell.Text & " " & "Found in Copy_Col" 'returns cell address
'MsgBox FoundCell.Columns & " " & "Columns" 'returns the column number as a Long
FoundCell.Cells.EntireColumn.Copy ActiveWorkbook.Sheets(2).Columns(FoundCell.Column) 'this works
Else
MsgBox "Nothing found Copy Col To New Sheet"
End If
End With
End If
End Function
<code>
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
A lot of code for such a simple task!!
Will the value occur only once in Sheet1?
If found, you want to copy all values in That column to Sheet2?
Including Row 1??

lenze
 
Upvote 0
The value may or may not occur in Sheet 1, hence the find and all the code. Really inexperienced at this, obviously.

I want to copy all the values of the column including the foundCell.
 
Upvote 0
So
Code:
Sub SearchMe()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim LR As Long
Dim C As Range
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
With ws1.UsedRange
    Set C = .Find(InputBox("Enter Value to Find"), LookIn:=xlValues)
    If Not C Is Nothing Then
    LR = ws1.Cells(Rows.Count, C.Column).End(xlUp).Row
    ws1.Cells(1, C.Column).Resize(LR, 1).Copy
    ws2.Cells(1, C.Column).PasteSpecial xlPasteValues
    Else: MsgBox "Value not found"
    End If
End With
End Sub

lenze
 
Upvote 0
In this case a user is not involved and using an input box is not what I need. The data on Sheet 1 is already provided and needs to be manipulated and placed on sheet 2. Part of the reason that code is so complex is an array filters through various strings. I'll keep reviewing and see if it helps.
 
Upvote 0
.... thank you...figured it out with your help

<code>
If Trim(FindString) <> "" Then
With wksht1.Range("A1:IV65536")
'With ThisWorkbook.Sheets(1).Range("A1:IV65536")
Set FoundCell = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
'if my cell is found copy to 2nd worksheet
If Not FoundCell Is Nothing Then
Application.Goto FoundCell, True
MsgBox FoundCell.Address & FoundCell.Text & " " & "Found in Copy_Col" 'returns cell address
'MsgBox FoundCell.Columns & " " & "Columns" 'returns the column number as a Long
'FoundCell.Cells.EntireColumn.Copy ActiveWorkbook.Sheets(2).Columns(FoundCell.Column) 'this works
wksht1.Cells(1, FoundCell.Column).EntireColumn.Copy
wksht2.Cells(1, FoundCell.Column).PasteSpecial xlPasteValues
</code>
 
Upvote 0
If you don't have userinterface, then use whatever you want in the Find Method. The principle of Finding and Copying will not change!!

lenze
 
Upvote 0
I'm glad it works, but you still have several things that will slow your code down. Probably not an issue, as I suspect it still executes in about 1 second!!

lenze
 
Upvote 0
Is there a way to keep the values format...

I.e a number is a number, a date a date... something like

xlFormat = true?
 
Upvote 0
Why copy the Entire Column instead of the Used Rows. See my example
And why look in the entire sheet instead of just the UsedRange. Your current code might have problems if used on Excel 2007.

But, for Formats and Values, change
Rich (BB code):
wksht2.Cells(1, FoundCell.Column).PasteSpecial xlPasteValues
to
Rich (BB code):
wksht2.Cells(1, FoundCell.Column).PasteSpecial xlPasteValuesAndNumberFormats

lenze
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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