2

I decided to change my tact. I decided to take another shot at this, but in a new way. I did a weekend long Google marathon and found I believe my answer,

Option Explicit
Sub DataUpdate()
Dim rFind As Long, NR As Long, LR As Long, LC As Long
LR = Range("C" & Rows.Count).End(xlUp).Row
LC = Cells(2, Columns.Count).End(xlToLeft).Column
NR = LR + 1

On Error Resume Next
rFind = Range("A25:A" & LR).Find(Range("A1")).Row
On Error GoTo 0

    If rFind = 0 Then
        If MsgBox("Customer record not found, add to dataset?", vbYesNo + vbQuestion) = vbYes Then
            Range("A2", Cells(LC, 2)).Copy
            Range("C" & NR).PasteSpecial xlPasteValues
            Range("A1", Cells(1, LC)).ClearContents
            Exit Sub
        End If
    Else
        Range("A2", Cells(2, LC)).Copy
        Range("A" & rFind).PasteSpecial xlPasteValues
        Range("A1", Cells(1, LC)).ClearContents
    End If
End Sub

Looking at this I just want a cleaner explanation instead of just taking it as is, and using it without knowing what I am doing.

Here is the sheet it is on:

http://dl.dropbox.com/u/3327208/Excel/Replace.zip

If I add this to my code, regurgitate this code I see I can do this, I just want to verify that this is correct.

Option Explicit

Sub PENCMR()
    Dim i As Integer

    With Application
        .ScreenUpdating = False
    End With

    'Internal NCMR
    Dim wsPE As Worksheet
    Dim wsNDA As Worksheet

    'Copy Ranges
    Dim c As Variant

    'Paste Ranges
    Dim p As Range

    'Setting Sheet
    Set wsPE = Sheets("Print-Edit NCMR")
    Set wsNDA = Sheets("NCMR Data")
    Set p = wsPE.Range("A54:U54")

    With wsPE
        c = Array(.Range("AG2"), .Range("B11"), .Range("B14"), .Range("B17"), .Range("B20"), .Range("B23") _
                , .Range("Q11"), .Range("Q14"), .Range("Q17"), .Range("Q20"), .Range("R25"), .Range("V23") _
                , .Range("V25"), .Range("V27"), .Range("B32"), .Range("B36"), .Range("B40"), .Range("B44") _
                , .Range("D49"), .Range("L49"), .Range("V49"))
    End With

    For i = LBound(c) To UBound(c)
        p(i + 1).Value = c(i).Value
    Next

    With wsNDA
        Dim rFind As Long, NR As Long, LR As Long, LC As Long
        LR = Range("C" & Rows.Count).End(xlUp).Row
        LC = Cells(2, Columns.Count).End(xlToLeft).Column
        NR = LR + 1
        rFind = wsNDA.Range("A:A" & LR).Find(Range("A54")).Row

        Range("A54", Cells(2, LC)).Copy
        Range("A" & rFind).PasteSpecial xlPasteValues
        Range("A54", Cells(1, LC)).ClearContents

    End With

    With Application
        .ScreenUpdating = True
    End With
End Sub

The code runs, but it doesn't come back with an error, yet it doesn't run completely. It hits to the point where it drags everything down, then it seems to die there. Can someone help me find out why it doesn't do what I think it should do, which is copy the row, search for the number in column A, and then write over it with the correct data in row 54...

I know something is wrong, but I don't have the skills to figure out what, if someone can help me it be greatly appreciated.

1 Answer 1

0

I am not 100% sure of what you are trying to achieve but there are several problems in your code:

Instead of

Set p = wsPE.Range("A54:U54")
For i = LBound(c) To UBound(c)
    p(i + 1).Value = c(i).Value
Next

You probably mean

Set p = wsPE.Range("A54")
For i = LBound(c) To UBound(c)
    p.Offset(0, i) = c(i)
Next

In your With wsNDA block, you need to put . before the Range and Cells, for example:

.Range("A54", .Cells(2, LC)).Copy

Finally:

  • I would remove the ScreenUpdating statements for now, and run the code in debug mode (F8) to see step by step what the code is doing and check the values of your variables if necessary use "Add Watch"
  • I would avoid using a range to store temporary data. You could use a 2D array instead, like this for example:

Dim data As Variant
Redim data(1 To 1, 1 To 21) As Variant
for i = xx To yy
    data(1,i+1) = c(i)
Next i

yourTargetCell.Resize(1, UBound(data,2)) = data
15
  • The only reason I did a range to store the data temporarily was because that was the only way I knew how to do it. Also, I wanted it to be stored temporarily just in case there was a bug I'd know what to look for. I'm still relatively new to VBA in general, so I don't know everything I should. Let me take the code you have tweaked and see how it works. I'll respond with questions I am sure :)
    – Matt Ridge
    May 1, 2012 at 14:06
  • As for what I am trying to do is this, if you download the file, and open it, you'll see two sheets, the first being a form, the second being a place for storage, and where the form gets its data. In the first form is a pull down menu on the right, this will allow you to select the info on the 2nd page. Then it will self populate the rest. After than I want to change something in there, and have a script after pushing the submit button to change the original row with the new data in it, without changing the number. Does this make any sense now?
    – Matt Ridge
    May 1, 2012 at 14:08
  • I am getting an error on rFind = wsNDA.Range("A:A" & LR).Find(Range("A54")).Row. Run-Time error '1004' Method 'Range' of object'_Worksheet'Failed.
    – Matt Ridge
    May 1, 2012 at 14:55
  • Try: .Range("A1:A" & LR).Find(.Range("A54")).Row (note the dots before the 2 Range and A1 instead of A).
    – assylias
    May 1, 2012 at 14:58
  • Same error. rFind = wsNDA.Range("A:A" & LR).Find(.Range("A54")).Row
    – Matt Ridge
    May 1, 2012 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.