VBA Code - Data Not being Added to Empty Row

MHamid

Active Member
Joined
Jan 31, 2013
Messages
340
Hello,

I am having an issue with my code below (this is not the full code). I want to add data into the empty row after the last row used. In my AuditData tab row 1 is the header and everything else is blank. So the data should be added into rows 2, 3 and so on. The code works, but not as expected. This is what the full code does. It will loop through all PDF files and extract the data via WORD into Excel for each PDF File it finds. So, when it finds the first PDF file, it will open the PDF file in Word and copy all the data as text into a sheet in Excel. Then it will extract the data that I need into two different sheets (Other and ChartData). Once that process is complete, it will then perform the below function.
It does add the data that I need into the AuditData tab. However, when it is working on the first PDF file, it adds this data in row 6 (leaving rows 2-5 blank). Then when it finds the second PDF file, it will add this data into row 8 (leaving row 7 blank). Therefore, header in row 1, 1st PDF data in row 6 and then 2nd PDF data in row 8. I am not sure what I am missing in my code that is causing this result. Can anyone please help?

Code:
Function ProcessedAudits()
'Define Variables
    Dim LR As Long
    Dim LastR As Long
   
'Set Variables
    LR = Cells(Rows.Count, "A").End(xlUp).Row
    LastR = Range("A" & Rows.Count).End(xlUp).Row
 
Worksheets("AuditData").Activate
 
'Add Audit Data
    With Worksheets("AuditData")
        Range("A" & LR).FormulaR1C1 = "=Other!R3C5"
        Range("B" & LR).FormulaR1C1 = "=Other!R7C5"
        Range("C" & LR).FormulaR1C1 = "=Other!R2C5"
        Range("D" & LR).FormulaR1C1 = "=Other!R5C5"
        Range("E" & LR).FormulaR1C1 = "=Other!R4C5"
        Range("F" & LR).FormulaR1C1 = "=Other!R6C5"
        Range("G" & LR).FormulaR1C1 = "=COUNTIF(ChartData!C3,""Level 1"")"
        Range("H" & LR).FormulaR1C1 = "=COUNTIF(ChartData!C3,""Level 2"")"
        Range("I" & LR).FormulaR1C1 = "=COUNTIF(ChartData!C3,""Level 3"")"
        Range("J" & LR).FormulaR1C1 = "=COUNTIF(ChartData!C3,""Level 4"")"
        Range("K" & LR).FormulaR1C1 = "=COUNTIF(ChartData!C3,""Level 5"")"
        Range("L" & LR).FormulaR1C1 = "=SUM(RC[-5]:RC[-1])"
        Range("M" & LR).FormulaR1C1 = "=COUNTIF(ChartData!C5,""Level 1 IBAM"")"
        Range("N" & LR).FormulaR1C1 = "=COUNTIF(ChartData!C5,""Level 2 IBAM"")"
        Range("O" & LR).FormulaR1C1 = "=COUNTIF(ChartData!C5,""Level 3 IBAM"")"
        Range("P" & LR).FormulaR1C1 = "=COUNTIF(ChartData!C5,""Level 4 IBAM"")"
        Range("Q" & LR).FormulaR1C1 = "=COUNTIF(ChartData!C5,""Level 5 IBAM"")"
        Range("R" & LR).FormulaR1C1 = "=SUM(RC[-5]:RC[-1])"
        Range("S" & LR).FormulaR1C1 = "=TRIM(LEFT(SUBSTITUTE('Other'!R16C1,""%"",REPT("" "",100)),100))"
        Range("T" & LR).FormulaR1C1 = "=TRIM(LEFT(SUBSTITUTE('Other'!R16C3,""%"",REPT("" "",100)),100))"
        Range("U" & LR).FormulaR1C1 = "=TRIM(LEFT(SUBSTITUTE('Other'!R17C3,""%"",REPT("" "",100)),100))"
        Range("V" & LR).FormulaR1C1 = "=TRIM(LEFT(SUBSTITUTE('Other'!R18C3,""%"",REPT("" "",100)),100))"
       
        'Copy PasteSpecial Values
        Range("A2:V" & LastR).Copy
        Range("A2:V" & LastR).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
       
        Range("A1").Select
   
    End With
 
