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>
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>