Rows.Count).End(xlUp).Row

chipsworld

Board Regular
Joined
May 23, 2019
Messages
169
Office Version
  1. 365
Not sure where this is wrong , but...

The below only puts new data on Row 35. If Row 35 has data, it will simply write over it. Where am I wrong? This is making me crazy!
Currently there is data on Rows 2 and 3 and then nothing until 35. Row 1 is the header Row...

VBA Code:
Public Sub PDMRA_Copy()
Dim rw As Long
Dim DestWS As Worksheet
Dim smname As String
    
smname = frmPDMRAcalc.txtnam2.Value
    
    Set DestWS = Sheets("PDMRA History")
    On Error Resume Next
    With DestWS
        rw = .Range("A:A").Find(smname, LookIn:=xlValues, LookAt:=xlPart, SearchDirection:=xlPrevious).Row
        If rw <> 0 Then
            .Range("B" & rw).Value = frmPDMRAcalc.txtqmobsrt.Value
            .Range("C" & rw).Value = frmPDMRAcalc.txtqmobend.Value
            .Range("D" & rw).Value = frmPDMRAcalc.cmbqcomp.Value
            .Range("E" & rw).Value = frmPDMRAcalc.cmbqusc.Value
            .Range("F" & rw).Value = frmPDMRAcalc.cmbqcntry.Value
            .Range("G" & rw).Value = frmPDMRAcalc.txtcmobsrt.Value
            .Range("H" & rw).Value = frmPDMRAcalc.txtcmobend.Value
            .Range("I" & rw).Value = frmPDMRAcalc.cmbccomp.Value
            .Range("J" & rw).Value = frmPDMRAcalc.cmbcusc.Value
            .Range("K" & rw).Value = frmPDMRAcalc.cmbccntry.Value
            .Range("L" & rw).Value = frmPDMRAcalc.txtmonthsdwell.Value
            .Range("M" & rw).Value = frmPDMRAcalc.txtpdmraern.Value
            .Range("N" & rw).Value = frmPDMRAcalc.txtmnthsearned.Value
            .Range("O" & rw).Value = Sheets("Formulas").Range("AC16").Value
            .Range("P" & rw).Value = Sheets("Formulas").Range("AC17").Value
        Else
            rw = Range("A" & Rows.Count).End(xlUp).Row + 1
            .Range("A" & rw).Value = frmPDMRAcalc.txtnam2.Value
            .Range("B" & rw).Value = frmPDMRAcalc.txtqmobsrt.Value
            .Range("C" & rw).Value = frmPDMRAcalc.txtqmobend.Value
            .Range("D" & rw).Value = frmPDMRAcalc.cmbqcomp.Value
            .Range("E" & rw).Value = frmPDMRAcalc.cmbqusc.Value
            .Range("F" & rw).Value = frmPDMRAcalc.cmbqcntry.Value
            .Range("G" & rw).Value = frmPDMRAcalc.txtcmobsrt.Value
            .Range("H" & rw).Value = frmPDMRAcalc.txtcmobend.Value
            .Range("I" & rw).Value = frmPDMRAcalc.cmbccomp.Value
            .Range("J" & rw).Value = frmPDMRAcalc.cmbcusc.Value
            .Range("K" & rw).Value = frmPDMRAcalc.cmbccntry.Value
            .Range("L" & rw).Value = frmPDMRAcalc.txtmonthsdwell.Value
            .Range("M" & rw).Value = frmPDMRAcalc.txtpdmraern.Value
            .Range("N" & rw).Value = frmPDMRAcalc.txtmnthsearned.Value
            .Range("O" & rw).Value = Sheets("Formulas").Range("AC16").Value
            .Range("P" & rw).Value = Sheets("Formulas").Range("AC17").Value
        End If
    End With
    
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Where do you want the data? row 36 or row 4?
 
Upvote 0
every new entry should go to the first empty row from the top...

It works fine for a name that is already there. Finds the row and copies data and overwrites the current.
 
Upvote 0
Sorry you are confusing me now. You run the first code which is pasting at every 7th row, now you saying that the 2nd code is to paste at the first first empty row from the top.

Please describe from start to finish in detail exactly what you want.
 
Upvote 0
Mark,
Now you have me confused! LOL The goal here is to always paste at the first empty row below any filled rows if new data.

1. The first code does a lookup for the name, and if it finds a match, it pastes the updated data through an overwrite. (works perfectly)
2. The second code (else) is if a match is not found, and it pastes all data to the first empty row. Thereby adding a new row of data. (pastes data on row 35 - always)

How is the first code pasting every 7th row?

VBA Code:
rw = .Range("A:A").Find(smname, LookIn:=xlValues, LookAt:=xlPart, SearchDirection:=xlPrevious).Row ' finds name in column A if present and returns row

VBA Code:
rw = Range("A" & Rows.Count).End(xlUp).Row + 1 ' if name not found in column A finds first empty row by searching from the bottom up and places data on that row.
 
Upvote 0
Oops my bad, posted on the wrong thread, sorry.
 
Upvote 0
To answer your question I think it is due to you missing a period in front of your range

Rich (BB code):
rw = .Range("A" & Rows.Count).End(xlUp).Row + 1
 
Upvote 0
Solution
Brilliant! I have been staring at that code for three days trying to figure out where I goofed up.

Thank you!!!
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,022
Latest member
RobertV1609

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