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
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
you can find Last filled cell in E column by this code :

LastCell = cells(rows.count, "E").end(xlup)

and then set new range
Set rng = range(rng,lastcell)

then instead of using Do loop you should use For each loop like:

for each cell in rng

next cell

but do not forget to include in case Cell is empty what should code do :)
 
Upvote 0
Thanks, i'll go read up on how to do that so i can write the code to suit and see how i go. cheers
 
Upvote 0
i'm not having much success with my coding skills. Would anyone be so kind as to modify that section of my original code to show me how it should be written to :
Find the last cell in column E and then loop through that range... skipping the blank cells but move the files for cells that arn't blank
 
Upvote 0
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


'================================================
'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 & "" & 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)


Else
'write code what should macro do in case of empty cell
End If
Next cell
End Sub


try this one, and it needs to add part when cell is empty, i mentioned comment in code for you
 
Upvote 0
Untested, but try
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
[COLOR=#0000ff]Dim Cell As Range[/COLOR]

'================================================
'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 & "\" & [COLOR=#0000ff]Cell[/COLOR].Value & ".pdf"
      
      
      'build up the full path to the new file you want to create
      newFile = newPath & "\" & Foldermonth & "\" & [COLOR=#0000ff]Cell[/COLOR].Value & "\" & [COLOR=#0000ff]Cell[/COLOR].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
   
Next Cell
End Sub
 
Upvote 0
Thank you to both of you for the reply. Unfortunately i get a compile error on both codes.... " next without for" just before the end sub.
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

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