0

Below is my code

Sub Append()
    'Append data from other files
    Path = "E:\NPM PahseIII\"
    Filename = Dir(Path & "*.xlsx")
    If InStr(Filename, ".") > 0 Then
        Filenamenoext = Left(Filename, InStr(Filename, ".") - 1)
    End If
    MsgBox Filenamenoext
    Range("A3").Select
    Do While Filename <> ""
        ActiveCell.Value = Filenamenoext
    Loop
End Sub

My problem is that as I've selected Range("A3").Select is hard coded, i want this selection to be done dynamically in loop such that when the first iteration of the loop start it should select Range("A3").Select & further select next cell in the next iteration. How can i achieve this? Edited See image below image

braX
  • 11,506
  • 5
  • 20
  • 33

1 Answers1

3

Like this (untested):

Sub Append()

    Const FPath As String = "E:\NPM PahseIII\"
    Dim c As Range, Filename 

    'find the first empty cell in ColA
    Set c = activesheet.cells(rows.count, 1).end(xlup).offset(1, 0)

    Filename = Dir(FPath & "*.xlsx")
    Do While Filename <> ""
        c.Value = Split(Filename, ".")(0) 'note: problem if any of your file names have embedded periods...
        Set c = c.offset(1, 0)
        Filename = Dir()
    Loop

End Sub
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • Your work in actually laying out the answer to the question deserves an upvote. – BigBen Dec 13 '18 at 18:20
  • And you could also simplify by moving `Set c = activesheet...` within the loop and eliminating the `Set c = c.offset`, just a thought. – BigBen Dec 13 '18 at 18:27
  • Yes could be a few lines shorter but wanted to show how to (1) get a reference to the first empty cell (2) "move" that reference using offset(). All without select/activate – Tim Williams Dec 13 '18 at 18:30
  • I agree. Thank you for fleshing this out in actual code. Pity I can't upvote again. – BigBen Dec 13 '18 at 18:31