Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

assistance mod code to shows balances the customers

0

hello  experts !

i have  this  file  and  the  code  it  works      the  code  works  on sheet  account   when  select  from dropdown in b1,  and  choose  the  dates b2,b3    but   i  would  mod  the code  I put  the  expected  result  in sheet  "result "how  the  data should be  see the  values  highlighted blue  I have  this  formula  

Range("e7:e" & lr).FormulaR1C1 = "=R[-1]C+RC[-7]-RC[-1]"

   but  I no  know  where  i add  it   and  should  be  bring  the  value  highlighted  red  this  always  bring  from sheet1 to  sheet  account  in row 7   if  the  name  contains  balance  in sheet 1 

I hope  to anybody  help me 

Sub sama1()
 Dim LastR As Long
 Dim SText As String
 Dim StDate As Date
 Dim EndDate As Date
 Dim LastR1 As Long
 Application.ScreenUpdating = False
 Sheets("account").Range("A5:h10000").ClearContents

 SText = Sheets("account").Range("B1")
 If SText = "" Then SText = "*"
 StDate = Sheets("account").Range("B2")
 If StDate = 0 Then StDate = WorksheetFunction.Min(Sheets("DATA").Columns(2))
 EndDate = Sheets("account").Range("B3")
 If EndDate = 0 Then EndDate = WorksheetFunction.Max(Sheets("DATA").Columns(2))
 LastR1 = Sheets("DATA").Cells(Rows.Count, 2).End(xlUp).Row
 Sheets("DATA").Range("A3:g" & LastR1).AutoFilter Field:=6, Criteria1:=SText
 Sheets("DATA").Range("A3:g" & LastR1).AutoFilter Field:=2, Criteria1:=">=" & Format(StDate, "mm/dd/yy"), Operator:=xlAnd, _
 Criteria2:="<=" & Format(EndDate, "mm/dd/yy")
 LastR = Sheets("DATA").Cells(Rows.Count, 2).End(xlUp).Row
 Sheets("DATA").Range("A3:g" & LastR).SpecialCells(xlCellTypeVisible).COPY
 Sheets("account").Range("A5").PasteSpecial
 Sheets("account").Range("A5").Select
 Sheets("DATA").Range("A3:g3").AutoFilter
 Application.ScreenUpdating = True
End Sub
Answer
Discuss

Answers

0
Selected Answer

Hi Kalil and welcome to the Forum

From the RESULT sheet in your workbook, I've seen what you want and corrected your macro (so it still operates from the button on the "account" sheet of the revised file, attached below).

Firstly, the balance formula you stated in  your question is slightly wrong, needing the correction in bold below:

Activecell.FormulaR1C1 = "=R[-1]C+RC[-2]-RC[-1]"

Also your macro starts with a statement which made that sheet way too big (by referring to 10,000 columns) so I changed it to Sheets("account").Range("A6:h114").ClearContents (which seems to be enough) and removed the other rows.

In the "account" sheet, I've inserted a Balance column E to your table. That means that your code has to be split to paste DATA columns A:D first, then columns E:G (to "account" F:H). I've changed the paste type to ValuesOnly (which gives a neater result in "account").

In new column E: the line under  'apply formula adds the corrected formula as far as the paste has filled values in the sheet. 

In row 6, the first balance value is copied from C3 (and a description added under H6).

REVISION 1: in the revised code extract below, I've added an If then statement on LastR to cover when date filters produce no transactions. Also added Applications.EnableEvents= False (to stop the Worksheet_Change macro triggering during execution) with ...=True at end of macro. REVISION 2: Corrected variable name in "remaining 3" copy statement and used .ShowAllData to clear Autofilter (before End If). 

Here are the changes to your code (in bold, with comments):

 LastR = Sheets("DATA").Cells(Rows.Count, 1).End(xlUp).Row
 If LastR > 3 Then ' copy data if there's any
    'copy first four filtered columns
    Sheets("DATA").Range("A4:D" & LastR).SpecialCells(xlCellTypeVisible).COPY
    Sheets("account").Range("A7").PasteSpecial Paste:=xlPasteValues
    ' apply formula
    Sheets("account").Range("E7:E" & Cells(Rows.Count, 2).End(xlUp).Row).FormulaR1C1 = "=R[-1]C+RC[-2]-RC[-1]"
    'copy remaining three filtered columns
    Sheets("DATA").Range("E4:G" & LastR).SpecialCells(xlCellTypeVisible).COPY
    Sheets("account").Range("F7").PasteSpecial Paste:=xlPasteValues

    Sheets("account").Range("A5").Select
    Sheets("DATA").Range("A3:g3").ShowAllData
 End If

Otherwise your code is the same as before. I changed the formula in F3 to 

=C3+D3-E3
since C3 should form part of the balance (and to match with the table balance, once data is gained).

Change "name" then  click the "get data" button and it should work okay. If so, you can delete the sheet "RESULT" and remove the red and blue fills from column E.

Note that I added a small Worksheet_Change event macro to the "account sheet" so that the table data is cleared if the name or to/from dates in row 3 are changed (otherwise the table doesn't match the lookup values in the table, until "get data" is clicked. Here's that code:

Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Range("B1:C3"), Target) Is Nothing Then     
Exit Sub
End If ' Otherwise if a search value changed, clear the data
Sheets("account").Range("A6:H114").ClearContents
End Sub

Hope this is what you need.

Discuss

Discussion

John
thank  you  but  there  is  a problem  if  i  select  name  and  put  date is  not  existed   for  the  name  in cells from date    and  to  date  it  copies   the  headrs  of  topics   , it  supposes  showing  empty  because  the  customer  doesn't  contain  the  date which I put  it  ,   may  you  fix  it  ,please?
Kalil (rep: 36) Mar 11, '21 at 6:48 am
Kalil

Please see Revision 1 to my Answer (and the chnaged file)- that should fix the problem with unfound dates. I havdn't tested that!

I didn't copy headers from DATA since I've added a column E to "account".
John_Ru (rep: 6142) Mar 11, '21 at 7:55 am
Argh! That didn't work properly. I will look later
John_Ru (rep: 6142) Mar 11, '21 at 7:57 am
Kalil

Sorry but I forgot to say last night that my Revision 2 (and revised file) should fix the problem you mentioned). Please check abd get back to me.
John_Ru (rep: 6142) Mar 12, '21 at 10:34 am
Jonn
I trust  you  to will  solve  it   ,  now  every  thing  is  ok
many  thanks 
Kalil (rep: 36) Mar 12, '21 at 11:32 am
That's good. Thanks for selecting my Answer Kalil
John_Ru (rep: 6142) Mar 12, '21 at 12:21 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login