Derbydab03 if f2 is blank how could it go to f5 without errr

September 1, 2017 at 00:01:58
Specs: Windows 7
Sub Copy_F2F5_Count()
'Copy A2:E4 based on F2
numCopies = Sheets(1).Range("F2")
For copyRange = 1 To numCopies
nxtRow = Sheets(2).Range("A" & Rows.Count).End(xlUp).Row + 1
Sheets(1).Range("A2:E4").Copy _
Destination:=Sheets(2).Range("A" & nxtRow)
Next
'Copy A5:E7 based on F5
numCopies = Sheets(1).Range("F5")
For copyRange = 1 To numCopies
nxtRow = Sheets(2).Range("A" & Rows.Count).End(xlUp).Row + 1
Sheets(1).Range("A5:E7").Copy _
Destination:=Sheets(2).Range("A" & nxtRow)
Next
End Sub

if F2 is blank how would it go to f5 without mismatch error

message edited by ace12345


See More: Derbydab03 if f2 is blank how could it go to f5 without errr

Reply ↓  Report •

#1
September 1, 2017 at 08:15:05
There is nothing wrong with the code itself that would cause a Type Mismatch error, assuming that F2 is actually blank. If F2 is blank/empty, then numCopies will be set to Empty and the loop will not run, therefore the code will continue at the F5 loop.

If F2 contains a space or some other character that cannot be seen (making the cell appear to be blank) then you could indeed get a Type Mismatch error since VBA is looking for a number.

I suggest that you use the techniques in the following tutorial to see what value numCopies is being set to when you think that F2 is blank. You should determine the root cause of the error and then figure out how to deal with it. It's not necessarily the code that is the problem.

https://www.computing.net/howtos/sh...

In addition, please click on the following How To line and read the instructions on how to post VBA code in this forum so that it is easier for us to read. Then edit/repost your code so that it looks similar to the example found via that link. Thanks!

How To Post Data or Code ---> Click Here Before Posting Data or VBA Code


Reply ↓  Report •
Related Solutions


Ask Question