further my previous post

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Further to my previous post

Further to my previous post

resolvedResolved · High Priority · Version 2007

Louisa has attended:
Excel VBA Intro Intermediate course
Excel VBA Advanced course

Further to my previous post

Here is the code that I have been trying to use.

It seems to jump 2 rows after it gets to the dates larger or equal to the first of the month.

It also deleted one row which was less than the first day of the month.

I am completely confused.


Option Explicit
Option Compare Text
Dim dateFirstOfMonth ' first day of current month
Dim intRowCount As Integer ' holds the row that is going to be copied from the trade sheet
Dim intTargetRowCount As Integer ' holds the row that the data is going on cashflow
Dim intNumRows As Integer 'hold the no of rows of data in the cashflow sheet
Dim intColumnCount As Integer 'hold the number of columns in our data set

Sub Cashflow()

Call CashflowDeleteCurrentMonth



End Sub


Sub CashflowDeleteCurrentMonth()
'
' Cashflow Macro

dateFirstOfMonth = Sheets("Front Sheet").Range("H13")

'
Application.ScreenUpdating = False


intNumRows = Sheets("cashflow sheet").Range("a5").CurrentRegion.Rows.Count
'count how many rows of data in our data in our data set starting in row 5



intTargetRowCount = 5

For intTargetRowCount = 1 To intNumRows

If (Sheets("cashflow sheet").Range("a5").Cells(intTargetRowCount, 1).Value >= dateFirstOfMonth) Then

' looking for dates greater than or equal to the first day of the month

For intColumnCount = 1 To 25 ' I want to delete cols 1 to 25

Cells(intTargetRowCount, intColumnCount).ClearContents

Next intColumnCount

intTargetRowCount = intTargetRowCount + 1
'after the data is cleared it moves onto the next row

End If 'first condition is finished and closed off

Next
'it searches for the next date greater than the first day of the month




Application.ScreenUpdating = True


End Sub

RE: Further to my previous post

Hi Louisa

There is one line not needed in your code

intTargetRowCount = intTargetRowCount + 1

If you comment this line out the current month data gets cleared as you'd expect. (There is no need to increase intTargetRowCount as it is part of a For loop)

Also the code in the workbook file you sent is slightly different from the code in your mesage. I think the line:
Columns(intColumnCount).clearcontents

should be replaced by

Cells(intTargetRowCount, intColumnCount).ClearContents

I hope that solves why August data wasn't getting cleared.

Regards
Doug Dunn
Best STL

RE: Further to my previous post

Hi,

The code is still deleting lines with date 31/07/12 but not before. It also didn't delete all the bottom dated 09/08/12.

I think there must be a mix up on the number of rows on the cashflow and on the trade sheet as its the 31.07.12 is 4 rows and the ones left at the bottom are 4 rows. Not sure what is going on.


Here is the code now.

Thanks for your help so far.

Louisa

Sub CashflowDeleteCurrentMonth()
'
' Cashflow Macro

dateFirstOfMonth = Sheets("Front Sheet").Range("H13")

'
Application.ScreenUpdating = False


intNumRows = Sheets("Cashflow sheet").Range("A4").CurrentRegion.Rows.Count
'count how many rows of data in our data in our data set starting in row 5


intTargetRowCount = 5

For intTargetRowCount = 1 To intNumRows

If (Sheets("cashflow sheet").Range("a5").Cells(intTargetRowCount, 1).Value >= dateFirstOfMonth) Then

' looking for dates greater than or equal to the first day of the month

For intColumnCount = 1 To 24 ' I want to delete cols 1 to 24

Cells(intTargetRowCount, intColumnCount).ClearContents

Next intColumnCount



End If 'first condition is finished and closed off

Next
'it searches for the next date less than the reporting date in column 10 of total sales.




Application.ScreenUpdating = True


End Sub

RE: Further to my previous post

Another question I have in relation to this which is confusing me.

In the course we were given this code as part of the for next loop. Why is there a range and cells. Is the range the starting point and the cells a point in relation to the range. As I know you can use Cells to reference a cell directly.

If Sheets("Total Sales").Range("a10").Cells(intRowCount, 10).Value = strName Then

I have managed to write some of my own loops already but this one is completely confusing me.

Thanks

Louisa

RE: Further to my previous post

Looks like I fixed it with this correction. It all has become clearer. Not fully understanding it but getting there.

Cells(intRowcount + 4, intColumncount).ClearContents

Let me know if looks ok to and if you could answer my other question too. That would be really helpful.

Thanks again.

RE: Further to my previous post

Hi Louisa

Well done fixing the last question and glad it's becoming clearer.
Sorry I don't think I can help with the earlier question in the time we allocate for forum questions. But I will do my best to answer short specific questions.

Regards
Doug
Best STL

Tue 4 Sep 2012: Automatically marked as resolved.

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Reset Excel toolbars to default settings

If you find any of your toolbars in Excel have changed (i.e. they are missing or have extra buttons) then you can reset them to their defaults.

1. Go to Tools - Customise.
2. Select the Toolbars tab.
3. Select (highlight) the name of the toolbar you wish to reset, then click the Reset button on the right.
4. Close the dialogue box.

View all Excel hints and tips


Server loaded in 0.08 secs.