VBA Code Question

Excel_Assis

Board Regular
Joined
Feb 19, 2011
Messages
132
Office Version
  1. 2016
Platform
  1. Windows
Could someone please help with the following.


For i = 37 To .Range("B" & .Rows.Count).End(xlUp).Row
If .Range("E" & i).Value <> "" Then
.Range("E" & i).Offset(0, -1).Value = .Range("E" & i).Value
End If



The code works to line starting with For i = 37 To .Range
then skips over the rest. It all worked fine on another spread sheet.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Don't know what precedes the For loop but try removing the periods.
Code:
For I = 37 To Range("B" & Rows.Count).End(xlUp).Row
If Range("E" & I).Value <> "" Then
Range("E" & I).Offset(0, -1).Value = Range("E" & I).Value
End If
 
Upvote 0
Hi Michael thanks for the suggestion I take it you meant remove the "" however this did not work.

The preceding code and following code is this.


Dim wsSrc As Worksheet: Set wsSrc = Sheets("Claim Form")
Dim wsDest As Worksheet: Set wsDest = Sheets("Sort Sum")
Dim wsDestSum As Worksheet: Set wsDestSum = Sheets("Cash Payment Voucher")
Dim i As Long
With wsDest
.UsedRange.Clear
wsSrc.Range("B9:B47,E9:E47,I9:I47,K9:K47").Copy .Range("B10")

For i = 37 To .Range("B" & .Rows.Count).End(xlUp).Row
If .Range("E" & i).Value <> "" Then
.Range("E" & i).Offset(0, -1).Value = .Range("E" & i).Value
End If
Next i

.Columns("E").Delete
.Range("B10:D100").AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=.Range("E10"), Unique:=True
.Columns("B:D").Delete
End With
 
Upvote 0
Not certain, but shouldn't this line refer to a sheet as well
Code:
For i = 37 To .Range("B" & .Rows.Count).End(xlUp).Row

Code:
wsDest.activate
For i = 37 To .Range("B" & .Rows.Count).End(xlUp).Row
 
Upvote 0
Hi Michael after looking deeper working on the problem I believe the problem exists with the .Value part of the For Loop.

I think the values in the cells are creating the problem. Could you advise if I am correct, the sample shown below is the data the For Loop is running on and how this could be fixed.




Excel Workbook
BCDE
10ITEMACC No.JOB NO. AJOB NO. B
11Field Hand Services9-0120CASN46CAPG21
12SURVEYS9-0400CA9999
13Petty Cash1-1109CAPK69CA9999
14Training/Conferences/Courses9-0125CAPG21
15Drafting & Computing Services9-0115CASN46
16Payroll Recoveries6-1099CA0000
17Staff Paye2-2101CAPG21
18Management Fees4-3000CAPL70
19Management Fees4-3000CAPG20
20Director Fees6-1091CASR47
21Vehicle Operating Costs9-0760CASO32CAPK69
22
23
24
Sheet2
 
Upvote 0
Your starting row in the data you posted is 11, so it seems to me the loop should start at 11 too, not 37.
 
Upvote 0
Hi hyat I am of the understanding i = 37 represents the number on lines the loop is looking at.
 
Upvote 0
Also, it may be that the loop is looking at data in the "Sort Sum" sheet, whereas your sheet is called "Sheet2"?
 
Upvote 0
A useful method of finding out how the code is working, i.e. whether the loop is being entered into, what values the variables are taking, whether the problem is with the .Value part or not, would be to step through the code line by line by using the F8 key.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,290
Members
452,902
Latest member
Knuddeluff

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