• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How to get macro to select sheet ("INCOMEXPENSESa") before running

Lymm

Member
Hi i have some code which extracts the expenses from a list of combined income and expenses. It runs great if I manually select the correct sheet ("INCOMEXPENSESa") first. If I dont select the right sheet it creats havoc on the active sheet :) Im sure this should be simple but I cant seem to get the right sintax to select the sheet in the macro. Thank you if anyone can help me with this.

Code:
Sub ExtractExpensesc()
'This is macro 3 Select sheet INCOMEXPENSESa first
  Dim twb As Workbook
  Dim rng As Range
  Dim ar As Variant
  Dim mysheet As Worksheet
  Dim Name As String
  Dim Sheet As Worksheet
  

  Set twb = ThisWorkbook
  Set rng = Range("A2:E2")
  ar = Array("Payment", "Name1", "Name2", "Amount", "Date")
  rng = ar

  rng.Borders(xlEdgeRight).LineStyle = xlContinuous
  rng.Borders(xlEdgeLeft).LineStyle = xlContinuous
  rng.Borders(xlEdgeTop).LineStyle = xlContinuous
  rng.Borders(xlEdgeBottom).LineStyle = xlContinuous
  rng.Borders(xlInsideVertical).LineStyle = xlContinuous
  
  Range("A3", Range("E65536").End(xlUp)).Sort [d3], 1

  Range("D2", Range("E65536").End(xlUp)).AutoFilter 1, "<0"
  Range("A2", Range("E65536").End(xlUp)).Copy
  Set mysheet = Sheets.Add
  On Error Resume Next
  mysheet.Name = "ExtractedExpensesc"
  ActiveWorkbook.Sheets("ExtractedExpensesc").Tab.ColorIndex = 6

  
  [a2].PasteSpecial

  [A1] = -1
  [A1].Copy
  Range("D3", Range("D65536").End(xlUp)).PasteSpecial , 4
  Range("D3", Range("D65536").End(xlUp)).Style = "Currency"
  Columns("A:E").AutoFit
  
  Range("A3", Range("E65536").End(xlUp)).Sort [b3], 1

  twb.Sheets("INCOMEXPENSESa").[d2].AutoFilter
  [A1].Select
End Sub
 
Thank you Hui,:awesome:, I must have tried every thing except that. I think I was trying to activate it instead of select. I knew it would be simple but if you dont get it quite right it dont work :oops:
 
Hi Lymm,
You have already solved it but I couldn't stop myself when I saw five lines of codes for applying borders;
Code:
  rng.Borders.LineStyle = xlContinuous
should do the trick.
With Regards
Rudra
 
Back
Top