VBA code selecting a range with a blank cell

Skooks

New Member
Joined
Dec 13, 2017
Messages
7
Hi all first time poster. Been getting some great tips and lessons from the board but i'm stuck on some VBA code.

I have pieced together and modified some code to do a task i'm after but one section i can't get to work.
I'm not good at writing VBA but i'm capable of modifying something to work. sometimes :)

i call this below macro from another macro and it works up until it comes across a blank cell in a column D of the spreadsheet

It essentially looks down the D column starting with D5 and if the cell has a number in it it then looks for a PDF file in one folder and if it exists moves it to another folder. Then moves onto the next cell. The issue is if ( for example) the first 5 cells have numbers in them it works flawlessly but if the 6th cell is blank and the 7th cell has numbers in it the file relating to the 7th cell doesnt get moved. I'm assuming i need to specify a better range but not sure how i do that. Im guessing but assuming the current code ( in code language) says " keep looping down the D column until it comes across a blank cell then stop" ... how do i change that to keep looping until D150 or similar ?

Sub Movefile()
Dim rng As Range
Dim oldPath As String
Dim newPath As String
Dim oldFile As String
Dim newFile As String
Dim Foldermonth As String

'================================================
'Set the paths to the folders you are processing
'REMEMBER END BACKSLASH
'================================================
oldPath = "C:\Users\larry\Documents\VBA test folder\Purchase order"
newPath = "C:\Users\larry\Documents\VBA test folder\Purchase order"
Foldermonth = ActiveSheet.Range("B2").Value

'=======================================================
'set up the start of the range you want to loop through
'EDIT SHEET NAME AND FIRST CELL ADDRESS IF NECESSARY
'======================================================
Set rng = ActiveSheet.Range("E5")

'==================================================
'loop through column B until you find an empty cell
'==================================================
Do Until rng = ""

'build up the full path to the old file
oldFile = oldPath & "" & rng.Value & ".pdf"

'build up the full path to the new file you want to create
newFile = newPath & "" & Foldermonth & "" & rng.Value & "" & rng.Value & ".pdf"

'copy the old file to the new new folder
If Len(Dir(oldFile, vbDirectory)) > 0 Then
FileCopy oldFile, newFile
Kill (oldFile)
End If
'get the next file by moving down one row
Set rng = rng.Offset(1, 0)
Loop
End Sub



Thanks in advance for any help
 
Missing an End If
Code:
Sub Movefile()
Dim rng As Range
Dim oldPath As String
Dim newPath As String
Dim oldFile As String
Dim newFile As String
Dim Foldermonth As String
Dim LastCell As Range
Dim LoopRng As Range
Dim Cell As Range

'================================================
'Set the paths to the folders you are processing
'REMEMBER END BACKSLASH
'================================================
oldPath = "C:\Users\larry\Documents\VBA test folder\Purchase order"
newPath = "C:\Users\larry\Documents\VBA test folder\Purchase order"
Foldermonth = ActiveSheet.Range("B2").Value


'=======================================================
'set up the start of the range you want to loop through
'EDIT SHEET NAME AND FIRST CELL ADDRESS IF NECESSARY
'======================================================
Set rng = ActiveSheet.Range("E5")
Set LastCell = ActiveSheet.Cells(Rows.Count, rng.column).End(xlUp)
Set LoopRng = ActiveSheet.Range(rng, LastCell)


'==================================================
'loop through column B until you find an empty cell
'==================================================
For Each Cell In LoopRng
   
   
   If Not IsEmpty(Cell) Then
   
      
      'build up the full path to the old file
      oldFile = oldPath & "\" & Cell.Value & ".pdf"
      
      
      'build up the full path to the new file you want to create
      newFile = newPath & "\" & Foldermonth & "\" & Cell.Value & "\" & Cell.Value & ".pdf"
      
      
      'copy the old file to the new new folder
      If Len(Dir(oldFile, vbDirectory)) > 0 Then
         FileCopy oldFile, newFile
         Kill (oldFile)
      End If
  [COLOR=#ff0000] End If[/COLOR]
   
Next Cell
End Sub
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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