llawrenceg
asked on
HOW DO I CORRECT THE CODE TO COPY CORRECTLY
I am copying the values from a row on one tab to a row on another tab based on a criteria. When I step thru the code I can see the source value being picked up but not the destination value being entered.
Can you point me in the right direction?
Can you point me in the right direction?
Set srcSheet = ThisWorkbook.Worksheets("CSM")
Set dstSheet = ThisWorkbook.Worksheets(1)
Set R = srcSheet.Range("B2")
i = 0
Set R = srcSheet.Range("B:B").Find(Right(dstSheet.Range("B15").Value, 5), R)
If Not R Is Nothing Then
foundRows(i) = R.Row
If i = 0 Or Not foundRows(i) = foundRows(0) Then
dstSheet.Cells(5 + i, 3).Value = srcSheet.Cells((foundRows(i)), 8)
dstSheet.Cells(5 + i, 4).Value = srcSheet.Cells((foundRows(i)), 9)
dstSheet.Cells(5 + i, 5).Value = srcSheet.Cells((foundRows(i)), 3)
dstSheet.Cells(5 + i, 6).Value = srcSheet.Cells((foundRows(i)), 4)
dstSheet.Cells(5 + i, 7).Value = srcSheet.Cells((foundRows(i)), 6)
Else
dstSheet.Range("C" & (5 + i) & ":H" & (12 + i)).ClearContents
dstSheet.Range("C" & (5 + i)).Formula = "N/A"
End If
Else
dstSheet.Range("C" & (5 + i) & ":H" & (12 + i)).ClearContents
dstSheet.Range("C" & (5 + i)).Formula = "N/A"
Set R = srcSheet.Range("B2")
End If
'srcBook.Close (False)
'Application.EnableEvents = True
Application.ScreenUpdating = True
Exit Sub
Post all your code, and, ideally, a sample workbook.
hmmm...Set dstSheet = ThisWorkbook.Worksheets(1)
Right after this, try to see which sheet was picked up:
Msgbox dstSheet.Name
Also, it's good practice at the end to clear the objects:
Set R = Nothing
Right after this, try to see which sheet was picked up:
Msgbox dstSheet.Name
Also, it's good practice at the end to clear the objects:
Set R = Nothing
ASKER
The MSGbox reads "dbSheet".
Sub CSM() ' Copies CSM data
Dim srcBook As Workbook
Dim srcSheet As Worksheet
Dim dstSheet As Worksheet
Dim R As Range
Dim foundRows(2) As Long, i As Long
'Application.EnableEvents = False
Application.ScreenUpdating = False
On Error GoTo crash
Set srcSheet = ThisWorkbook.Worksheets("CSM")
Set dstSheet = ThisWorkbook.Worksheets(1)
Set R = srcSheet.Range("B2")
i = 0
Set R = srcSheet.Range("B:B").Find(Right(dstSheet.Range("B15").Value, 5), R)
If Not R Is Nothing Then
foundRows(i) = R.Row
If i = 0 Or Not foundRows(i) = foundRows(0) Then
dstSheet.Cells(5 + i, 3).Value = srcSheet.Cells((foundRows(i)), 8)
dstSheet.Cells(5 + i, 4).Value = srcSheet.Cells((foundRows(i)), 9)
dstSheet.Cells(5 + i, 5).Value = srcSheet.Cells((foundRows(i)), 3)
dstSheet.Cells(5 + i, 6).Value = srcSheet.Cells((foundRows(i)), 4)
dstSheet.Cells(5 + i, 7).Value = srcSheet.Cells((foundRows(i)), 6)
Else
dstSheet.Range("C" & (5 + i) & ":H" & (12 + i)).ClearContents
dstSheet.Range("C" & (5 + i)).Formula = "N/A"
End If
Else
dstSheet.Range("C" & (5 + i) & ":H" & (12 + i)).ClearContents
dstSheet.Range("C" & (5 + i)).Formula = "N/A"
Set R = srcSheet.Range("B2")
End If
'srcBook.Close (False)
'Application.EnableEvents = True
Application.ScreenUpdating = True
Exit Sub
crash:
Application.EnableEvents = True
Application.ScreenUpdating = True
MsgBox Err.Description
On Error GoTo 0
End Sub
First, a minor memory leak...replace this:
...
Exit Sub
crash:
Application.EnableEvents = True
Application.ScreenUpdating = True
MsgBox Err.Description
On Error GoTo 0
...
with this:
nocrash:
Exit Sub
crash:
Application.EnableEvents = True
Application.ScreenUpdating = True
MsgBox Err.Description
Resume nocrash
--
Confirm that dbSheet is the tab that has the data you want to read.
...
Exit Sub
crash:
Application.EnableEvents = True
Application.ScreenUpdating
MsgBox Err.Description
On Error GoTo 0
...
with this:
nocrash:
Exit Sub
crash:
Application.EnableEvents = True
Application.ScreenUpdating
MsgBox Err.Description
Resume nocrash
--
Confirm that dbSheet is the tab that has the data you want to read.
ASKER
dbSheet is not any tab in theworkbok. there ar only 4 tabs.... one is CSM
SHEET(1)IS NAMED DIFFERENTLY ....BASED ON user name
SHEET(1)IS NAMED DIFFERENTLY ....BASED ON user name
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry about the workbook . It's all proprietary Client information
First the code works to the msg box
Set R = srcSheet.Range("B:B").Find (Right(dst Sheet.Rang e("B15").V alue, 5), R)' picks up the correct value of R from cell B15
If Not R Is Nothing Then ' the value of R changes to the value of Range (B16")
dstSheet.Cells(5 + i, 3).Value = srcSheet.Cells((foundRows( i)),8) " gete the correct value from source
dstSheet.Cells(5 + i, 4).Value = srcSheet.Cells((foundRows( i)), 9)
dstSheet.Cells(5 + i, 5).Value = srcSheet.Cells((foundRows( i)), 3)
dstSheet.Cells(5 + i, 6).Value = srcSheet.Cells((foundRows( i)), 4)
dstSheet.Cells(5 + i, 7).Value = srcSheet.Cells((foundRows( i)), 6)
First the code works to the msg box
Set R = srcSheet.Range("B:B").Find
If Not R Is Nothing Then ' the value of R changes to the value of Range (B16")
dstSheet.Cells(5 + i, 3).Value = srcSheet.Cells((foundRows(
dstSheet.Cells(5 + i, 4).Value = srcSheet.Cells((foundRows(
dstSheet.Cells(5 + i, 5).Value = srcSheet.Cells((foundRows(
dstSheet.Cells(5 + i, 6).Value = srcSheet.Cells((foundRows(
dstSheet.Cells(5 + i, 7).Value = srcSheet.Cells((foundRows(
ASKER
Finally figured it out . There were 3 hidden worksheets on the workbook and the code was running against the hidden sheets-- dbsheet was a hidden sheet.
Once I delete the hidden sheets, everything worked
Once I delete the hidden sheets, everything worked
Excellent! Glad you figured it out!