End Function
Also, I have tried adding +1 to locate the last empty cell. But this resulting in adding the 1st PDF data into row 7 and the 2nd PDF data into row 9. And the 2nd PDF data came over as #REF for all columns. Again, I am not sure what it is that I am doing wrong.

Code:
LR = Cells(Rows.Count, "A").End(xlUp).Row + 1

Thank you
 

mohadin

Active Member
Joined
Mar 22, 2015
Messages
358
Office Version
2013
Platform
Windows
Range("G" & LR).Formula = "=COUNTIF(ChartData!C3,""Level 1"")"
.
.
.
 
Last edited:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,246
Office Version
365
Platform
Windows
Try
Code:
Function ProcessedAudits()
'Define Variables
    Dim LR As Long
    Dim LastR As Long
   
Worksheets("AuditData").Activate
   
'Set Variables
    LR = Cells(Rows.Count, "A").End(xlUp).Row
    LastR = Range("A" & Rows.Count).End(xlUp).Row
 
 
'Add Audit Data
        Range("A" & LR).FormulaR1C1 = "=Other!R3C5"
        Range("B" & LR).FormulaR1C1 = "=Other!R7C5"
        Range("C" & LR).FormulaR1C1 = "=Other!R2C5"
        Range("D" & LR).FormulaR1C1 = "=Other!R5C5"
        Range("E" & LR).FormulaR1C1 = "=Other!R4C5"
        Range("F" & LR).FormulaR1C1 = "=Other!R6C5"
        Range("G" & LR).FormulaR1C1 = "=COUNTIF(ChartData!C3,""Level 1"")"
        Range("H" & LR).FormulaR1C1 = "=COUNTIF(ChartData!C3,""Level 2"")"
        Range("I" & LR).FormulaR1C1 = "=COUNTIF(ChartData!C3,""Level 3"")"
        Range("J" & LR).FormulaR1C1 = "=COUNTIF(ChartData!C3,""Level 4"")"
        Range("K" & LR).FormulaR1C1 = "=COUNTIF(ChartData!C3,""Level 5"")"
        Range("L" & LR).FormulaR1C1 = "=SUM(RC[-5]:RC[-1])"
        Range("M" & LR).FormulaR1C1 = "=COUNTIF(ChartData!C5,""Level 1 IBAM"")"
        Range("N" & LR).FormulaR1C1 = "=COUNTIF(ChartData!C5,""Level 2 IBAM"")"
        Range("O" & LR).FormulaR1C1 = "=COUNTIF(ChartData!C5,""Level 3 IBAM"")"
        Range("P" & LR).FormulaR1C1 = "=COUNTIF(ChartData!C5,""Level 4 IBAM"")"
        Range("Q" & LR).FormulaR1C1 = "=COUNTIF(ChartData!C5,""Level 5 IBAM"")"
        Range("R" & LR).FormulaR1C1 = "=SUM(RC[-5]:RC[-1])"
        Range("S" & LR).FormulaR1C1 = "=TRIM(LEFT(SUBSTITUTE('Other'!R16C1,""%"",REPT("" "",100)),100))"
        Range("T" & LR).FormulaR1C1 = "=TRIM(LEFT(SUBSTITUTE('Other'!R16C3,""%"",REPT("" "",100)),100))"
        Range("U" & LR).FormulaR1C1 = "=TRIM(LEFT(SUBSTITUTE('Other'!R17C3,""%"",REPT("" "",100)),100))"
        Range("V" & LR).FormulaR1C1 = "=TRIM(LEFT(SUBSTITUTE('Other'!R18C3,""%"",REPT("" "",100)),100))"
       
        'Copy PasteSpecial Values
        Range("A2:V" & LastR).Copy
        Range("A2:V" & LastR).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
       
        Range("A1").Select
   
 
End Function
 

Forum statistics

Threads
1,077,822
Messages
5,336,564
Members
399,088
Latest member
Swindlestikz

Some videos you may like

This Week's Hot Topics

Top